From 63f842c0cf6542f5ff81fceacd39d7be1a27ea8f Mon Sep 17 00:00:00 2001 From: Jesper Dolieslager Date: Mon, 8 Oct 2012 20:27:45 +0200 Subject: [PATCH 1/6] Add unit tests and fix minor errors --- src/Factory.php | 32 ++++++--- test/FactoryTest.php | 92 +++++++++++++++++++++++++- test/Writer/TestAssets/DummyWriter.php | 23 +++++++ 3 files changed, 137 insertions(+), 10 deletions(-) create mode 100755 test/Writer/TestAssets/DummyWriter.php diff --git a/src/Factory.php b/src/Factory.php index 6c900dd..0407e8f 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -52,7 +52,7 @@ class Factory * @var array */ protected static $writerExtensions = array( - 'php' => 'PhpArray', + 'php' => 'php', 'ini' => 'ini', 'json' => 'json', 'xml' => 'xml', @@ -132,8 +132,9 @@ public static function fromFiles(array $files, $returnConfigObject = false) * * @param string $filename * @param array|Config $config - * * @return boolean TRUE on success | FALSE on failure + * @throws Exception\RuntimeException + * @throws Exception\InvalidArgumentException */ public static function toFile($filename, $config) { @@ -142,7 +143,7 @@ public static function toFile($filename, $config) (!is_object($config) && !is_array($config)) ) { throw new Exception\InvalidArgumentException( - __METHOD__." \$config should be an array or instance of Zend\Config\Config" + __METHOD__." \$config should be an array or instance of Zend\\Config\\Config" ); } @@ -161,29 +162,32 @@ public static function toFile($filename, $config) ); } - if(!isset(self::$extensionWriters[$extension])) { + if(!isset(self::$writerExtensions[$extension])) { throw new Exception\RuntimeException( - "Unsupported config file extension: .{$extension}" + "Unsupported config file extension: '.{$extension}' for writing." ); } - $writer = self::$extensionWriters[$extension]; + $writer = self::$writerExtensions[$extension]; if (($writer instanceOf Writer\AbstractWriter) === false) { $writer = self::getWriterPluginManager()->get($writer); - self::$extensionWriters[$extension] = $writer; + self::$writerExtensions[$extension] = $writer; } if (is_object($config)) { $config = $config->toArray(); } - return file_put_contents($filename, $writer->processConfig($config)); + $content = $writer->processConfig($config); + + return (bool) (file_put_contents($filename, $content) !== false); } /** * Set reader plugin manager * * @param ReaderPluginManager $readers + * @return void */ public static function setReaderPluginManager(ReaderPluginManager $readers) { @@ -207,6 +211,7 @@ public static function getReaderPluginManager() * Set writer plugin manager * * @param WriterPluginManager $writers + * @return void */ public static function setWriterPluginManager(WriterPluginManager $writers) { @@ -233,6 +238,7 @@ public static function getWriterPluginManager() * @param string $extension * @param string|Reader\ReaderInterface $reader * @throws Exception\InvalidArgumentException + * @return void */ public static function registerReader($extension, $reader) { @@ -250,6 +256,14 @@ public static function registerReader($extension, $reader) self::$extensions[$extension] = $reader; } + /** + * Set config writer for file extension + * + * @param string $extension + * @param string|Writer\AbstractWriter $writer + * @throw Exception\InvalidArgumentException + * @return void + */ public static function registerWriter($extension, $writer) { $extension = strtolower($extension); @@ -263,6 +277,6 @@ public static function registerWriter($extension, $writer) )); } - self::$extensions[$extension] = $writer; + self::$writerExtensions[$extension] = $writer; } } diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 3f5d724..3f72b74 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -20,6 +20,28 @@ */ class FactoryTest extends \PHPUnit_Framework_TestCase { + protected $tmpFiles = array(); + + protected function getTestAssetFileName($ext) + { + if (empty($this->tmpfiles[$ext])) { + $this->tmpfiles[$ext] = tempnam(sys_get_temp_dir(), 'zend-config-writer').'.'.$ext; + } + return $this->tmpfiles[$ext]; + } + + public function tearDown() + { + foreach($this->tmpFiles as $file) { + if (file_exists($file)) { + if (!is_writable($file)) { + chmod($file, 0777); + } + @unlink($file); + } + } + } + public function testFromIni() { $config = Factory::fromFile(__DIR__ . '/TestAssets/Ini/include-base.ini'); @@ -125,7 +147,7 @@ public function testFactoryCanRegisterCustomReaderInstance() $this->assertEquals($configObject['one'], 1); } - public function testFactoryCanRegisterCustomReaderPlugn() + public function testFactoryCanRegisterCustomReaderPlugin() { $dummyReader = new Reader\TestAssets\DummyReader(); Factory::getReaderPluginManager()->setService('DummyReader', $dummyReader); @@ -138,5 +160,73 @@ public function testFactoryCanRegisterCustomReaderPlugn() $this->assertEquals($configObject['one'], 1); } + public function testFactoryToFileInvalidFileExtension() + { + $this->setExpectedException('RuntimeException'); + $result = Factory::toFile(__DIR__.'/TestAssets/bad.ext', array()); + } + + public function testFactoryToFileNoDirInHere() + { + $this->setExpectedException('RuntimeException'); + $result = Factory::toFile(__DIR__.'/TestAssets/NoDirInHere/nonExisiting/dummy.php', array()); + } + + public function testFactoryWriteToFile() + { + $config = array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')); + + $file = $this->getTestAssetFileName('php'); + $result = Factory::toFile($file, $config); + + // build string line by line as we are trailing-whitespace sensitive. + $expected = " 'foo',\n"; + $expected .= " 'bar' => \n"; + $expected .= " array (\n"; + $expected .= " 0 => 'baz',\n"; + $expected .= " 1 => 'foo',\n"; + $expected .= " ),\n"; + $expected .= ");\n"; + + $this->assertEquals(true, $result); + $this->assertEquals($expected, file_get_contents($file)); + } + + public function testFactoryToFileWrongConfig() + { + $this->setExpectedException('InvalidArgumentException'); + $result = Factory::toFile('test.ini', 'Im wrong'); + } + + public function testFactoryRegisterInvalidWriter() + { + $this->setExpectedException('InvalidArgumentException'); + Factory::registerWriter('dum', new Reader\TestAssets\DummyReader()); + } + + public function testFactoryCanRegisterCustomWriterInstance() + { + Factory::registerWriter('dum', new Writer\TestAssets\DummyWriter()); + + $file = $this->getTestAssetFileName('dum'); + $res = Factory::toFile($file, array('one' => 1)); + + $this->assertEquals($res, true); + } + + public function testFactoryCanRegisterCustomWriterPlugin() + { + $dummyWriter = new Writer\TestAssets\DummyWriter(); + Factory::getWriterPluginManager()->setService('DummyWriter', $dummyWriter); + + Factory::registerWriter('dum', 'DummyWriter'); + + $file = $this->getTestAssetFileName('dum'); + + $res = Factory::toFile($file, array('one' => 1)); + $this->assertEquals($res, true); + } } diff --git a/test/Writer/TestAssets/DummyWriter.php b/test/Writer/TestAssets/DummyWriter.php new file mode 100755 index 0000000..8bf8ba9 --- /dev/null +++ b/test/Writer/TestAssets/DummyWriter.php @@ -0,0 +1,23 @@ + Date: Mon, 8 Oct 2012 20:30:47 +0200 Subject: [PATCH 2/6] fix comment --- src/Factory.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 0407e8f..25986d2 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -128,14 +128,14 @@ public static function fromFiles(array $files, $returnConfigObject = false) } /** - * Writes a config to a file - * - * @param string $filename - * @param array|Config $config - * @return boolean TRUE on success | FALSE on failure - * @throws Exception\RuntimeException - * @throws Exception\InvalidArgumentException - */ + * Writes a config to a file + * + * @param string $filename + * @param array|Config $config + * @return boolean TRUE on success | FALSE on failure + * @throws Exception\RuntimeException + * @throws Exception\InvalidArgumentException + */ public static function toFile($filename, $config) { if ( From 2083d4285a585ecf5e1788eb9a56f6fa37b81c2b Mon Sep 17 00:00:00 2001 From: Jesper Dolieslager Date: Mon, 8 Oct 2012 22:04:39 +0200 Subject: [PATCH 3/6] fix eof --- src/WriterPluginManager.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/WriterPluginManager.php b/src/WriterPluginManager.php index 58598c5..35717f5 100755 --- a/src/WriterPluginManager.php +++ b/src/WriterPluginManager.php @@ -27,4 +27,3 @@ public function validatePlugin($plugin) ); } } - From bc87fcff205161c5eb20670307a8b3a579072e3c Mon Sep 17 00:00:00 2001 From: Jesper Dolieslager Date: Tue, 9 Oct 2012 07:55:12 +0200 Subject: [PATCH 4/6] eol in tests ;) --- test/Writer/TestAssets/DummyWriter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Writer/TestAssets/DummyWriter.php b/test/Writer/TestAssets/DummyWriter.php index 8bf8ba9..d5a989c 100755 --- a/test/Writer/TestAssets/DummyWriter.php +++ b/test/Writer/TestAssets/DummyWriter.php @@ -20,4 +20,3 @@ public function processConfig(array $config) return serialize($config); } } - From a1d355922c0eb28574d23499b63edc16340107e6 Mon Sep 17 00:00:00 2001 From: Erik Landvall Date: Tue, 15 Jan 2013 20:03:24 +0100 Subject: [PATCH 5/6] Restoring the error handler Restoring the error handler no matter what the exception is instead --- src/Writer/AbstractWriter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index ab02f1a..a718a76 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -56,7 +56,7 @@ function ($error, $message = '', $file = '', $line = 0) use ($filename) { { file_put_contents($filename, $this->toString($config), $flags); } - catch( Exception\RuntimeException $e ) + catch( \Exception $e ) { restore_error_handler(); throw $e; From fe929276c31b61b900fae855662cccba3a35193e Mon Sep 17 00:00:00 2001 From: Erik Landvall Date: Tue, 15 Jan 2013 23:35:17 +0100 Subject: [PATCH 6/6] Update library/Zend/Config/Writer/AbstractWriter.php Syntax changes --- src/Writer/AbstractWriter.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index a718a76..be48c21 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -52,12 +52,9 @@ function ($error, $message = '', $file = '', $line = 0) use ($filename) { }, E_WARNING ); - try - { + try { file_put_contents($filename, $this->toString($config), $flags); - } - catch( \Exception $e ) - { + } catch( \Exception $e ) { restore_error_handler(); throw $e; }