diff --git a/src/Config.php b/src/Config.php index d01eac94..6d1c52f8 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,9 +3,21 @@ namespace Szurubooru; 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) { @@ -14,6 +26,11 @@ class Config extends \ArrayObject } } + public function getDataDirectory() + { + return $this->dataDirectory; + } + public function offsetGet($index) { if (!parent::offsetExists($index)) @@ -29,7 +46,7 @@ class Config extends \ArrayObject { if (!is_array($value)) { - $this->$key = $value; + $this->offsetSet($key, $value); } else { @@ -39,7 +56,7 @@ class Config extends \ArrayObject foreach (explode('.', $section) as $subSection) { if (!$ptr->offsetExists($subSection)) - $ptr->offsetSet($subSection, new self()); + $ptr->offsetSet($subSection, new \ArrayObject([], $this->getArrayObjectFlags())); $ptr = $ptr->$subSection; } @@ -49,4 +66,9 @@ class Config extends \ArrayObject } } } + + private function getArrayObjectFlags() + { + return parent::ARRAY_AS_PROPS | parent::STD_PROP_LIST; + } } diff --git a/src/Services/FileService.php b/src/Services/FileService.php index 88bd58bc..3094a238 100644 --- a/src/Services/FileService.php +++ b/src/Services/FileService.php @@ -6,9 +6,9 @@ class FileService private $dataDirectory; 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; } diff --git a/src/di.php b/src/di.php index 9ad5558c..ec472a82 100644 --- a/src/di.php +++ b/src/di.php @@ -2,11 +2,7 @@ $dataDirectory = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data'; return [ - \Szurubooru\Config::class => DI\object()->constructor([ - $dataDirectory . DIRECTORY_SEPARATOR . 'config.ini', - $dataDirectory . DIRECTORY_SEPARATOR . 'local.ini']), - - \Szurubooru\Services\FileService::class => DI\object()->constructor($dataDirectory), + \Szurubooru\Config::class => DI\object()->constructor($dataDirectory), \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), diff --git a/tests/AbstractDatabaseTestCase.php b/tests/AbstractDatabaseTestCase.php index 2a98958f..4da39ef0 100644 --- a/tests/AbstractDatabaseTestCase.php +++ b/tests/AbstractDatabaseTestCase.php @@ -11,11 +11,10 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa $host = 'localhost'; $port = 27017; $database = 'test'; - $config = new \Szurubooru\Config(); - $config->database = new \StdClass; - $config->database->host = 'localhost'; - $config->database->port = 27017; - $config->database->name = 'test'; + $config = $this->mockConfig(); + $config->set('database/host', 'localhost'); + $config->set('database/port', '27017'); + $config->set('database/name', 'test'); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config); $this->upgradeService = new \Szurubooru\UpgradeService($this->databaseConnection); $this->upgradeService->prepareForUsage(); diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index c9f9cadd..e15f236b 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -8,9 +8,9 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock(); } - public function mockConfig() + public function mockConfig($path = null) { - return new ConfigMock(); + return new ConfigMock($path); } public function createTestDirectory() diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index c29759d8..111b92b0 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -3,83 +3,76 @@ namespace Szurubooru\Tests; final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase { - private static $testFileName1; - private static $testFileName2; + private $testDirectory; + private $baseConfigFilePath; + private $localConfigFilePath; public function setUp() { - self::$testFileName1 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-config1.ini'; - self::$testFileName2 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test-config2.ini'; + parent::setUp(); + $this->testDirectory = $this->createTestDirectory(); + $this->baseConfigFilePath = $this->testDirectory . DIRECTORY_SEPARATOR . 'config.ini'; + $this->localConfigFilePath = $this->testDirectory . DIRECTORY_SEPARATOR . 'local.ini'; } public function testReadingNonSections() { - file_put_contents(self::$testFileName1, 'test=value'); - $config = new \Szurubooru\Config([self::$testFileName1]); + file_put_contents($this->baseConfigFilePath, 'test=value'); + $config = new \Szurubooru\Config($this->testDirectory); $this->assertEquals('value', $config->test); } public function testReadingUnnestedSections() { - file_put_contents(self::$testFileName1, '[test]' . PHP_EOL . 'key=value'); - $config = new \Szurubooru\Config([self::$testFileName1]); + file_put_contents($this->baseConfigFilePath, '[test]' . PHP_EOL . 'key=value'); + $config = new \Szurubooru\Config($this->testDirectory); $this->assertEquals('value', $config->test->key); } public function testReadingNestedSections() { - file_put_contents(self::$testFileName1, '[test.subtest]' . PHP_EOL . 'key=value'); - $config = new \Szurubooru\Config([self::$testFileName1]); + file_put_contents($this->baseConfigFilePath, '[test.subtest]' . PHP_EOL . 'key=value'); + $config = new \Szurubooru\Config($this->testDirectory); $this->assertEquals('value', $config->test->subtest->key); } public function testReadingMultipleNestedSections() { file_put_contents( - self::$testFileName1, + $this->baseConfigFilePath, '[test.subtest]' . PHP_EOL . 'key=value' . PHP_EOL . '[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('zombie', $config->test->subtest->deeptest->key); } public function testReadingNonExistentFiles() { - $config = new \Szurubooru\Config([self::$testFileName1]); - $this->assertEquals(0, count((array) $config)); + $config = new \Szurubooru\Config($this->testDirectory); + $this->assertEquals(0, count(iterator_to_array($config->getIterator()))); } public function testMultipleFiles() { - file_put_contents(self::$testFileName1, 'test=trash'); - file_put_contents(self::$testFileName2, 'test=overridden'); - $config = new \Szurubooru\Config([self::$testFileName1, self::$testFileName2]); + file_put_contents($this->baseConfigFilePath, 'test=trash'); + file_put_contents($this->localConfigFilePath, 'test=overridden'); + $config = new \Szurubooru\Config($this->testDirectory); $this->assertEquals('overridden', $config->test); } public function testReadingUnexistingProperties() { - file_put_contents(self::$testFileName1, 'meh=value'); - $config = new \Szurubooru\Config([self::$testFileName1]); + file_put_contents($this->baseConfigFilePath, 'meh=value'); + $config = new \Szurubooru\Config($this->testDirectory); $this->assertNull($config->unexistingSection); } public function testOverwritingValues() { - file_put_contents(self::$testFileName1, 'meh=value'); - $config = new \Szurubooru\Config([self::$testFileName1]); + file_put_contents($this->baseConfigFilePath, 'meh=value'); + $config = new \Szurubooru\Config($this->testDirectory); $config->newKey = 'fast'; $this->assertEquals('fast', $config->newKey); } - - protected function tearDown() - { - foreach ([self::$testFileName1, self::$testFileName2] as $temporaryFileName) - { - if (file_exists($temporaryFileName)) - unlink($temporaryFileName); - } - } - } diff --git a/tests/PrivilegeTest.php b/tests/PrivilegeTest.php index 2089e91f..a8520d47 100644 --- a/tests/PrivilegeTest.php +++ b/tests/PrivilegeTest.php @@ -18,13 +18,11 @@ class PrivilegeTest extends \Szurubooru\Tests\AbstractTestCase $refl = new \ReflectionClass(\Szurubooru\Privilege::class); $constants = array_values($refl->getConstants()); - $configPath = __DIR__ + $dataPath = __DIR__ . DIRECTORY_SEPARATOR . '..' - . DIRECTORY_SEPARATOR . 'data' - . DIRECTORY_SEPARATOR . 'config.ini'; + . DIRECTORY_SEPARATOR . 'data'; - $config = new \Szurubooru\Config(); - $config->loadFromIni($configPath); + $config = new \Szurubooru\Config($dataPath); foreach ($config->security->privileges as $key => $value) { $this->assertTrue(in_array($key, $constants), "$key not in constants"); diff --git a/tests/Services/FileServiceTest.php b/tests/Services/FileServiceTest.php index 94b5edb8..de74469a 100644 --- a/tests/Services/FileServiceTest.php +++ b/tests/Services/FileServiceTest.php @@ -6,8 +6,9 @@ class FileServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testSaving() { $testDirectory = $this->createTestDirectory(); + $configMock = $this->mockConfig($testDirectory); $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='; $fileService->saveFromBase64($input, 'dog.txt'); $expected = 'awesome dog'; diff --git a/tests/Services/ThumbnailServiceTest.php b/tests/Services/ThumbnailServiceTest.php index a0d0c93a..06d7662a 100644 --- a/tests/Services/ThumbnailServiceTest.php +++ b/tests/Services/ThumbnailServiceTest.php @@ -15,8 +15,9 @@ class ThumbnailServiceTest extends \Szurubooru\Tests\AbstractTestCase touch($tempDirectory . DS . 'thumbnails' . DS . '5x5' . DS . 'keep'); touch($tempDirectory . DS . 'thumbnails' . DS . '10x10' . DS . 'remove'); + $configMock = $this->mockConfig($tempDirectory); $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); $thumbnailService = new \Szurubooru\Services\ThumbnailService($fileService, $thumbnailGeneratorMock);