* @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([], $res); } public function testGet1() { $c = new Config(); $c->confFile = "/tmp/testconf.php"; $c->default = [ "database" => [ "dsn" => null, "username" => null, "password" => null, "driver_options" => null, "tableprefix" => ""]]; $res = $c->get("database"); $this->assertSame([ "dsn" => null, "username" => null, "password" => null, "driver_options" => null, "tableprefix" => ""], $res); } public function testSet1() { $c = new Config(); $c->confFile = "/tmp/testconf.php"; $c->default = [ "database" => [ "dsn" => null, "tableprefix" => ""]]; $res = $c->set("database", ["dsn" => 1,"tableprefix" => 3]); $this->assertSame(true, $res); } public function testGet2() { $c = new Config(); $c->confFile = "/tmp/testconf.php"; $c->default = [ "database" => [ "dsn" => null, "tableprefix" => ""]]; $res = $c->get("database"); $this->assertSame([ "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 = [ "database" => [ "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 = [ "database" => [ "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 = [ "database" => [ "dsn" => null, "tableprefix" => ""]]; $this->setExpectedException("Exception"); $res = $c->set("database", ["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 = [ "database" => [ "dsn" => null, "tableprefix" => ""]]; $this->setExpectedException("Exception"); $res = $c->set("database", ["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 = [ "database" => [ "dsn" => null, "tableprefix" => ""]]; $this->setExpectedException("Exception"); $res = $c->set("database", ["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 = [ "database" => [ "dsn" => null, "user" => null, "password" => null, "tableprefix" => ""]]; $res = $c->set("database", ["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 = [ "database" => [ "dsn" => null, "user" => null, "password" => null, "tableprefix" => ""], "servers" => [ "not configured" => [ "type" => "bind", "username" => "", ], ] ]; $res = $c->get("servers"); $this->assertSame($res, ["not configured" => [ "type" => "bind", "username" => "", ]]); } public function testSet7() { $c = new Config(); $c->confFile = "/tmp/testconf.php"; chmod("/tmp/testconf.php", 0666); $c->default = [ "database" => [ "dsn" => null, "user" => null, "password" => null, "tableprefix" => ""], "servers" => [ "not configured" => [ "type" => "bind", "username" => "", ], ] ]; $res = $c->set("servers", [ "127.0.0.1" => [ "type" => "BIND", "username" => "toto", ]]); } public function testGet7() { $c = new Config(); $c->confFile = "/tmp/testconf.php"; chmod("/tmp/testconf.php", 0666); $c->default = [ "database" => [ "dsn" => null, "user" => null, "password" => null, "tableprefix" => ""], "servers" => [ "not configured" => [ "type" => "bind", "username" => "", ], ] ]; $res = $c->get("servers"); $this->assertSame($res, [ "127.0.0.1" => [ "type" => "BIND", "username" => "toto", ]]); } public function testGet8() { $c = new Config(); $c->confFile = "/tmp/testconf.php"; chmod("/tmp/testconf.php", 0666); $c->default = [ "database" => [ "dsn" => null, "user" => null, "password" => null, "tableprefix" => ""], "servers" => [ "not configured" => [ "type" => "bind", "username" => "", "password" => "", ], ] ]; $res = $c->get("servers"); $this->assertSame($res, [ "127.0.0.1" => [ "type" => "BIND", "username" => "toto", "password" => "", ]]); } }