* @license BSD */ namespace Domframework\Tests; use Domframework\Dblayer; class DblayerTestENGINE 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 = [ "sqlite" => [ "dsn" => "sqlite:/tmp/databaseDBLayer.db", "username" => null, "password" => null, "driver_options" => null, "tableprefix" => "", ], "mysql" => [ "dsn" => "mysql:host=127.0.0.1;port=3306;dbname=test", "username" => "root", "password" => "lqsym", "driver_options" => null, "tableprefix" => "", ], "pgsql" => [ "dsn" => "pgsql:host=127.0.0.1;port=5432;dbname=dbname", "username" => "root", "password" => "root", "driver_options" => null, "tableprefix" => "", ], ]; /** * @group singleton */ public function testDropTable() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); foreach ( ["users", "grouped", "multiple", "multiple2", "users3", "readOR"] 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 } /** * @group singleton */ public function testCreateTable1() { // Create a table named grouped $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = []; $db->primary = "group"; $res = $db->createTable(); $db->disconnect(); $this->assertSame(0, $res); } /** * @group singleton */ public function testInsert1() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = []; $db->primary = "group"; $res = $db->insert(["group" => "gr ou\"p", "object" => "/éobj%", "where" => "\$'\"", "with space" => "with space"]); // SQLite start at 1, MySQL start at 0... $this->assertSame($res, "gr ou\"p"); } public function testRead1() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = []; $res = $db->read([["group", "gr ou\"p"], ["object","/éobj%"], ["where","\$'\""], ["with space","with space"]]); $this->assertSame([0 => ["group" => "gr ou\"p", "object" => "/éobj%", "where" => "\$'\"", "with space" => "with space"]], $res); } public function testUpdate1() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = []; $db->primary = "group"; // Don't update primary key $res = $db->update("gr ou\"p", ["object" => "%éàoppp", "with space" => "WITH SPACE"]); $this->assertSame(1, $res); } public function testRead2() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = []; $res = $db->read([["group", "gr ou\"p"], ["object","%éàoppp"], ["where","\$'\""], ["with space","WITH SPACE"]]); $this->assertSame([0 => ["group" => "gr ou\"p", "object" => "%éàoppp", "where" => "\$'\"", "with space" => "WITH SPACE"]], $res); } public function testUpdate2() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = [["group","object"]]; $db->primary = "group"; // Update primary key $res = $db->update("gr ou\"p", ["group" => "NEW GROUP", "object" => "%éàoppp", "with space" => "WITH SPACE"]); $this->assertSame(1, $res); } public function testRead3() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = []; $res = $db->read([["group", "NEW GROUP"], ["object","%éàoppp"], ["where","\$'\""], ["with space","WITH SPACE"]]); $this->assertSame([0 => ["group" => "NEW GROUP", "object" => "%éàoppp", "where" => "\$'\"", "with space" => "WITH SPACE"]], $res); } public function testUpdate3() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "grouped"; $db->fields = ["group" => ["varchar", "255", "not null"], "object" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = ["group"]; $db->primary = "group"; // Update primary key with primary key in unique with same values to test if // the exception is NOT raised $res = $db->update("NEW GROUP", ["group" => "NEW GROUP", "object" => "%éàoppp", "with space" => "WITH SPACE"]); // SQLite and PostgreSQL return 1 line modified // MySQL return 0 by default because there is no modification on the line // http://fr2.php.net/manual/en/pdostatement.rowcount.php#104930 // Now, the MYSQL_ATTR_FOUND_ROWS is added to connection to be the same in // all the engines $this->assertSame(1, $res); } /** * @group singleton */ // Part to test the foreign keys public function testCreateTable2() { // Create a table named group $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->disconnect(); $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "users"; $db->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->foreign = [ "groupmember" => ["grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"], ]; $res = $db->createTable(); $this->assertSame(0, $res); } public function testInsert2() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "users"; $db->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->unique = ["user"]; $db->primary = "user"; $db->foreign = [ "groupmember" => ["grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"], ]; $res = $db->insert(["user" => "Us ou\"r", "groupmember" => "NEW GROUP", "where" => "\$'\"", "with space" => "with space"]); $db->disconnect(); // SQLite start at 1, MySQL start at 0... $this->assertSame($res <= 2 || $res === "Us ou\"r", true); } // Test the unique feature public function testInsert3() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "users"; $db->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; // Unique simple in insert $db->unique = ["user"]; $db->primary = "user"; $db->foreign = [ "groupmember" => ["grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"], ]; $this->setExpectedException("Exception"); $res = $db->insert(["user" => "Us ou\"r", "groupmember" => "NEW GROUP", "where" => "\$'\"", "with space" => "with space"]); } public function testInsert4() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "users"; $db->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; // Unique multiple in insert $db->unique = [["user", "groupmember"]]; $db->primary = "user"; $db->foreign = [ "groupmember" => ["grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"], ]; $this->setExpectedException("Exception"); $res = $db->insert(["user" => "Us ou\"r", "groupmember" => "NEW GROUP", "where" => "\$'\"", "with space" => "with space"]); } /** * @group singleton */ // Test multiple actions in one single connection public function testMultiple1() { // Create a table named group $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "multiple"; $db->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db->createTable(); $db2 = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db2->table = "multiple2"; $db2->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $db2->createTable(); $res = $db2->read([["user", "toto"]]); $res = $db->read([["user", "toto"]]); $db->disconnect(); $this->assertSame([], $res); } /** * @group singleton */ public function testCreateTable3() { // Create a table named group $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "users3"; $db->fields = ["user" => ["varchar", "255", "not null"], "groupmember" => ["varchar", "255", "not null"], "where" => ["varchar", "255", "not null"], "with space" => ["varchar", "255", "not null"]]; $res = $db->createTable(); $db->disconnect(); $this->assertSame(0, $res); } /** * Check the OR feature on the same column with different value */ public function testReadOR1() { $dbconfig = $this->confs["ENGINE"]; $db = new Dblayer( $dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"] ); $db->table = "readOR"; $db->fields = ["id" => ["integer"], "user" => ["varchar", "255", "not null"]]; $db->primary = "id"; $db->unique = ["id"]; $db->createTable(); $db->insert(["id" => "0", "user" => "test0"]); $db->insert(["id" => "1", "user" => "test1"]); $res = $db->read( [["id", 0], ["id", 1]], ["user"], null, true ); $db->disconnect(); $this->assertSame([0 => ["user" => "test0"], 1 => ["user" => "test1"]], $res); } }