Refactored config

This commit is contained in:
Marcin Kurczewski 2014-09-10 17:41:47 +02:00
parent 2fab532fde
commit fdfb4acbf9
9 changed files with 66 additions and 56 deletions

View file

@ -3,9 +3,21 @@ namespace Szurubooru;
class Config extends \ArrayObject class Config extends \ArrayObject
{ {
public function __construct(array $configPaths = []) private $dataDirectory;
public function __construct($dataDirectory)
{ {
parent::setFlags(parent::ARRAY_AS_PROPS | parent::STD_PROP_LIST); $this->setFlags($this->getArrayObjectFlags());
$this->dataDirectory = $dataDirectory;
$this->tryLoadFromIni([
$dataDirectory . DIRECTORY_SEPARATOR . 'config.ini',
$dataDirectory . DIRECTORY_SEPARATOR . 'local.ini']);
}
public function tryLoadFromIni($configPaths)
{
if (!is_array($configPaths))
$configPaths = [$configPaths];
foreach ($configPaths as $configPath) foreach ($configPaths as $configPath)
{ {
@ -14,6 +26,11 @@ class Config extends \ArrayObject
} }
} }
public function getDataDirectory()
{
return $this->dataDirectory;
}
public function offsetGet($index) public function offsetGet($index)
{ {
if (!parent::offsetExists($index)) if (!parent::offsetExists($index))
@ -29,7 +46,7 @@ class Config extends \ArrayObject
{ {
if (!is_array($value)) if (!is_array($value))
{ {
$this->$key = $value; $this->offsetSet($key, $value);
} }
else else
{ {
@ -39,7 +56,7 @@ class Config extends \ArrayObject
foreach (explode('.', $section) as $subSection) foreach (explode('.', $section) as $subSection)
{ {
if (!$ptr->offsetExists($subSection)) if (!$ptr->offsetExists($subSection))
$ptr->offsetSet($subSection, new self()); $ptr->offsetSet($subSection, new \ArrayObject([], $this->getArrayObjectFlags()));
$ptr = $ptr->$subSection; $ptr = $ptr->$subSection;
} }
@ -49,4 +66,9 @@ class Config extends \ArrayObject
} }
} }
} }
private function getArrayObjectFlags()
{
return parent::ARRAY_AS_PROPS | parent::STD_PROP_LIST;
}
} }

View file

@ -6,9 +6,9 @@ class FileService
private $dataDirectory; private $dataDirectory;
private $httpHelper; private $httpHelper;
public function __construct($dataDirectory, \Szurubooru\Helpers\HttpHelper $httpHelper) public function __construct(\Szurubooru\Config $config, \Szurubooru\Helpers\HttpHelper $httpHelper)
{ {
$this->dataDirectory = $dataDirectory; $this->dataDirectory = $config->getDataDirectory();
$this->httpHelper = $httpHelper; $this->httpHelper = $httpHelper;
} }

View file

@ -2,11 +2,7 @@
$dataDirectory = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data'; $dataDirectory = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data';
return [ return [
\Szurubooru\Config::class => DI\object()->constructor([ \Szurubooru\Config::class => DI\object()->constructor($dataDirectory),
$dataDirectory . DIRECTORY_SEPARATOR . 'config.ini',
$dataDirectory . DIRECTORY_SEPARATOR . 'local.ini']),
\Szurubooru\Services\FileService::class => DI\object()->constructor($dataDirectory),
\Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')),

View file

@ -11,11 +11,10 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa
$host = 'localhost'; $host = 'localhost';
$port = 27017; $port = 27017;
$database = 'test'; $database = 'test';
$config = new \Szurubooru\Config(); $config = $this->mockConfig();
$config->database = new \StdClass; $config->set('database/host', 'localhost');
$config->database->host = 'localhost'; $config->set('database/port', '27017');
$config->database->port = 27017; $config->set('database/name', 'test');
$config->database->name = 'test';
$this->databaseConnection = new \Szurubooru\DatabaseConnection($config); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config);
$this->upgradeService = new \Szurubooru\UpgradeService($this->databaseConnection); $this->upgradeService = new \Szurubooru\UpgradeService($this->databaseConnection);
$this->upgradeService->prepareForUsage(); $this->upgradeService->prepareForUsage();

View file

@ -8,9 +8,9 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock(); return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
} }
public function mockConfig() public function mockConfig($path = null)
{ {
return new ConfigMock(); return new ConfigMock($path);
} }
public function createTestDirectory() public function createTestDirectory()

