2
0

IniTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Config
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Config
  24. */
  25. require_once 'Zend/Config.php';
  26. /**
  27. * Zend_Config_Ini
  28. */
  29. require_once 'Zend/Config/Ini.php';
  30. /**
  31. * Zend_Config_Writer_Ini
  32. */
  33. require_once 'Zend/Config/Writer/Ini.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Config
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Config
  41. */
  42. class Zend_Config_Writer_IniTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_tempName;
  45. public function setUp()
  46. {
  47. $this->_tempName = tempnam(dirname(__FILE__) . '/temp', 'tmp');
  48. }
  49. public function tearDown()
  50. {
  51. @unlink($this->_tempName);
  52. }
  53. public function testNoFilenameSet()
  54. {
  55. $writer = new Zend_Config_Writer_Ini(array('config' => new Zend_Config(array())));
  56. try {
  57. $writer->write();
  58. $this->fail('An expected Zend_Config_Exception has not been raised');
  59. } catch (Zend_Config_Exception $expected) {
  60. $this->assertContains('No filename was set', $expected->getMessage());
  61. }
  62. }
  63. public function testNoConfigSet()
  64. {
  65. $writer = new Zend_Config_Writer_Ini(array('filename' => $this->_tempName));
  66. try {
  67. $writer->write();
  68. $this->fail('An expected Zend_Config_Exception has not been raised');
  69. } catch (Zend_Config_Exception $expected) {
  70. $this->assertContains('No config was set', $expected->getMessage());
  71. }
  72. }
  73. public function testFileNotWritable()
  74. {
  75. $writer = new Zend_Config_Writer_Ini(array('config' => new Zend_Config(array()), 'filename' => '/../../../'));
  76. try {
  77. $writer->write();
  78. $this->fail('An expected Zend_Config_Exception has not been raised');
  79. } catch (Zend_Config_Exception $expected) {
  80. $this->assertContains('Could not write to file', $expected->getMessage());
  81. }
  82. }
  83. public function testWriteAndRead()
  84. {
  85. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  86. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  87. $writer->write();
  88. $config = new Zend_Config_Ini($this->_tempName, null);
  89. $this->assertEquals('foo', $config->default->test);
  90. }
  91. public function testNoSection()
  92. {
  93. $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
  94. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  95. $writer->write();
  96. $config = new Zend_Config_Ini($this->_tempName, null);
  97. $this->assertEquals('foo', $config->test);
  98. $this->assertEquals('bar', $config->test2->test3);
  99. }
  100. public function testWriteAndReadOriginalFile()
  101. {
  102. $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', null, array('skipExtends' => true));
  103. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  104. $writer->write();
  105. $config = new Zend_Config_Ini($this->_tempName, null);
  106. $this->assertEquals('multi', $config->staging->one->two->three);
  107. $config = new Zend_Config_Ini($this->_tempName, null, array('skipExtends' => true));
  108. $this->assertFalse(isset($config->staging->one));
  109. }
  110. public function testWriteAndReadSingleSection()
  111. {
  112. $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', 'staging', array('skipExtends' => true));
  113. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  114. $writer->write();
  115. $config = new Zend_Config_Ini($this->_tempName, null);
  116. $this->assertEquals('staging', $config->staging->hostname);
  117. $this->assertEquals('', $config->staging->debug);
  118. $this->assertEquals(null, @$config->production);
  119. }
  120. public function testArgumentOverride()
  121. {
  122. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  123. $writer = new Zend_Config_Writer_Ini();
  124. $writer->write($this->_tempName, $config);
  125. $config = new Zend_Config_Ini($this->_tempName, null);
  126. $this->assertEquals('foo', $config->default->test);
  127. }
  128. /**
  129. * @group ZF-8234
  130. */
  131. public function testRender()
  132. {
  133. $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
  134. $writer = new Zend_Config_Writer_Ini();
  135. $iniString = $writer->setConfig($config)->render();
  136. $expected = <<<ECS
  137. test = "foo"
  138. [bar]
  139. 0 = "baz"
  140. 1 = "foo"
  141. ECS;
  142. $this->assertEquals($expected, $iniString);
  143. }
  144. public function testRenderWithoutSections()
  145. {
  146. $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
  147. $writer = new Zend_Config_Writer_Ini();
  148. $writer->setRenderWithoutSections();
  149. $iniString = $writer->setConfig($config)->render();
  150. $expected = <<<ECS
  151. test = "foo"
  152. test2.test3 = "bar"
  153. ECS;
  154. $this->assertEquals($expected, $iniString);
  155. }
  156. public function testRenderWithoutSections2()
  157. {
  158. $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', null, array('skipExtends' => true));
  159. $writer = new Zend_Config_Writer_Ini();
  160. $writer->setRenderWithoutSections();
  161. $iniString = $writer->setConfig($config)->render();
  162. $expected = <<<ECS
  163. all.hostname = "all"
  164. all.name = "thisname"
  165. all.db.host = "127.0.0.1"
  166. all.db.user = "username"
  167. all.db.pass = "password"
  168. all.db.name = "live"
  169. all.one.two.three = "multi"
  170. staging.hostname = "staging"
  171. staging.db.name = "dbstaging"
  172. staging.debug = ""
  173. debug.hostname = "debug"
  174. debug.debug = "1"
  175. debug.values.changed = "1"
  176. debug.db.name = "dbdebug"
  177. debug.special.no = ""
  178. debug.special.null = ""
  179. debug.special.false = ""
  180. other_staging.only_in = "otherStaging"
  181. other_staging.db.pass = "anotherpwd"
  182. ECS;
  183. $this->assertEquals($expected, $iniString);
  184. }
  185. public function testZF6521_NoDoubleQuoutesInValue()
  186. {
  187. $config = new Zend_Config(array('default' => array('test' => 'fo"o')));
  188. try {
  189. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  190. $writer->write();
  191. $this->fail('An expected Zend_Config_Exception has not been raised');
  192. } catch (Zend_Config_Exception $expected) {
  193. $this->assertContains('Value can not contain double quotes "', $expected->getMessage());
  194. }
  195. }
  196. /**
  197. * @group ZF-6289
  198. */
  199. public function testZF6289_NonSectionElementsAndSectionJumbling()
  200. {
  201. $config = new Zend_Config(array(
  202. 'one' => 'element',
  203. 'two' => array('type' => 'section'),
  204. 'three' => 'element',
  205. 'four' => array('type' => 'section'),
  206. 'five' => 'element'
  207. ));
  208. $writer = new Zend_Config_Writer_Ini;
  209. $iniString = $writer->setConfig($config)->render($config);
  210. $expected = <<<ECS
  211. one = "element"
  212. three = "element"
  213. five = "element"
  214. [two]
  215. type = "section"
  216. [four]
  217. type = "section"
  218. ECS;
  219. $this->assertEquals(
  220. $expected,
  221. $iniString
  222. );
  223. }
  224. }