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
{
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;
}
}

View file

@ -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;
}

View file

@ -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')),

View file

@ -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();

View file

@ -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()

View file

@ -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);
}
}
}

View file

@ -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");

View file

@ -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';

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 . '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);