/tests/ZendTest/Config/FactoryTest.php

https://github.com/telkins/zf2 · PHP · 250 lines · 188 code · 51 blank · 11 comment · 3 complexity · 3cfb3e77b447c8f3e466a5efc100bbe3 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Config;
  10. use Zend\Config\Factory;
  11. /**
  12. * @group Zend_Config
  13. */
  14. class FactoryTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $tmpFiles = array();
  17. protected $originalIncludePath;
  18. protected function getTestAssetFileName($ext)
  19. {
  20. if (empty($this->tmpfiles[$ext])) {
  21. $this->tmpfiles[$ext] = tempnam(sys_get_temp_dir(), 'zend-config-writer').'.'.$ext;
  22. }
  23. return $this->tmpfiles[$ext];
  24. }
  25. public function setUp()
  26. {
  27. $this->originalIncludePath = get_include_path();
  28. set_include_path(__DIR__ . '/TestAssets');
  29. }
  30. public function tearDown()
  31. {
  32. set_include_path($this->originalIncludePath);
  33. foreach ($this->tmpFiles as $file) {
  34. if (file_exists($file)) {
  35. if (!is_writable($file)) {
  36. chmod($file, 0777);
  37. }
  38. @unlink($file);
  39. }
  40. }
  41. }
  42. public function testFromIni()
  43. {
  44. $config = Factory::fromFile(__DIR__ . '/TestAssets/Ini/include-base.ini');
  45. $this->assertEquals('bar', $config['base']['foo']);
  46. }
  47. public function testFromXml()
  48. {
  49. $config = Factory::fromFile(__DIR__ . '/TestAssets/Xml/include-base.xml');
  50. $this->assertEquals('bar', $config['base']['foo']);
  51. }
  52. public function testFromIniFiles()
  53. {
  54. $files = array(
  55. __DIR__ . '/TestAssets/Ini/include-base.ini',
  56. __DIR__ . '/TestAssets/Ini/include-base2.ini'
  57. );
  58. $config = Factory::fromFiles($files);
  59. $this->assertEquals('bar', $config['base']['foo']);
  60. $this->assertEquals('baz', $config['test']['bar']);
  61. }
  62. public function testFromXmlFiles()
  63. {
  64. $files = array(
  65. __DIR__ . '/TestAssets/Xml/include-base.xml',
  66. __DIR__ . '/TestAssets/Xml/include-base2.xml'
  67. );
  68. $config = Factory::fromFiles($files);
  69. $this->assertEquals('bar', $config['base']['foo']);
  70. $this->assertEquals('baz', $config['test']['bar']);
  71. }
  72. public function testFromPhpFiles()
  73. {
  74. $files = array(
  75. __DIR__ . '/TestAssets/Php/include-base.php',
  76. __DIR__ . '/TestAssets/Php/include-base2.php'
  77. );
  78. $config = Factory::fromFiles($files);
  79. $this->assertEquals('bar', $config['base']['foo']);
  80. $this->assertEquals('baz', $config['test']['bar']);
  81. }
  82. public function testFromIniAndXmlAndPhpFiles()
  83. {
  84. $files = array(
  85. __DIR__ . '/TestAssets/Ini/include-base.ini',
  86. __DIR__ . '/TestAssets/Xml/include-base2.xml',
  87. __DIR__ . '/TestAssets/Php/include-base3.php',
  88. );
  89. $config = Factory::fromFiles($files);
  90. $this->assertEquals('bar', $config['base']['foo']);
  91. $this->assertEquals('baz', $config['test']['bar']);
  92. $this->assertEquals('baz', $config['last']['bar']);
  93. }
  94. public function testFromIniAndXmlAndPhpFilesFromIncludePath()
  95. {
  96. $files = array(
  97. 'Ini/include-base.ini',
  98. 'Xml/include-base2.xml',
  99. 'Php/include-base3.php',
  100. );
  101. $config = Factory::fromFiles($files, false, true);
  102. $this->assertEquals('bar', $config['base']['foo']);
  103. $this->assertEquals('baz', $config['test']['bar']);
  104. $this->assertEquals('baz', $config['last']['bar']);
  105. }
  106. public function testReturnsConfigObjectIfRequestedAndArrayOtherwise()
  107. {
  108. $files = array(
  109. __DIR__ . '/TestAssets/Ini/include-base.ini',
  110. );
  111. $configArray = Factory::fromFile($files[0]);
  112. $this->assertTrue(is_array($configArray));
  113. $configArray = Factory::fromFiles($files);
  114. $this->assertTrue(is_array($configArray));
  115. $configObject = Factory::fromFile($files[0], true);
  116. $this->assertInstanceOf('Zend\Config\Config', $configObject);
  117. $configObject = Factory::fromFiles($files, true);
  118. $this->assertInstanceOf('Zend\Config\Config', $configObject);
  119. }
  120. public function testNonExistentFileThrowsRuntimeException()
  121. {
  122. $this->setExpectedException('RuntimeException');
  123. $config = Factory::fromFile('foo.bar');
  124. }
  125. public function testUnsupportedFileExtensionThrowsRuntimeException()
  126. {
  127. $this->setExpectedException('RuntimeException');
  128. $config = Factory::fromFile(__DIR__ . '/TestAssets/bad.ext');
  129. }
  130. public function testFactoryCanRegisterCustomReaderInstance()
  131. {
  132. Factory::registerReader('dum', new Reader\TestAssets\DummyReader());
  133. $configObject = Factory::fromFile(__DIR__ . '/TestAssets/dummy.dum', true);
  134. $this->assertInstanceOf('Zend\Config\Config', $configObject);
  135. $this->assertEquals($configObject['one'], 1);
  136. }
  137. public function testFactoryCanRegisterCustomReaderPlugin()
  138. {
  139. $dummyReader = new Reader\TestAssets\DummyReader();
  140. Factory::getReaderPluginManager()->setService('DummyReader', $dummyReader);
  141. Factory::registerReader('dum', 'DummyReader');
  142. $configObject = Factory::fromFile(__DIR__ . '/TestAssets/dummy.dum', true);
  143. $this->assertInstanceOf('Zend\Config\Config', $configObject);
  144. $this->assertEquals($configObject['one'], 1);
  145. }
  146. public function testFactoryToFileInvalidFileExtension()
  147. {
  148. $this->setExpectedException('RuntimeException');
  149. $result = Factory::toFile(__DIR__.'/TestAssets/bad.ext', array());
  150. }
  151. public function testFactoryToFileNoDirInHere()
  152. {
  153. $this->setExpectedException('RuntimeException');
  154. $result = Factory::toFile(__DIR__.'/TestAssets/NoDirInHere/nonExisiting/dummy.php', array());
  155. }
  156. public function testFactoryWriteToFile()
  157. {
  158. $config = array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo'));
  159. $file = $this->getTestAssetFileName('php');
  160. $result = Factory::toFile($file, $config);
  161. // build string line by line as we are trailing-whitespace sensitive.
  162. $expected = "<?php\n";
  163. $expected .= "return array(\n";
  164. $expected .= " 'test' => 'foo',\n";
  165. $expected .= " 'bar' => array(\n";
  166. $expected .= " 0 => 'baz',\n";
  167. $expected .= " 1 => 'foo',\n";
  168. $expected .= " ),\n";
  169. $expected .= ");\n";
  170. $this->assertEquals(true, $result);
  171. $this->assertEquals($expected, file_get_contents($file));
  172. }
  173. public function testFactoryToFileWrongConfig()
  174. {
  175. $this->setExpectedException('InvalidArgumentException');
  176. $result = Factory::toFile('test.ini', 'Im wrong');
  177. }
  178. public function testFactoryRegisterInvalidWriter()
  179. {
  180. $this->setExpectedException('InvalidArgumentException');
  181. Factory::registerWriter('dum', new Reader\TestAssets\DummyReader());
  182. }
  183. public function testFactoryCanRegisterCustomWriterInstance()
  184. {
  185. Factory::registerWriter('dum', new Writer\TestAssets\DummyWriter());
  186. $file = $this->getTestAssetFileName('dum');
  187. $res = Factory::toFile($file, array('one' => 1));
  188. $this->assertEquals($res, true);
  189. }
  190. public function testFactoryCanRegisterCustomWriterPlugin()
  191. {
  192. $dummyWriter = new Writer\TestAssets\DummyWriter();
  193. Factory::getWriterPluginManager()->setService('DummyWriter', $dummyWriter);
  194. Factory::registerWriter('dum', 'DummyWriter');
  195. $file = $this->getTestAssetFileName('dum');
  196. $res = Factory::toFile($file, array('one' => 1));
  197. $this->assertEquals($res, true);
  198. }
  199. }