From 2a4468cdd0ae7d233fffc6560fcabb9a10a879c5 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 11 Sep 2014 14:33:47 +0000 Subject: [PATCH] First version of tests for dblayer with support of 3 db engines. The tests files are generated from dblayerComplet git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1810 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/autoload.php.dist | 10 +++ Tests/{dblayerTest.php => dblayerComplet.php} | 72 ++++++++++--------- 2 files changed, 48 insertions(+), 34 deletions(-) rename Tests/{dblayerTest.php => dblayerComplet.php} (75%) diff --git a/Tests/autoload.php.dist b/Tests/autoload.php.dist index c0f5db4..a02be1c 100644 --- a/Tests/autoload.php.dist +++ b/Tests/autoload.php.dist @@ -14,6 +14,16 @@ if (file_exists ("/tmp/database.db")) unlink ("/tmp/database.db"); } +file_put_contents ("Tests/dblayerSqliteTest.php", + str_replace ("{ENGINE}", "sqlite", + file_get_contents ("Tests/dblayerComplet.php"))); +file_put_contents ("Tests/dblayerMySQLTest.php", + str_replace ("{ENGINE}", "mysql", + file_get_contents ("Tests/dblayerComplet.php"))); +file_put_contents ("Tests/dblayerPostgreSQLTest.php", + str_replace ("{ENGINE}", "pgsql", + file_get_contents ("Tests/dblayerComplet.php"))); + require_once ("domframework/config.php"); /** The class need to take care of configuration parameters */ class configuration extends config diff --git a/Tests/dblayerTest.php b/Tests/dblayerComplet.php similarity index 75% rename from Tests/dblayerTest.php rename to Tests/dblayerComplet.php index b239d15..44855ae 100644 --- a/Tests/dblayerTest.php +++ b/Tests/dblayerComplet.php @@ -3,30 +3,45 @@ @package domframework @author Dominique Fournier */ -/** Test the dblayer.php file */ -class test_dblayer extends PHPUnit_Framework_TestCase +class test_dblayer_{ENGINE} extends PHPUnit_Framework_TestCase { - public function test_createDB () - { - // Must use the model to create the database structure as there is no - // creation of tables in controller - $configuration = new configuration (); - $dbconfig = $configuration->get ("database"); - if (! isset ($dbconfig["tableprefix"])) - $dbconfig["tableprefix"] = ""; - } - // 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/database.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" => "", + ), + ); + + public function test_dropTable () + { + $dbconfig = $this->confs["{ENGINE}"]; + $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], + $dbconfig["password"], $dbconfig["driver_options"]); + $db->table = "grouped"; + $this->setExpectedException ("Exception"); + $res = $db->dropTable(); + // Never generate an error + } + public function test_createTable () { // Create a table named group - $configuration = new configuration (); - $dbconfig = $configuration->get ("database"); - if (! isset ($dbconfig["tableprefix"])) - $dbconfig["tableprefix"] = ""; + $dbconfig = $this->confs["{ENGINE}"]; $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db->table = "grouped"; @@ -40,10 +55,7 @@ class test_dblayer extends PHPUnit_Framework_TestCase public function test_insert () { - $configuration = new configuration (); - $dbconfig = $configuration->get ("database"); - if (! isset ($dbconfig["tableprefix"])) - $dbconfig["tableprefix"] = ""; + $dbconfig = $this->confs["{ENGINE}"]; $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db->table = "grouped"; @@ -61,10 +73,7 @@ class test_dblayer extends PHPUnit_Framework_TestCase public function test_read1 () { - $configuration = new configuration (); - $dbconfig = $configuration->get ("database"); - if (! isset ($dbconfig["tableprefix"])) - $dbconfig["tableprefix"] = ""; + $dbconfig = $this->confs["{ENGINE}"]; $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db->table = "grouped"; @@ -85,10 +94,7 @@ class test_dblayer extends PHPUnit_Framework_TestCase public function test_update1 () { - $configuration = new configuration (); - $dbconfig = $configuration->get ("database"); - if (! isset ($dbconfig["tableprefix"])) - $dbconfig["tableprefix"] = ""; + $dbconfig = $this->confs["{ENGINE}"]; $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db->table = "grouped"; @@ -98,16 +104,14 @@ class test_dblayer extends PHPUnit_Framework_TestCase "with space"=>array ("varchar", "255", "not null")); $db->unique = array (); $db->primary = "group"; - $db->update ("gr ou\"p", array ("object"=>"%éàoppp", - "with space"=>"WITH SPACE")); + $res = $db->update ("gr ou\"p", array ("object"=>"%éàoppp", + "with space"=>"WITH SPACE")); + $this->assertSame (1, $res); } public function test_read2 () { - $configuration = new configuration (); - $dbconfig = $configuration->get ("database"); - if (! isset ($dbconfig["tableprefix"])) - $dbconfig["tableprefix"] = ""; + $dbconfig = $this->confs["{ENGINE}"]; $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], $dbconfig["password"], $dbconfig["driver_options"]); $db->table = "grouped";