diff --git a/Tests/configTest.php b/Tests/configTest.php new file mode 100644 index 0000000..70a9793 --- /dev/null +++ b/Tests/configTest.php @@ -0,0 +1,154 @@ + */ + +/** Test the config.php file */ +class test_config extends PHPUnit_Framework_TestCase +{ + + public function test_ExternFile () + { + if (file_exists ("/tmp/testconf.php")) + unlink ("/tmp/testconf.php"); + } + + public function test_params1 () + { + $c = new config (); + $res = $c->params (); + $this->assertSame (array (), $res); + } + + public function testGet1 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "username"=>null, + "password"=>null, + "driver_options"=>null, + "tableprefix"=>"")); + $res = $c->get ("database"); + $this->assertSame (array ( + "dsn" => null, + "username" => null, + "password" => null, + "driver_options" => null, + "tableprefix" => ""), $res); + } + + public function testSet1 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $res = $c->set ("database", array ("dsn"=>1,"tableprefix"=>3)); + $this->assertSame (true, $res); + } + + public function testGet2 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $res = $c->get ("database"); + $this->assertSame (array ( + "dsn" => 1, + "tableprefix" => 3), $res); + } + + /** Can't create file */ + public function testGet3 () + { + $c = new config (); + $c->confFile = "/tmp/1/2.3/dd/testconf.php"; + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $this->setExpectedException ("Exception"); + $res = $c->get ("database"); + } + + /** Can't read the file */ + public function testGet4 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + chmod ("/tmp/testconf.php", 0222); + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $this->setExpectedException ("Exception"); + $res = $c->get ("database"); + } + + /** File non exists and can not be created */ + public function testSet2 () + { + $c = new config (); + $c->confFile = "/tmp/1/2.3/dd/testconf.php"; + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $this->setExpectedException ("Exception"); + $res = $c->set ("database", array ("dsn"=>1,"tableprefix"=>3)); + } + + /** Can't read the file */ + public function testSet3 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + chmod ("/tmp/testconf.php", 0222); + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $this->setExpectedException ("Exception"); + $res = $c->set ("database", array ("dsn"=>1,"tableprefix"=>3)); + } + + /** Can't write the file */ + public function testSet4 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + chmod ("/tmp/testconf.php", 0444); + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "tableprefix"=>"")); + $this->setExpectedException ("Exception"); + $res = $c->set ("database", array ("dsn"=>1,"tableprefix"=>3)); + } + + /** Save TRUE/FALSE/NULL/String values */ + public function testSet5 () + { + $c = new config (); + $c->confFile = "/tmp/testconf.php"; + chmod ("/tmp/testconf.php", 0666); + $c->default = array ( + "database"=>array ( + "dsn"=>null, + "user"=>null, + "password"=>null, + "tableprefix"=>"")); + $res = $c->set ("database", array ("dsn"=>true, "user"=>null, + "password"=>"text", + "tableprefix"=>false)); + $this->assertSame (true, $res); + } +} diff --git a/Tests/configuration.php b/Tests/configuration.php new file mode 100644 index 0000000..6b12c0a --- /dev/null +++ b/Tests/configuration.php @@ -0,0 +1,10 @@ + array ( + "dsn" => "sqlite:/tmp/database.db", + "username" => null, + "password" => null, + "driver_options" => null, + "tableprefix" => "", + ), +); diff --git a/Tests/dblayerTest.php b/Tests/dblayerTest.php new file mode 100644 index 0000000..c5c86d7 --- /dev/null +++ b/Tests/dblayerTest.php @@ -0,0 +1,20 @@ + */ + +/** Test the dblayer.php file */ +class test_dblayer 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"] = ""; + } + + +}