* @license BSD */ namespace Domframework\Tests; use Domframework\Config; /** Test the Config.php file */ class ConfigTest extends \PHPUnit_Framework_TestCase { public function testExternFile() { if (file_exists("/tmp/testconf.php")) { unlink("/tmp/testconf.php"); } } public function testParams1() { $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); } public function testGet6() { $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" => ""), "servers" => array ( "not configured" => array ( "type" => "bind", "username" => "", ), ) ); $res = $c->get("servers"); $this->assertSame($res, array ("not configured" => array ( "type" => "bind", "username" => "", ))); } public function testSet7() { $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" => ""), "servers" => array ( "not configured" => array ( "type" => "bind", "username" => "", ), ) ); $res = $c->set("servers", array ( "127.0.0.1" => array ( "type" => "BIND", "username" => "toto", ))); } public function testGet7() { $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" => ""), "servers" => array ( "not configured" => array ( "type" => "bind", "username" => "", ), ) ); $res = $c->get("servers"); $this->assertSame($res, array ( "127.0.0.1" => array ( "type" => "BIND", "username" => "toto", ))); } public function testGet8() { $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" => ""), "servers" => array ( "not configured" => array ( "type" => "bind", "username" => "", "password" => "", ), ) ); $res = $c->get("servers"); $this->assertSame($res, array ( "127.0.0.1" => array ( "type" => "BIND", "username" => "toto", "password" => "", ))); } }