diff --git a/tests/OneDBTest.php b/tests/OneDBTest.php index ec38103..27386b2 100644 --- a/tests/OneDBTest.php +++ b/tests/OneDBTest.php @@ -31,21 +31,20 @@ public static function setUpBeforeClass() 'password' => $GLOBALS['db_password'] )); - // Init test data and also perform test on query method + // Init test data and perform test on query method self::$_db->query( - 'CREATE TABLE IF NOT EXISTS ' - . self::$_db->btick(self::$_table). ' (' + 'CREATE TABLE IF NOT EXISTS ' . self::$_table . ' (' . 'id INT(9) NOT NULL PRIMARY KEY AUTO_INCREMENT,' - . 'name VARCHAR(50) NOT NULL' + . 'name VARCHAR(50) NOT NULL,' + . 'email VARCHAR(50) NOT NULL,' + . 'tel VARCHAR(30) NOT NULL' . ') ENGINE = InnoDB' ); } public static function tearDownAfterClass() { - self::$_db->query( - 'DROP TABLE IF EXISTS ' . self::$_db->btick(self::$_table) - ); + self::$_db->query('DROP TABLE IF EXISTS ' . self::$_table); } public function testGetPDO() @@ -65,42 +64,101 @@ public function testBtick() public function testInsert() { - $id = self::$_db->insert(self::$_table, array( - 'name' => 'John Doe' + $aID = self::$_db->insert(self::$_table, array( + 'name' => 'John Doe', + 'email' => 'jd@jd.com', + 'tel' => 55555555 + )); + + $this->assertTrue(is_int($aID) && $aID > 0); + + $bID = self::$_db->insert(self::$_table, array( + 'name' => 'Skywalker', + 'email' => 'sw@sw.com', + 'tel' => 44444444 )); - $this->assertTrue(is_int($id) && $id > 0); + $this->assertTrue(is_int($bID) && $bID > 0); + } + + public function testMultiInsert() + { + self::$_db->multiInsert(self::$_table, + array('name', 'email', 'tel'), + array( + array( + 'John Doe', + 'john@doe.com', + 12345678 + ), + array( + 'John Smith', + 'john@smith.com', + 11223344 + ), + array( + 'Jack Smith', + 'jack@smith.com', + 87654321 + ) + ) + ); + $result = self::$_db->fetchRow( + 'SELECT * FROM ' . self::$_table . ' WHERE tel = 87654321' + ); + + // Check if last value was inserted successfully + $this->assertEquals('jack@smith.com', $result['email']); + } + + public function testSave() + { + $testTel = '22222222'; + + $tmpId = self::$_db->save(self::$_table, array( + 'name' => 'Bill Gates', + 'email' => 'bg@microsoft.com', + 'tel' => '11111111' + )); - $id = self::$_db->insert(self::$_table, array( - 'name' => 'Skywalker' + $id = self::$_db->save(self::$_table, array( + 'id' => $tmpId, + 'name' => 'Bill Gates', + 'email' => 'bg@microsoft.com', + 'tel' => $testTel )); - $this->assertTrue(is_int($id) && $id > 0); + $this->assertSame($tmpId, $id); + + $tel = self::$_db->fetchOne( + 'SELECT tel FROM ' . self::$_table . ' WHERE id = ' . $tmpId + ); + + $this->assertEquals($tel, $testTel); } public function testUpdate() { - $name = 'Steve Jobs'; + $testName = 'Steve Jobs'; self::$_db->update( self::$_table, - array('name' => $name), + array('name' => $testName), array('id = ?' => 1) ); $result = self::$_db->fetchRow( - 'SELECT * FROM ' . self::$_db->btick(self::$_table) - . ' WHERE ' . self::$_db->btick('id') . ' = 1' + 'SELECT * FROM ' . self::$_table . ' WHERE id = 1' ); - $this->assertEquals($name, $result['name']); + $this->assertEquals($testName, $result['name']); } public function testFetchAll() { $result = self::$_db->fetchAll( - 'SELECT * FROM ' . self::$_db->btick(self::$_table) + 'SELECT * FROM ' . self::$_table ); $this->assertTrue(is_array($result[0])); @@ -110,10 +168,11 @@ public function testFetchAll() public function testFetchAssoc() { $result = self::$_db->fetchAssoc( - 'SELECT * FROM ' . self::$_db->btick(self::$_table) + 'SELECT * FROM ' . self::$_table ); $this->assertTrue(count($result) > 0); + foreach ($result as $id => $row) { // Compare int with string $this->assertTrue($id == $row['id']); @@ -123,8 +182,7 @@ public function testFetchAssoc() public function testFetchRow() { $result = self::$_db->fetchRow( - 'SELECT * FROM ' . self::$_db->btick(self::$_table) - . ' WHERE ' . self::$_db->btick('id') . ' = 1' + 'SELECT * FROM ' . self::$_table . ' WHERE id = 1' ); $this->assertArrayHasKey('name', $result); @@ -135,8 +193,7 @@ public function testFetchOne() $name = 'Steve Jobs'; $result = self::$_db->fetchOne( - 'SELECT ' . self::$_db->btick('name') . ' FROM ' . self::$_db->btick(self::$_table) - . ' WHERE ' . self::$_db->btick('id') . ' = 1' + 'SELECT name FROM ' . self::$_table . ' WHERE id = 1' ); $this->assertEquals($name, $result); @@ -144,8 +201,23 @@ public function testFetchOne() public function testQuery() { - // insert, update, delete - // all fetching modes + $testName = 'Steve Jobs'; + + // Perform tests on fetching modes + $fetchAll = self::$_db->query( + 'SELECT * FROM ' . self::$_table + ); + $fetchRow = self::$_db->query( + 'SELECT * FROM ' . self::$_table . ' WHERE id = 1' + ); + $fetchOne = self::$_db->query( + 'SELECT name FROM ' . self::$_table . ' WHERE id = 1' + ); + + $this->assertTrue(is_array($fetchAll[0])); + $this->assertArrayHasKey('name', $fetchAll[0]); + $this->assertEquals($testName, $fetchRow['name']); + $this->assertEquals($testName, $fetchOne); } public function testDelete() @@ -156,8 +228,7 @@ public function testDelete() ); $result = self::$_db->fetchOne( - 'SELECT ' . self::$_db->btick('name') . ' FROM ' . self::$_db->btick(self::$_table) - . ' WHERE ' . self::$_db->btick('id') . ' = 2' + 'SELECT name FROM ' . self::$_table . ' WHERE id = 2' ); $this->assertNull($result); @@ -165,7 +236,23 @@ public function testDelete() public function testTruncte() { + self::$_db->truncate(self::$_table); + + $result = self::$_db->fetchAll('SELECT * FROM ' . self::$_table); + $this->assertEmpty($result); + } + + public function testDescribe() + { + $result = self::$_db->describe(self::$_table); + $realKeys = array('id', 'name', 'email', 'tel'); + $resultKeys = array_keys($result); + + $this->assertSame( + array_diff($realKeys, $resultKeys), + array_diff($resultKeys, $realKeys) + ); } public function testDrop()