Exemplo n.º 1
0
 /**
  * Test de récupération des données
  */
 public function testFirstAndAll()
 {
     Config::add(__DIR__ . '/../Test/namespace.php');
     $connection = new Connection('sqlite::memory:');
     $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $connection->query('CREATE TABLE IF NOT EXISTS news (
                             `id` INTEGER PRIMARY KEY AUTOINCREMENT,
                             `title` VARCHAR(50) NOT NULL,
                             `content` LONGTEXT NOT NULL,
                             `created_at` DATETIME NOT NULL
                         )');
     $allNews = [[':title' => 'Titre 1', ':content' => 'Contenu 1', ':createdAt' => '2016-07-18 09:20:35'], [':title' => 'Titre 2', ':content' => 'Contenu 2', ':createdAt' => '2016-07-18 10:20:35'], [':title' => 'Titre 3', ':content' => 'Contenu 3', ':createdAt' => '2016-07-18 11:20:35'], [':title' => 'Titre 4', ':content' => 'Contenu 4', ':createdAt' => '2016-07-18 12:20:35']];
     require_once __DIR__ . '/../Test/Table/NewsTable.php';
     require_once __DIR__ . '/../Test/Entities/Database/News.php';
     $newsTable = new NewsTable($connection, '', News::class);
     foreach ($allNews as $news) {
         $newsTable->add($news);
     }
     $this->assertEquals(0, $newsTable->badCount());
     $this->assertEquals(count($allNews), $newsTable->count());
     $first = $newsTable->getById(1);
     $this->assertEquals($allNews[0][':content'], $first->content);
     $impossible = $newsTable->getById(5);
     $this->assertFalse($impossible);
     $all = $newsTable->getAll();
     $this->assertEquals(count($allNews), $newsTable->count());
     foreach ($all as $index => $value) {
         $this->assertInstanceOf(News::class, $value);
         $this->assertSame($allNews[$index][':content'], $value->content);
     }
 }
Exemplo n.º 2
0
 /**
  * Test de la nouvelle méthode query
  */
 public function testQuery()
 {
     $connection = new Connection('mysql:host=localhost;dbname=phq', 'phq', 'phq');
     $request = $connection->query('SELECT id FROM phq.messages WHERE nickname LIKE :nickname AND id > :id AND content != :content', [':nickname' => '%a%', ':id' => 3, ':content' => null]);
     $this->assertEquals('SELECT id FROM phq.messages WHERE nickname LIKE :nickname AND id > :id AND content != :content', $request->queryString);
 }
Exemplo n.º 3
0
 /**
  * Récupère l'élément par rapport à son id
  *
  * @param int $id
  * @return bool
  */
 public function getById($id)
 {
     $sql = "SELECT * FROM {$this->tableName} WHERE id = :id";
     return $this->first($this->db->query($sql, [':id' => $id]));
 }