diff --git a/src/Config.php b/src/Config.php index 1ad7b19..70fa03a 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,36 +1,30 @@ data; + /** @var self $value */ foreach ($data as $key => $value) { if ($value instanceof self) { $array[$key] = $value->toArray(); @@ -200,6 +193,7 @@ public function __isset($name) * * @param string $name * @return void + * @throws Exception\InvalidArgumentException */ public function __unset($name) { @@ -342,11 +336,12 @@ public function offsetUnset($offset) * - Items in $merge with INTEGER keys will be appended. * - Items in $merge with STRING keys will overwrite current values. * - * @param Config $replace + * @param Config $merge * @return Config */ public function merge(self $merge) { + /** @var Config $value */ foreach ($merge as $key => $value) { if (array_key_exists($key, $this->data)) { if (is_int($key)) { @@ -384,6 +379,7 @@ public function setReadOnly() { $this->allowModifications = false; + /** @var Config $value */ foreach ($this->data as $value) { if ($value instanceof self) { $value->setReadOnly(); diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index a167476..81b0132 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -1,21 +1,11 @@ 'Ini', - 'xml' => 'Xml' + protected static $extensions = array( + 'ini' => 'ini', + 'json' => 'json', + 'xml' => 'xml', + 'yaml' => 'yaml', ); + /** * Read a config from a file. * * @param string $filename * @param boolean $returnConfigObject * @return array|Config + * @throws Exception\InvalidArgumentException + * @throws Exception\RuntimeException */ public static function fromFile($filename, $returnConfigObject = false) { @@ -64,17 +63,22 @@ public static function fromFile($filename, $returnConfigObject = false) if ($extension === 'php') { if (!is_file($filename) || !is_readable($filename)) { - throw new Exception\RuntimeException(sprintf('Filename "%s" is either not a file or not readable', $filename)); + throw new Exception\RuntimeException(sprintf( + "File '%s' doesn't exist or not readable", + $filename + )); } $config = include $filename; - } elseif (isset(self::$readers[$extension])) { - if (is_string(self::$readers[$extension])) { - $classname = __NAMESPACE__ . '\\Reader\\' . self::$readers[$extension]; - self::$readers[$extension] = new $classname(); + } elseif (isset(self::$extensions[$extension])) { + $reader = self::$extensions[$extension]; + if (!$reader instanceof Reader\ReaderInterface) { + $reader = self::getReaderPluginManager()->get($reader); + self::$extensions[$extension] = $reader; } - - $config = self::$readers[$extension]->fromFile($filename); + + /** @var Reader\ReaderInterface $reader */ + $config = $reader->fromFile($filename); } else { throw new Exception\RuntimeException(sprintf( 'Unsupported config file extension: .%s', @@ -102,4 +106,50 @@ public static function fromFiles(array $files, $returnConfigObject = false) return ($returnConfigObject) ? new Config($config) : $config; } + + /** + * Set reader plugin manager + * + * @param ReaderPluginManager $readers + */ + public static function setReaderPluginManager(ReaderPluginManager $readers) + { + self::$readers = $readers; + } + + /** + * Get the reader plugin manager + * + * @return ReaderPluginManager + */ + public static function getReaderPluginManager() + { + if (static::$readers === null) { + static::$readers = new ReaderPluginManager(); + } + return static::$readers; + } + + /** + * Set config reader for file extension + * + * @param string $extension + * @param string|Reader\ReaderInterface $reader + * @throws Exception\InvalidArgumentException + */ + public static function registerReader($extension, $reader) + { + $extension = strtolower($extension); + + if (!is_string($reader) && !$reader instanceof Reader\ReaderInterface) { + throw new Exception\InvalidArgumentException(sprintf( + 'Reader should be plugin name, class name or ' . + 'instance of %s\Reader\ReaderInterface; recieved "%s"', + __NAMESPACE__, + (is_object($reader) ? get_class($reader) : gettype($reader)) + )); + } + + self::$extensions[$extension] = $reader; + } } diff --git a/src/Processor/Constant.php b/src/Processor/Constant.php index 4bd966e..9fbae8d 100644 --- a/src/Processor/Constant.php +++ b/src/Processor/Constant.php @@ -1,35 +1,19 @@ userOnly = $userOnly; + $this->userOnly = (bool) $userOnly; + return $this; } /** @@ -101,5 +86,4 @@ public function getTokens() { return $this->tokens; } - } diff --git a/src/Processor/Filter.php b/src/Processor/Filter.php index d58c7c8..e8154da 100644 --- a/src/Processor/Filter.php +++ b/src/Processor/Filter.php @@ -1,21 +1,11 @@ filter; + $this->filter = $filter; + return $this; } /** - * @param ZendFilter $filter + * @return ZendFilter */ - public function setFilter(ZendFilter $filter) + public function getFilter() { - $this->filter = $filter; + return $this->filter; } /** * Process * * @param Config $config - * @return Config + * @return Config + * @throws Exception\InvalidArgumentException */ public function process(Config $config) { if ($config->isReadOnly()) { - throw new Exception\InvalidArgumentException('Cannot parse config because it is read-only'); + throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); } /** @@ -94,7 +84,7 @@ public function process(Config $config) /** * Process a single value * - * @param $value + * @param mixed $value * @return mixed */ public function processValue($value) diff --git a/src/Processor/ProcessorInterface.php b/src/Processor/ProcessorInterface.php index 0994ba2..520a18b 100644 --- a/src/Processor/ProcessorInterface.php +++ b/src/Processor/ProcessorInterface.php @@ -1,21 +1,11 @@ isReadOnly()) { - throw new InvalidArgumentException('Cannot parse config because it is read-only'); + throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); } foreach ($this as $parser) { - /** @var $parser \Zend\Config\Processor\ProcessorInterface */ + /** @var $parser ProcessorInterface */ $parser->process($config); } } @@ -53,13 +43,13 @@ public function process(Config $config) /** * Process a single value * - * @param $value + * @param mixed $value * @return mixed */ public function processValue($value) { foreach ($this as $parser) { - /** @var $parser \Zend\Config\Processor\ProcessorInterface */ + /** @var $parser ProcessorInterface */ $value = $parser->processValue($value); } diff --git a/src/Processor/Token.php b/src/Processor/Token.php index 286dad2..cf725ba 100644 --- a/src/Processor/Token.php +++ b/src/Processor/Token.php @@ -1,21 +1,11 @@ value - * to replace it with - * @param string $prefix - * @param string $suffix + * @param array|Config|Traversable $tokens Associative array of TOKEN => value + * to replace it with + * @param string $prefix + * @param string $suffix * @internal param array $options - * @return \Zend\Config\Processor\Token + * @return Token */ public function __construct($tokens = array(), $prefix = '', $suffix = '') { @@ -80,51 +68,53 @@ public function __construct($tokens = array(), $prefix = '', $suffix = '') } /** - * @return string + * @param string $prefix + * @return Token */ - public function getPrefix() + public function setPrefix($prefix) { - return $this->prefix; + // reset map + $this->map = null; + $this->prefix = $prefix; + return $this; } /** * @return string */ - public function getSuffix() + public function getPrefix() { - return $this->suffix; + return $this->prefix; } /** - * @param string $prefix - * @return mixed + * @param string $suffix + * @return Token */ - public function setPrefix($prefix) + public function setSuffix($suffix) { // reset map $this->map = null; + $this->suffix = $suffix; - return $this->prefix = $prefix; + return $this; } /** - * @param string $suffix * @return string */ - public function setSuffix($suffix) + public function getSuffix() { - // reset map - $this->map = null; - - return $this->suffix = $suffix; + return $this->suffix; } /** * Set token registry. * - * @param array|\Zend\Config\Config|\ArrayObject|\Traversable $tokens Associative array of TOKEN => value - * to replace it with - * @throws \Zend\Config\Exception\InvalidArgumentException + * @param array|Config|Traversable $tokens Associative array of TOKEN => value + * to replace it with + * @return Token + * @throws Exception\InvalidArgumentException */ public function setTokens($tokens) { @@ -132,7 +122,7 @@ public function setTokens($tokens) $this->tokens = $tokens; } elseif ($tokens instanceof Config) { $this->tokens = $tokens->toArray(); - } elseif ($tokens instanceof \Traversable) { + } elseif ($tokens instanceof Traversable) { $this->tokens = array(); foreach ($tokens as $key => $val) { $this->tokens[$key] = $val; @@ -143,10 +133,13 @@ public function setTokens($tokens) // reset map $this->map = null; + + return $this; } /** * Get current token registry. + * * @return array */ public function getTokens() @@ -157,9 +150,10 @@ public function getTokens() /** * Add new token. * - * @param $token - * @param $value - * @throws \Zend\Config\Exception\InvalidArgumentException + * @param string $token + * @param mixed $value + * @return Token + * @throws Exception\InvalidArgumentException */ public function addToken($token, $value) { @@ -170,14 +164,16 @@ public function addToken($token, $value) // reset map $this->map = null; + + return $this; } /** * Add new token. * - * @param $token - * @param $value - * @throws \Zend\Config\Exception\InvalidArgumentException + * @param string $token + * @param mixed $value + * @return Token */ public function setToken($token, $value) { @@ -204,11 +200,12 @@ protected function buildMap() * * @param Config $config * @return Config + * @throws InvalidArgumentException */ public function process(Config $config) { if ($config->isReadOnly()) { - throw new Exception\InvalidArgumentException('Cannot parse config because it is read-only'); + throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); } if ($this->map === null) { @@ -244,6 +241,7 @@ public function processValue($value) } $keys = array_keys($this->map); $values = array_values($this->map); + return str_replace($keys, $values, $value); } } diff --git a/src/Processor/Translator.php b/src/Processor/Translator.php index ef15cca..bb2c623 100644 --- a/src/Processor/Translator.php +++ b/src/Processor/Translator.php @@ -1,37 +1,23 @@ setTranslator($translator); + $this->setTextDomain($textDomain); $this->setLocale($locale); } + /** + * @param ZendTranslator $translator + * @return Translator + */ + public function setTranslator(ZendTranslator $translator) + { + $this->translator = $translator; + return $this; + } + /** * @return ZendTranslator */ @@ -68,15 +70,17 @@ public function getTranslator() } /** - * @param ZendTranslator $translator + * @param string|null $locale + * @return Translator */ - public function setTranslator(ZendTranslator $translator) + public function setLocale($locale) { - $this->translator = $translator; + $this->locale = $locale; + return $this; } /** - * @return Locale|string|null + * @return string|null */ public function getLocale() { @@ -84,24 +88,34 @@ public function getLocale() } /** - * @param Locale|string|null $locale + * @param string $textDomain + * @return Translator */ - public function setLocale($locale) + public function setTextDomain($textDomain) { - $this->locale = $locale; + $this->textDomain = $textDomain; + return $this; + } + + /** + * @return string + */ + public function getTextDomain() + { + return $this->textDomain; } /** * Process * - * @param Config $config + * @param Config $config * @return Config - * @throws InvalidArgumentException + * @throws Exception\InvalidArgumentException */ public function process(Config $config) { if ($config->isReadOnly()) { - throw new InvalidArgumentException('Cannot parse config because it is read-only'); + throw new Exception\InvalidArgumentException('Cannot process config because it is read-only'); } /** @@ -111,7 +125,7 @@ public function process(Config $config) if ($val instanceof Config) { $this->process($val); } else { - $config->$key = $this->translator->translate($val, $this->locale); + $config->{$key} = $this->translator->translate($val, $this->textDomain, $this->locale); } } @@ -126,7 +140,7 @@ public function process(Config $config) */ public function processValue($value) { - return $this->translator->translate($value, $this->locale); + return $this->translator->translate($value, $this->textDomain, $this->locale); } } diff --git a/src/Reader/Ini.php b/src/Reader/Ini.php index 34d6412..b02df3b 100644 --- a/src/Reader/Ini.php +++ b/src/Reader/Ini.php @@ -1,22 +1,11 @@ directory = dirname($filename); set_error_handler( @@ -101,9 +93,9 @@ function($error, $message = '', $file = '', $line = 0) use ($filename) { /** * fromString(): defined by Reader interface. * - * @see ReaderInterface::fromString() * @param string $string - * @return array + * @return array|bool + * @throws Exception\RuntimeException */ public function fromString($string) { @@ -171,6 +163,7 @@ protected function processSection(array $section) * @param string $value * @param array $config * @return array + * @throws Exception\RuntimeException */ protected function processKey($key, $value, array &$config) { @@ -186,7 +179,9 @@ protected function processKey($key, $value, array &$config) $config[$pieces[0]] = array(); } } elseif (!is_array($config[$pieces[0]])) { - throw new Exception\RuntimeException(sprintf('Cannot create sub-key for "%s", as key already exists', $pieces[0])); + throw new Exception\RuntimeException(sprintf( + 'Cannot create sub-key for "%s", as key already exists', $pieces[0] + )); } $this->processKey($pieces[1], $value, $config[$pieces[0]]); diff --git a/src/Reader/Json.php b/src/Reader/Json.php index e9a2910..01a5f9e 100644 --- a/src/Reader/Json.php +++ b/src/Reader/Json.php @@ -1,37 +1,25 @@ directory = dirname($filename); try { $config = JsonFormat::decode(file_get_contents($filename), JsonFormat::TYPE_ARRAY); - } catch (\Zend\Json\Exception\RuntimeException $e) { + } catch (JsonException\RuntimeException $e) { throw new Exception\RuntimeException($e->getMessage()); } @@ -70,37 +63,42 @@ public function fromFile($filename) * * @see ReaderInterface::fromString() * @param string $string - * @return array + * @return array|bool + * @throws Exception\RuntimeException */ public function fromString($string) { if (empty($string)) { return array(); } + $this->directory = null; try { $config = JsonFormat::decode($string, JsonFormat::TYPE_ARRAY); - } catch (\Zend\Json\Exception\RuntimeException $e) { + } catch (JsonException\RuntimeException $e) { throw new Exception\RuntimeException($e->getMessage()); } return $this->process($config); } + /** * Process the array for @include * * @param array $data - * @return array + * @return array + * @throws Exception\RuntimeException */ - protected function process(array $data) { + protected function process(array $data) + { foreach ($data as $key => $value) { if (is_array($value)) { $data[$key] = $this->process($value); } - if (trim($key)==='@include') { + if (trim($key) === '@include') { if ($this->directory === null) { - throw new Exception\RuntimeException('Cannot process @include statement for a json string'); + throw new Exception\RuntimeException('Cannot process @include statement for a JSON string'); } $reader = clone $this; unset($data[$key]); diff --git a/src/Reader/ReaderInterface.php b/src/Reader/ReaderInterface.php index e10280c..4ecaf8d 100644 --- a/src/Reader/ReaderInterface.php +++ b/src/Reader/ReaderInterface.php @@ -1,30 +1,19 @@ reader = new XMLReader(); - $this->reader->open($filename, null, LIBXML_XINCLUDE); + $this->reader = new XMLReader(); + $this->reader->open($filename, null, LIBXML_XINCLUDE); $this->directory = dirname($filename); @@ -96,7 +89,8 @@ function($error, $message = '', $file = '', $line = 0) use ($filename) { * * @see ReaderInterface::fromString() * @param string $string - * @return array + * @return array|bool + * @throws Exception\RuntimeException */ public function fromString($string) { diff --git a/src/Reader/Yaml.php b/src/Reader/Yaml.php index 5f173a7..5c638cc 100644 --- a/src/Reader/Yaml.php +++ b/src/Reader/Yaml.php @@ -1,22 +1,11 @@ setYamlDecoder($yamlDecoder); } else { if (function_exists('yaml_parse')) { @@ -60,41 +50,52 @@ public function __construct($yamlDecoder=null) { } } } - /** - * Get callback for decoding YAML - * - * @return callable - */ - public function getYamlDecoder() - { - return $this->yamlDecoder; - } + /** * Set callback for decoding YAML * - * @param callable $yamlDecoder the decoder to set + * @param string|callable $yamlDecoder the decoder to set * @return Yaml + * @throws Exception\InvalidArgumentException */ public function setYamlDecoder($yamlDecoder) { if (!is_callable($yamlDecoder)) { - throw new Exception\InvalidArgumentException('Invalid parameter to setYamlDecoder() - must be callable'); + throw new Exception\RuntimeException( + 'Invalid parameter to setYamlDecoder() - must be callable' + ); } $this->yamlDecoder = $yamlDecoder; return $this; } + + /** + * Get callback for decoding YAML + * + * @return callable + */ + public function getYamlDecoder() + { + return $this->yamlDecoder; + } + /** * fromFile(): defined by Reader interface. * * @see ReaderInterface::fromFile() * @param string $filename * @return array + * @throws Exception\RuntimeException */ public function fromFile($filename) { - if (!file_exists($filename)) { - throw new Exception\RuntimeException("The file $filename doesn't exists."); + if (!is_file($filename) || !is_readable($filename)) { + throw new Exception\RuntimeException(sprintf( + "File '%s' doesn't exist or not readable", + $filename + )); } + if (null === $this->getYamlDecoder()) { throw new Exception\RuntimeException("You didn't specify a Yaml callback decoder"); } @@ -114,7 +115,8 @@ public function fromFile($filename) * * @see ReaderInterface::fromString() * @param string $string - * @return array + * @return array|bool + * @throws Exception\RuntimeException */ public function fromString($string) { @@ -134,18 +136,21 @@ public function fromString($string) return $this->process($config); } + /** * Process the array for @include - * + * * @param array $data - * @return array + * @return array + * @throws Exception\RuntimeException */ - protected function process(array $data) { + protected function process(array $data) + { foreach ($data as $key => $value) { if (is_array($value)) { $data[$key] = $this->process($value); } - if (trim($key)==='@include') { + if (trim($key) === '@include') { if ($this->directory === null) { throw new Exception\RuntimeException('Cannot process @include statement for a json string'); } diff --git a/src/ReaderPluginManager.php b/src/ReaderPluginManager.php new file mode 100644 index 0000000..008bd0d --- /dev/null +++ b/src/ReaderPluginManager.php @@ -0,0 +1,66 @@ + 'Zend\Config\Reader\Ini', + 'json' => 'Zend\Config\Reader\Json', + 'xml' => 'Zend\Config\Reader\Xml', + 'yaml' => 'Zend\Config\Reader\Yaml', + ); + + /** + * Validate the plugin + * Checks that the reader loaded is an instance of Reader\ReaderInterface. + * + * @param Reader\ReaderInterface $plugin + * @return void + * @throws Exception\InvalidArgumentException if invalid + */ + public function validatePlugin($plugin) + { + if ($plugin instanceof Reader\ReaderInterface) { + // we're okay + return; + } + + throw new Exception\InvalidArgumentException(sprintf( + 'Plugin of type %s is invalid; must implement %s\Reader\ReaderInterface', + (is_object($plugin) ? get_class($plugin) : gettype($plugin)), + __NAMESPACE__ + )); + } +} diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index 09726d7..1d6df80 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -1,21 +1,11 @@ toString($config), $exclusiveLock); + file_put_contents($filename, $this->toString($config), $flags); restore_error_handler(); } @@ -73,7 +61,7 @@ function($error, $message = '', $file = '', $line = 0) use ($filename) { * * @see WriterInterface::toString() * @param mixed $config - * @return void + * @return string * @throws Exception\InvalidArgumentException */ public function toString($config) @@ -88,8 +76,7 @@ public function toString($config) } /** - * Process an array configuration. - * + * @param array $config * @return string */ abstract protected function processConfig(array $config); diff --git a/src/Writer/Ini.php b/src/Writer/Ini.php index 164097f..cae2dc0 100644 --- a/src/Writer/Ini.php +++ b/src/Writer/Ini.php @@ -1,21 +1,11 @@ setYamlEncoder($yamlEncoder); } else { - if (function_exists('yaml_parse')) { - $this->setYamlEncoder('yaml_parse'); + if (function_exists('yaml_emit')) { + $this->setYamlEncoder('yaml_emit'); } } } + /** * Get callback for decoding YAML * @@ -59,11 +51,13 @@ public function getYamlEncoder() { return $this->yamlEncoder; } + /** * Set callback for decoding YAML * * @param callable $yamlEncoder the decoder to set * @return Yaml + * @throws Exception\InvalidArgumentException */ public function setYamlEncoder($yamlEncoder) { @@ -73,11 +67,13 @@ public function setYamlEncoder($yamlEncoder) $this->yamlEncoder = $yamlEncoder; return $this; } + /** * processConfig(): defined by AbstractWriter. * * @param array $config * @return string + * @throws Exception\RuntimeException */ public function processConfig(array $config) { diff --git a/test/ConfigTest.php b/test/ConfigTest.php index c67efc2..b1fe550 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -21,7 +21,7 @@ namespace ZendTest\Config; -use \Zend\Config\Config; +use Zend\Config\Config; /** * @category Zend diff --git a/test/FactoryTest.php b/test/FactoryTest.php index d7de536..29f91f9 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -21,7 +21,7 @@ namespace ZendTest\Config; -use \Zend\Config\Factory; +use Zend\Config\Factory; /** * @category Zend @@ -33,9 +33,6 @@ */ class FactoryTest extends \PHPUnit_Framework_TestCase { - protected $_iniFileConfig; - protected $_iniFileNested; - public function testFromIni() { $config = Factory::fromFile(__DIR__ . '/TestAssets/Ini/include-base.ini'); @@ -56,8 +53,8 @@ public function testFromIniFiles() __DIR__ . '/TestAssets/Ini/include-base.ini', __DIR__ . '/TestAssets/Ini/include-base2.ini' ); - $config = Factory::fromFiles($files); + $this->assertEquals('bar', $config['base']['foo']); $this->assertEquals('baz', $config['test']['bar']); } @@ -68,8 +65,8 @@ public function testFromXmlFiles() __DIR__ . '/TestAssets/Xml/include-base.xml', __DIR__ . '/TestAssets/Xml/include-base2.xml' ); - $config = Factory::fromFiles($files); + $this->assertEquals('bar', $config['base']['foo']); $this->assertEquals('baz', $config['test']['bar']); } @@ -80,8 +77,8 @@ public function testFromPhpFiles() __DIR__ . '/TestAssets/Php/include-base.php', __DIR__ . '/TestAssets/Php/include-base2.php' ); - $config = Factory::fromFiles($files); + $this->assertEquals('bar', $config['base']['foo']); $this->assertEquals('baz', $config['test']['bar']); } @@ -93,8 +90,8 @@ public function testFromIniAndXmlAndPhpFiles() __DIR__ . '/TestAssets/Xml/include-base2.xml', __DIR__ . '/TestAssets/Php/include-base3.php', ); - $config = Factory::fromFiles($files); + $this->assertEquals('bar', $config['base']['foo']); $this->assertEquals('baz', $config['test']['bar']); $this->assertEquals('baz', $config['last']['bar']); @@ -125,10 +122,35 @@ public function testNonExistentFileThrowsRuntimeException() $config = Factory::fromFile('foo.bar'); } - public function testInvalidFileExtensionThrowsInvalidArgumentException() + public function testUnsupportedFileExtensionThrowsRuntimeException() { $this->setExpectedException('RuntimeException'); $config = Factory::fromFile(__DIR__ . '/TestAssets/bad.ext'); } + + public function testFactoryCanRegisterCustomReaderInstance() + { + Factory::registerReader('dum', new Reader\TestAssets\DummyReader()); + + $configObject = Factory::fromFile(__DIR__ . '/TestAssets/dummy.dum', true); + $this->assertInstanceOf('Zend\Config\Config', $configObject); + + $this->assertEquals($configObject['one'], 1); + } + + public function testFactoryCanRegisterCustomReaderPlugn() + { + $dummyReader = new Reader\TestAssets\DummyReader(); + Factory::getReaderPluginManager()->setService('DummyReader',$dummyReader); + + Factory::registerReader('dum', 'DummyReader'); + + $configObject = Factory::fromFile(__DIR__ . '/TestAssets/dummy.dum', true); + $this->assertInstanceOf('Zend\Config\Config', $configObject); + + $this->assertEquals($configObject['one'], 1); + } + + } diff --git a/test/ProcessorTest.php b/test/ProcessorTest.php index 8dd1e15..196ed07 100644 --- a/test/ProcessorTest.php +++ b/test/ProcessorTest.php @@ -27,8 +27,8 @@ use Zend\Config\Processor\Filter as FilterProcessor; use Zend\Config\Processor\Constant as ConstantProcessor; use Zend\Config\Processor\Queue as Queue; -use Zend\Translator\Translator; -use Zend\Translator\Adapter\ArrayAdapter; +use Zend\I18n\Translator\Translator; +use Zend\I18n\Translator\Loader\PhpArray; use Zend\Filter\StringToLower; use Zend\Filter\StringToUpper; use Zend\Filter\PregReplace; @@ -45,7 +45,7 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase { protected $nested; protected $tokenBare, $tokenPrefix, $tokenSuffix, $tokenSurround, $tokenSurroundMixed; - protected $translator, $translatorStrings; + protected $translatorData, $translatorFile; protected $userConstants, $phpConstants; protected $filter; @@ -116,7 +116,7 @@ public function setUp() ), ); - $this->translator = array( + $this->translatorData = array( 'pages' => array( array( 'id' => 'oneDog', @@ -131,10 +131,7 @@ public function setUp() ) ); - $this->translatorStrings = array( - 'one dog' => 'ein Hund', - 'two dogs' => 'zwei Hunde' - ); + $this->translatorFile = realpath(__DIR__ . '/_files/translations-de_DE.php'); $this->filter = array( 'simple' => 'some MixedCase VALue', @@ -143,11 +140,6 @@ public function setUp() ), ); - if (ArrayAdapter::hasCache()) { - ArrayAdapter::clearCache(); - ArrayAdapter::removeCache(); - } - $this->userConstants = array( 'simple' => 'SOME_USERLAND_CONSTANT', 'inside' => 'some text with SOME_USERLAND_CONSTANT inside', @@ -198,7 +190,8 @@ public function testBareTokenPost() public function testAddInvalidToken() { $processor = new TokenProcessor(); - $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot use ' . gettype(array()) . ' as token name.'); + $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', + 'Cannot use ' . gettype(array()) . ' as token name.'); $processor->addToken(array(), 'bar'); } @@ -217,7 +210,8 @@ public function testTokenReadOnly() $processor = new TokenProcessor(); $processor->addToken('BARETOKEN', 'some replaced value'); - $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only'); + $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', + 'Cannot process config because it is read-only'); $processor->process($config); } @@ -352,36 +346,39 @@ public function testPHPConstants() public function testTranslator() { - $config = new Config($this->translator, true); - $translator = new Translator(Translator::AN_ARRAY, $this->translatorStrings, 'de_DE'); - $processor = new TranslatorProcessor($translator); + $config = new Config($this->translatorData, true); + $translator = new Translator(); + $translator->addTranslationFile('phparray', $this->translatorFile); + $processor = new TranslatorProcessor($translator); $processor->process($config); $this->assertEquals('oneDog', $config->pages[0]->id); $this->assertEquals('ein Hund', $config->pages[0]->label); - $this->assertEquals('twoDogs', $config->pages[1]->id); $this->assertEquals('zwei Hunde', $config->pages[1]->label); } - + public function testTranslatorReadOnly() { - $config = new Config($this->translator, false); - $translator = new Translator(Translator::AN_ARRAY, $this->translatorStrings, 'de_DE'); - $processor = new TranslatorProcessor($translator); - $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only'); + $config = new Config($this->translatorData, false); + $translator = new Translator(); + $processor = new TranslatorProcessor($translator); + + $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', + 'Cannot process config because it is read-only'); $processor->process($config); } public function testTranslatorSingleValue() { - $translator = new Translator(Translator::AN_ARRAY, $this->translatorStrings, 'de_DE'); - $processor = new TranslatorProcessor($translator); - $word = 'one dog'; - $this->assertEquals('ein Hund', $processor->processValue($word)); + $translator = new Translator(); + $translator->addTranslationFile('phparray', $this->translatorFile); + $processor = new TranslatorProcessor($translator); + + $this->assertEquals('ein Hund', $processor->processValue('one dog')); } - + public function testFilter() { $config = new Config($this->filter, true); @@ -401,7 +398,8 @@ public function testFilterReadOnly() $filter = new StringToLower(); $processor = new FilterProcessor($filter); - $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only'); + $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', + 'Cannot process config because it is read-only'); $processor->process($config); } @@ -449,7 +447,8 @@ public function testQueueReadOnly() $queue = new Queue(); $queue->insert($lowerProcessor); - $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only'); + $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', + 'Cannot process config because it is read-only'); $queue->process($config); } @@ -471,6 +470,7 @@ public function testQueueSingleValue() $this->assertEquals('test', $queue->processValue($data)); } + /** * @depends testQueueFIFO */ diff --git a/test/Reader/AbstractReaderTestCase.php b/test/Reader/AbstractReaderTestCase.php index 1f4a99d..55644b8 100644 --- a/test/Reader/AbstractReaderTestCase.php +++ b/test/Reader/AbstractReaderTestCase.php @@ -21,9 +21,9 @@ namespace ZendTest\Config\Reader; -use \PHPUnit_Framework_TestCase as TestCase, - \Zend\Config\Reader\ReaderInterface, - \ReflectionClass; +use PHPUnit_Framework_TestCase as TestCase; +use Zend\Config\Reader\ReaderInterface; +use ReflectionClass; /** * @category Zend @@ -43,6 +43,7 @@ abstract class AbstractReaderTestCase extends TestCase /** * Get test asset name for current test case. * + * @param string $name * @return string */ abstract protected function getTestAssetPath($name); @@ -50,7 +51,7 @@ abstract protected function getTestAssetPath($name); public function testMissingFile() { $filename = $this->getTestAssetPath('no-file'); - $this->setExpectedException('Zend\Config\Exception\RuntimeException', "The file $filename doesn't exists."); + $this->setExpectedException('Zend\Config\Exception\RuntimeException', "doesn't exist or not readable"); $config = $this->reader->fromFile($filename); } diff --git a/test/Reader/IniTest.php b/test/Reader/IniTest.php index 82b101e..f0d5edb 100644 --- a/test/Reader/IniTest.php +++ b/test/Reader/IniTest.php @@ -21,7 +21,7 @@ namespace ZendTest\Config\Reader; -use \Zend\Config\Reader\Ini; +use Zend\Config\Reader\Ini; /** * @category Zend diff --git a/test/Reader/JsonTest.php b/test/Reader/JsonTest.php index b52bb2d..522cdb7 100644 --- a/test/Reader/JsonTest.php +++ b/test/Reader/JsonTest.php @@ -21,7 +21,7 @@ namespace ZendTest\Config\Reader; -use \Zend\Config\Reader\Json; +use Zend\Config\Reader\Json; /** * @category Zend diff --git a/test/Reader/TestAssets/DummyReader.php b/test/Reader/TestAssets/DummyReader.php new file mode 100644 index 0000000..504464a --- /dev/null +++ b/test/Reader/TestAssets/DummyReader.php @@ -0,0 +1,27 @@ +reader = new IniReader(); $this->writer = new IniWriter(); } - - public function testNoSection() { $config = new Config(array('test' => 'foo', 'test2' => array('test3' => 'bar'))); @@ -58,13 +55,12 @@ public function testNoSection() public function testWriteAndReadOriginalFile() { - $config = $this->reader->fromFile(__DIR__ . '/files/allsections.ini'); + $config = $this->reader->fromFile(__DIR__ . '/_files/allsections.ini'); $this->writer->toFile($this->getTestAssetFileName(), $config); $config = $this->reader->fromFile($this->getTestAssetFileName()); $this->assertEquals('multi', $config['all']['one']['two']['three']); - } } diff --git a/test/Writer/JsonTest.php b/test/Writer/JsonTest.php index 51a57a7..44656bc 100644 --- a/test/Writer/JsonTest.php +++ b/test/Writer/JsonTest.php @@ -21,9 +21,9 @@ namespace ZendTest\Config\Writer; -use \Zend\Config\Writer\Json as JsonWriter; -use \Zend\Config\Config; -use \Zend\Config\Reader\Json as JsonReader; +use Zend\Config\Writer\Json as JsonWriter; +use Zend\Config\Config; +use Zend\Config\Reader\Json as JsonReader; /** * @category Zend @@ -35,15 +35,12 @@ */ class JsonTest extends AbstractWriterTestCase { - public function setUp() { $this->reader = new JsonReader(); $this->writer = new JsonWriter(); } - - public function testNoSection() { $config = new Config(array('test' => 'foo', 'test2' => array('test3' => 'bar'))); @@ -58,7 +55,7 @@ public function testNoSection() public function testWriteAndReadOriginalFile() { - $config = $this->reader->fromFile(__DIR__ . '/files/allsections.json'); + $config = $this->reader->fromFile(__DIR__ . '/_files/allsections.json'); $this->writer->toFile($this->getTestAssetFileName(), $config); diff --git a/test/Writer/PhpArrayTest.php b/test/Writer/PhpArrayTest.php index 5077a5e..8caf8e3 100644 --- a/test/Writer/PhpArrayTest.php +++ b/test/Writer/PhpArrayTest.php @@ -21,9 +21,9 @@ namespace ZendTest\Config\Writer; -use \Zend\Config\Writer\PhpArray; -use \Zend\Config\Config; -use ZendTest\Config\Writer\files\PhpReader; +use Zend\Config\Writer\PhpArray; +use Zend\Config\Config; +use ZendTest\Config\Writer\TestAssets\PhpReader; /** * @category Zend diff --git a/test/Writer/TestAssets/PhpReader.php b/test/Writer/TestAssets/PhpReader.php new file mode 100644 index 0000000..ba7a527 --- /dev/null +++ b/test/Writer/TestAssets/PhpReader.php @@ -0,0 +1,11 @@ +reader->fromFile(__DIR__ . '/files/allsections.yaml'); + $config = $this->reader->fromFile(__DIR__ . '/_files/allsections.yaml'); $this->writer->toFile($this->getTestAssetFileName(), $config); diff --git a/test/Writer/files/allsections.ini b/test/Writer/_files/allsections.ini similarity index 93% rename from test/Writer/files/allsections.ini rename to test/Writer/_files/allsections.ini index d4eef2d..4eaa75b 100644 --- a/test/Writer/files/allsections.ini +++ b/test/Writer/_files/allsections.ini @@ -1,26 +1,26 @@ -[all] -hostname = all -name = thisname -db.host = 127.0.0.1 -db.user = username -db.pass = password -db.name = live -one.two.three = multi - -[staging] -hostname = staging -db.name = dbstaging -debug = false - -[debug] -hostname = debug -debug = true -values.changed = yes -db.name = dbdebug -special.no = no -special.null = null -special.false = false - -[other_staging] -only_in = otherStaging -db.pass = anotherpwd +[all] +hostname = all +name = thisname +db.host = 127.0.0.1 +db.user = username +db.pass = password +db.name = live +one.two.three = multi + +[staging] +hostname = staging +db.name = dbstaging +debug = false + +[debug] +hostname = debug +debug = true +values.changed = yes +db.name = dbdebug +special.no = no +special.null = null +special.false = false + +[other_staging] +only_in = otherStaging +db.pass = anotherpwd diff --git a/test/Writer/files/allsections.json b/test/Writer/_files/allsections.json similarity index 100% rename from test/Writer/files/allsections.json rename to test/Writer/_files/allsections.json diff --git a/test/Writer/files/allsections.xml b/test/Writer/_files/allsections.xml similarity index 100% rename from test/Writer/files/allsections.xml rename to test/Writer/_files/allsections.xml diff --git a/test/Writer/files/allsections.yaml b/test/Writer/_files/allsections.yaml similarity index 100% rename from test/Writer/files/allsections.yaml rename to test/Writer/_files/allsections.yaml diff --git a/test/Writer/files/PhpReader.php b/test/Writer/files/PhpReader.php deleted file mode 100644 index d7e91fe..0000000 --- a/test/Writer/files/PhpReader.php +++ /dev/null @@ -1,9 +0,0 @@ - 'ein Hund', + 'two dogs' => 'zwei Hunde' +);