*/ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase { // Test with column name 'group', 'object', 'where', 'with space' // Test with table name 'group', 'object', 'where', 'with space' // For the 3 DB engines public $engine="{ENGINE}"; public $confs = array ( "sqlite" => array ( "dsn" => "sqlite:/tmp/databaseDBLayeroo.db", "username" => null, "password" => null, "driver_options" => null, "tableprefix" => "", ), "mysql" => array ( "dsn" => "mysql:host=127.0.0.1;port=3306;dbname=test", "username" => "root", "password" => "lqsym", "driver_options" => null, "tableprefix" => "", ), "pgsql" => array ( "dsn" => "pgsql:host=127.0.0.1;port=5432;dbname=dbname", "username" => "root", "password" => "root", "driver_options" => null, "tableprefix" => "", ), ); private function db1 () { $dbconfig = $this->confs["{ENGINE}"]; $db1 = new dblayeroo ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db1->table ("groupedoo"); $db1->fields (array ("group"=>array ("varchar(255)", "not null"), "object"=>array ("varchar(255)", "not null"), "where"=>array ("varchar(255)", "not null"), "with space"=>array ("varchar(255)"))); $db1->unique (array ()); $db1->primary ("group"); return $db1; } private function db2 () { $dbconfig = $this->confs["{ENGINE}"]; $db2 = new dblayeroo ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db2->table ("usersoo"); $db2->fields (array ("uid"=>array ("integer", "not null", "autoincrement"), "gecos"=>array ("varchar(255)", "not null"), "password"=>array ("varchar(255)", "not null"), "group" => array ("varchar(255)", "not null"), )); $db2->unique (array ("gecos","password")); $db2->primary ("uid"); $db2->foreign (array ("group" => array ("groupedoo", "group", "ON DELETE CASCADE"))); return $db2; } private function db3 () { $dbconfig = $this->confs["{ENGINE}"]; $db3 = new dblayeroo ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); return $db3; } public function test_dropTable () { $dbconfig = $this->confs["{ENGINE}"]; $db = new dblayeroo ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); foreach (array ("usersoo", "groupedoo", "multipleoo", "multiple2oo", "users3oo", "readORoo") as $table) { $db->table ($table); try { $res = $db->dropTable(); } catch (Exception $e) { } } $db->disconnect (); // Never generate an error, just drop the table if it exists, and do noting // if it doesn't exists } public function test_createTable1 () { // Create a table named groupedoo $db1 = $this->db1 (); $res = $db1->createTable (); $db1->disconnect (); $this->assertSame (0, $res); } public function test_createTable2 () { // Create a table named usersoo $db2 = $this->db2 (); $res = $db2->createTable (); $db2->disconnect (); $this->assertSame (0, $res); } public function test_select1 () { // Select all on the table : nothing $db1 = $this->db1 (); $res = $db1->select()->execute (); $db1->disconnect (); $this->assertSame (array (), $res); } public function test_insert1 () { // Insert without value : raise an exception $this->setExpectedException ("Exception"); $db1 = $this->db1 (); $res = $db1->insert()->execute (); $db1->disconnect (); } public function test_insert2 () { // Insert : missing not null field : exception $this->setExpectedException ("Exception"); $db1 = $this->db1 (); $res = $db1->insert() ->setValues(array ("group"=>"group1", "where"=>"where")) ->execute (); $db1->disconnect (); } public function test_insert3 () { // Insert : first row inserted $db1 = $this->db1 (); $res = $db1->insert()->setValues(array ("group"=>"group1", "where"=>"where", "object"=>"object"))->execute (); $db1->disconnect (); // As the key is not an autoincrement, the lastInsertID can be 0 or 1 $this->assertGreaterThanOrEqual ($res, "0"); } public function test_select2 () { // Select all on the table : nothing $db1 = $this->db1 (); $res = $db1->select()->execute (); $db1->disconnect (); $this->assertSame (array (array ("group"=>"group1", "object"=>"object", "where"=>"where", "with space"=>null)), $res); } public function test_update1 () { // update the all the rows of the table (without WHERE) $db1 = $this->db1 (); $res = $db1->update()->setValues(array ("group"=>"group2", "where"=>"where", "object"=>"object"))->execute (); $db1->disconnect (); $this->assertSame (1, $res); } public function test_select3 () { // Select all on the table after update $db1 = $this->db1 (); $res = $db1->select()->execute (); $db1->disconnect (); $this->assertSame (array (array ("group"=>"group2", "object"=>"object", "where"=>"where", "with space"=>null)), $res); } public function test_update2 () { // update the all the rows of the table (with inexisting WHERE) $db1 = $this->db1 (); $res = $db1->update() ->setValues(array ("group"=>"group2", "where"=>"where", "object"=>"object")) ->whereAdd ("group", "=", "group1") ->execute (); $db1->disconnect (); $this->assertSame (0, $res); } public function test_update3 () { // update the all the rows of the table (with existing WHERE) $db1 = $this->db1 (); $res = $db1->update() ->setValues(array ("group"=>"group1", "where"=>"where", "object"=>"object")) ->whereAdd ("group", "=", "group2") ->execute (); $db1->disconnect (); $this->assertSame (1, $res); } public function test_update4 () { // update the all the rows of the table : NOT NULL value not provided // (already existing in the table) $db1 = $this->db1 (); $res = $db1->update() ->setValues(array ("group"=>"group1")) ->execute (); $db1->disconnect (); $this->assertSame (1, $res); } public function test_delete1 () { // Delete : WHERE return nothing $db1 = $this->db1 (); $res = $db1->delete () ->whereAdd ("group", "=", "group2") ->execute (); $db1->disconnect (); $this->assertSame (0, $res); } public function test_delete2 () { // Delete all $db1 = $this->db1 (); $res = $db1->delete () ->execute (); $db1->disconnect (); $this->assertSame (1, $res); } public function test_select4 () { // Select all on the table : nothing $db1 = $this->db1 (); $res = $db1->select()->execute (); $db1->disconnect (); $this->assertSame (array (), $res); } public function test_insert5 () { // Insert : first row inserted for TABLE 2 tests $db1 = $this->db1 (); $res = $db1->insert()->setValues(array ("group"=>"group1", "where"=>"where", "object"=>"object"))->execute (); $db1->disconnect (); // As the key is not an autoincrement, the lastInsertID can be 0 or 1 $this->assertGreaterThanOrEqual ($res, "0"); } /////////////////// /// TABLE 2 /// /////////////////// public function test_insertAutoincrement1 () { // Test autoincrement $db1 = $this->db1 (); $db2 = $this->db2 (); $db2->setForeignObj ($db1); $res = $db2->insert()->setValues(array ("gecos"=>"name", "password"=>"toto", "group"=>"group1"))->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame ("1", $res); } public function test_insertAutoincrement2 () { // Test autoincrement $db1 = $this->db1 (); $db2 = $this->db2 (); $db2->setForeignObj ($db1); $res = $db2->insert()->setValues(array ("gecos"=>"firstname2", "password"=>"toto2", "group"=>"group1"))->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame ("2", $res); } public function test_insertAutoincrement3 () { // Test autoincrement $db1 = $this->db1 (); $db2 = $this->db2 (); $db2->setForeignObj ($db1); $res = $db2->insert()->setValues(array ("gecos"=>"firstname3", "password"=>"toto3", "group"=>"group1"))->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame ("3", $res); } public function test_delete3 () { // Delete with WHERE clause $db1 = $this->db1 (); $db2 = $this->db2 (); $db2->setForeignObj ($db1); $res = $db2->delete () ->whereAdd ("gecos", "LIKE", "firstname%") ->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (2, $res); } public function test_select5 () { // Select all on the table : one entry "gecos"=>"name" $db1 = $this->db1 (); $db2 = $this->db2 (); $db2->setForeignObj ($db1); $res = $db2->select()->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (array (array ("uid"=>1, "gecos"=>"name", "password"=>"toto", "group"=>"group1")), $res); } /// SCHEMA MANAGEMENT /// public function test_getTableSchema1 () { $db3 = $this->db3 (); $res = $db3->getTableSchema ("usersoo"); $db3->disconnect (); $this->assertSame ( array ("fields"=>array ( "uid"=>array ("integer", "not null", "autoincrement"), "gecos"=>array ("varchar(255)", "not null"), "password"=>array ("varchar(255)", "not null"), "group" => array ("varchar(255)", "not null"), ), "primary"=>"uid", "unique"=>array ("gecos","password"), "foreign" => array ( "group" => array ('groupedoo', 'group', 'ON DELETE CASCADE'), ), ), $res); } // JOINS public function test_insertJoin1 () { $db1 = $this->db1 (); $db1->insert() ->setValues(array ("group"=>"group2", "object"=>"object", "where"=>"where")) ->execute (); $db2 = $this->db2 (); $db2->setForeignObj ($db1); $res = $db2->insert() ->setValues(array ("gecos"=>"name2", "password"=>"pwd2", "group"=>"group1")) ->execute (); $res = $db2->insert() ->setValues(array ("gecos"=>"name3", "password"=>"pwd3", "group"=>"group1")) ->execute (); $res = $db2->insert() ->setValues(array ("gecos"=>"name4", "password"=>"pwd4", "group"=>"group1")) ->execute (); $db1->disconnect (); $db2->disconnect (); } public function test_innerJoin1 () { // No filter $db1 = $this->db1 (); $db2 = $this->db2 (); $res = $db2->select () ->joinInner ($db1, array ("group"=>"group")) ->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (array ( array ( 'usersoo.uid' => 1, 'usersoo.gecos' => 'name', 'usersoo.password' => 'toto', 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, ), array ( 'usersoo.uid' => 4, 'usersoo.gecos' => 'name2', 'usersoo.password' => 'pwd2', 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, ), array ( 'usersoo.uid' => 5, 'usersoo.gecos' => 'name3', 'usersoo.password' => 'pwd3', 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, ), array ( 'usersoo.uid' => 6, 'usersoo.gecos' => 'name4', 'usersoo.password' => 'pwd4', 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, ), ), $res); } public function test_leftJoin2 () { // No filter $db1 = $this->db1 (); $db2 = $this->db2 (); $res = $db1->select () ->joinLeft ($db2, array ("group"=>"group")) ->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (array ( array ( 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, 'usersoo.uid' => 1, 'usersoo.gecos' => 'name', 'usersoo.password' => 'toto', 'usersoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, 'usersoo.uid' => 4, 'usersoo.gecos' => 'name2', 'usersoo.password' => 'pwd2', 'usersoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, 'usersoo.uid' => 5, 'usersoo.gecos' => 'name3', 'usersoo.password' => 'pwd3', 'usersoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group1', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, 'usersoo.uid' => 6, 'usersoo.gecos' => 'name4', 'usersoo.password' => 'pwd4', 'usersoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group2', 'groupedoo.object' => 'object', 'groupedoo.where' => 'where', 'groupedoo.with space' => NULL, 'usersoo.uid' => 0, 'usersoo.gecos' => NULL, 'usersoo.password' => NULL, 'usersoo.group' => NULL, ), ), $res); } public function test_leftJoin3 () { // Filter on the db1 $db1 = $this->db1 (); $db2 = $this->db2 (); $res = $db1->select () ->displayColumn ("group") ->joinLeft ($db2, array ("group"=>"group")) ->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (array ( array ( 'groupedoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group1', ), array ( 'groupedoo.group' => 'group2', ), ), $res); } public function test_leftJoin4 () { // Filter on the db1 $db1 = $this->db1 (); $db2 = $this->db2 (); $db2->displayColumn ("group"); $res = $db1->select () ->displayColumn ("group") ->joinLeft ($db2, array ("group"=>"group")) ->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (array ( array ( 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', ), array ( 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', ), array ( 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', ), array ( 'usersoo.group' => 'group1', 'groupedoo.group' => 'group1', ), array ( 'usersoo.group' => NULL, 'groupedoo.group' => 'group2', ), ), $res); } }