View file

@ -3,83 +3,76 @@ namespace Szurubooru\Tests;
final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase
{ {
private static $testFileName1; private $testDirectory;
private static $testFileName2; private $baseConfigFilePath;
private $localConfigFilePath;
public function setUp() public function setUp()
{ {
self::$testFileName1 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-config1.ini'; parent::setUp();
self::$testFileName2 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-config2.ini'; $this->testDirectory = $this->createTestDirectory();
$this->baseConfigFilePath = $this->testDirectory . DIRECTORY_SEPARATOR . 'config.ini';
$this->localConfigFilePath = $this->testDirectory . DIRECTORY_SEPARATOR . 'local.ini';
} }
public function testReadingNonSections() public function testReadingNonSections()
{ {
file_put_contents(self::$testFileName1, 'test=value'); file_put_contents($this->baseConfigFilePath, 'test=value');
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertEquals('value', $config->test); $this->assertEquals('value', $config->test);
} }
public function testReadingUnnestedSections() public function testReadingUnnestedSections()
{ {
file_put_contents(self::$testFileName1, '[test]' . PHP_EOL . 'key=value'); file_put_contents($this->baseConfigFilePath, '[test]' . PHP_EOL . 'key=value');
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertEquals('value', $config->test->key); $this->assertEquals('value', $config->test->key);
} }
public function testReadingNestedSections() public function testReadingNestedSections()
{ {
file_put_contents(self::$testFileName1, '[test.subtest]' . PHP_EOL . 'key=value'); file_put_contents($this->baseConfigFilePath, '[test.subtest]' . PHP_EOL . 'key=value');
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertEquals('value', $config->test->subtest->key); $this->assertEquals('value', $config->test->subtest->key);
} }
public function testReadingMultipleNestedSections() public function testReadingMultipleNestedSections()
{ {
file_put_contents( file_put_contents(
self::$testFileName1, $this->baseConfigFilePath,
'[test.subtest]' . PHP_EOL . 'key=value' . PHP_EOL . '[test.subtest]' . PHP_EOL . 'key=value' . PHP_EOL .
'[test.subtest.deeptest]' . PHP_EOL . 'key=zombie'); '[test.subtest.deeptest]' . PHP_EOL . 'key=zombie');
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertEquals('value', $config->test->subtest->key); $this->assertEquals('value', $config->test->subtest->key);
$this->assertEquals('zombie', $config->test->subtest->deeptest->key); $this->assertEquals('zombie', $config->test->subtest->deeptest->key);
} }
public function testReadingNonExistentFiles() public function testReadingNonExistentFiles()
{ {
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertEquals(0, count((array) $config)); $this->assertEquals(0, count(iterator_to_array($config->getIterator())));
} }
public function testMultipleFiles() public function testMultipleFiles()
{ {
file_put_contents(self::$testFileName1, 'test=trash'); file_put_contents($this->baseConfigFilePath, 'test=trash');
file_put_contents(self::$testFileName2, 'test=overridden'); file_put_contents($this->localConfigFilePath, 'test=overridden');
$config = new \Szurubooru\Config([self::$testFileName1, self::$testFileName2]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertEquals('overridden', $config->test); $this->assertEquals('overridden', $config->test);
} }
public function testReadingUnexistingProperties() public function testReadingUnexistingProperties()
{ {
file_put_contents(self::$testFileName1, 'meh=value'); file_put_contents($this->baseConfigFilePath, 'meh=value');
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$this->assertNull($config->unexistingSection); $this->assertNull($config->unexistingSection);
} }
public function testOverwritingValues() public function testOverwritingValues()
{ {
file_put_contents(self::$testFileName1, 'meh=value'); file_put_contents($this->baseConfigFilePath, 'meh=value');
$config = new \Szurubooru\Config([self::$testFileName1]); $config = new \Szurubooru\Config($this->testDirectory);
$config->newKey = 'fast'; $config->newKey = 'fast';
$this->assertEquals('fast', $config->newKey); $this->assertEquals('fast', $config->newKey);
} }
protected function tearDown()
{
foreach ([self::$testFileName1, self::$testFileName2] as $temporaryFileName)
{
if (file_exists($temporaryFileName))
unlink($temporaryFileName);
}
}
} }

View file

@ -18,13 +18,11 @@ class PrivilegeTest extends \Szurubooru\Tests\AbstractTestCase
$refl = new \ReflectionClass(\Szurubooru\Privilege::class); $refl = new \ReflectionClass(\Szurubooru\Privilege::class);
$constants = array_values($refl->getConstants()); $constants = array_values($refl->getConstants());
$configPath = __DIR__ $dataPath = __DIR__
. DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'data';
. DIRECTORY_SEPARATOR . 'config.ini';
$config = new \Szurubooru\Config(); $config = new \Szurubooru\Config($dataPath);
$config->loadFromIni($configPath);
foreach ($config->security->privileges as $key => $value) foreach ($config->security->privileges as $key => $value)
{ {
$this->assertTrue(in_array($key, $constants), "$key not in constants"); $this->assertTrue(in_array($key, $constants), "$key not in constants");

View file

@ -6,8 +6,9 @@ class FileServiceTest extends \Szurubooru\Tests\AbstractTestCase
public function testSaving() public function testSaving()
{ {
$testDirectory = $this->createTestDirectory(); $testDirectory = $this->createTestDirectory();
$configMock = $this->mockConfig($testDirectory);
$httpHelper = $this->mock( \Szurubooru\Helpers\HttpHelper::class); $httpHelper = $this->mock( \Szurubooru\Helpers\HttpHelper::class);
$fileService = new \Szurubooru\Services\FileService($testDirectory, $httpHelper); $fileService = new \Szurubooru\Services\FileService($configMock, $httpHelper);
$input = 'data:text/plain,YXdlc29tZSBkb2c='; $input = 'data:text/plain,YXdlc29tZSBkb2c=';
$fileService->saveFromBase64($input, 'dog.txt'); $fileService->saveFromBase64($input, 'dog.txt');
$expected = 'awesome dog'; $expected = 'awesome dog';

View file

@ -15,8 +15,9 @@ class ThumbnailServiceTest extends \Szurubooru\Tests\AbstractTestCase
touch($tempDirectory . DS . 'thumbnails' . DS . '5x5' . DS . 'keep'); touch($tempDirectory . DS . 'thumbnails' . DS . '5x5' . DS . 'keep');
touch($tempDirectory . DS . 'thumbnails' . DS . '10x10' . DS . 'remove'); touch($tempDirectory . DS . 'thumbnails' . DS . '10x10' . DS . 'remove');
$configMock = $this->mockConfig($tempDirectory);
$httpHelperMock = $this->mock(\Szurubooru\Helpers\HttpHelper::class); $httpHelperMock = $this->mock(\Szurubooru\Helpers\HttpHelper::class);
$fileService = new \Szurubooru\Services\FileService($tempDirectory, $httpHelperMock); $fileService = new \Szurubooru\Services\FileService($configMock, $httpHelperMock);
$thumbnailGeneratorMock = $this->mock(\Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator::class); $thumbnailGeneratorMock = $this->mock(\Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator::class);
$thumbnailService = new \Szurubooru\Services\ThumbnailService($fileService, $thumbnailGeneratorMock); $thumbnailService = new \Szurubooru\Services\ThumbnailService($fileService, $thumbnailGeneratorMock);