IniTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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-2010 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. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /**
  27. * Zend_Config
  28. */
  29. require_once 'Zend/Config.php';
  30. /**
  31. * Zend_Config_Ini
  32. */
  33. require_once 'Zend/Config/Ini.php';
  34. /**
  35. * Zend_Config_Writer_Ini
  36. */
  37. require_once 'Zend/Config/Writer/Ini.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Config
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Config
  45. */
  46. class Zend_Config_Writer_IniTest extends PHPUnit_Framework_TestCase
  47. {
  48. protected $_tempName;
  49. public function setUp()
  50. {
  51. $this->_tempName = tempnam(dirname(__FILE__) . '/temp', 'tmp');
  52. }
  53. public function tearDown()
  54. {
  55. @unlink($this->_tempName);
  56. }
  57. public function testNoFilenameSet()
  58. {
  59. $writer = new Zend_Config_Writer_Ini(array('config' => new Zend_Config(array())));
  60. try {
  61. $writer->write();
  62. $this->fail('An expected Zend_Config_Exception has not been raised');
  63. } catch (Zend_Config_Exception $expected) {
  64. $this->assertContains('No filename was set', $expected->getMessage());
  65. }
  66. }
  67. public function testNoConfigSet()
  68. {
  69. $writer = new Zend_Config_Writer_Ini(array('filename' => $this->_tempName));
  70. try {
  71. $writer->write();
  72. $this->fail('An expected Zend_Config_Exception has not been raised');
  73. } catch (Zend_Config_Exception $expected) {
  74. $this->assertContains('No config was set', $expected->getMessage());
  75. }
  76. }
  77. public function testFileNotWritable()
  78. {
  79. $writer = new Zend_Config_Writer_Ini(array('config' => new Zend_Config(array()), 'filename' => '/../../../'));
  80. try {
  81. $writer->write();
  82. $this->fail('An expected Zend_Config_Exception has not been raised');
  83. } catch (Zend_Config_Exception $expected) {
  84. $this->assertContains('Could not write to file', $expected->getMessage());
  85. }
  86. }
  87. public function testWriteAndRead()
  88. {
  89. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  90. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  91. $writer->write();
  92. $config = new Zend_Config_Ini($this->_tempName, null);
  93. $this->assertEquals('foo', $config->default->test);
  94. }
  95. public function testNoSection()
  96. {
  97. $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
  98. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  99. $writer->write();
  100. $config = new Zend_Config_Ini($this->_tempName, null);
  101. $this->assertEquals('foo', $config->test);
  102. $this->assertEquals('bar', $config->test2->test3);
  103. }
  104. public function testWriteAndReadOriginalFile()
  105. {
  106. $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', null, array('skipExtends' => true));
  107. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  108. $writer->write();
  109. $config = new Zend_Config_Ini($this->_tempName, null);
  110. $this->assertEquals('multi', $config->staging->one->two->three);
  111. $config = new Zend_Config_Ini($this->_tempName, null, array('skipExtends' => true));
  112. $this->assertFalse(isset($config->staging->one));
  113. }
  114. public function testWriteAndReadSingleSection()
  115. {
  116. $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', 'staging', array('skipExtends' => true));
  117. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  118. $writer->write();
  119. $config = new Zend_Config_Ini($this->_tempName, null);
  120. $this->assertEquals('staging', $config->staging->hostname);
  121. $this->assertEquals('', $config->staging->debug);
  122. $this->assertEquals(null, @$config->production);
  123. }
  124. public function testArgumentOverride()
  125. {
  126. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  127. $writer = new Zend_Config_Writer_Ini();
  128. $writer->write($this->_tempName, $config);
  129. $config = new Zend_Config_Ini($this->_tempName, null);
  130. $this->assertEquals('foo', $config->default->test);
  131. }
  132. /**
  133. * @group ZF-8234
  134. */
  135. public function testRender()
  136. {
  137. $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
  138. $writer = new Zend_Config_Writer_Ini();
  139. $iniString = $writer->setConfig($config)->render();
  140. $expected = <<<ECS
  141. test = "foo"
  142. [bar]
  143. 0 = "baz"
  144. 1 = "foo"
  145. ECS;
  146. $this->assertEquals($expected, $iniString);
  147. }
  148. public function testRenderWithoutSections()
  149. {
  150. $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
  151. $writer = new Zend_Config_Writer_Ini();
  152. $writer->setRenderWithoutSections();
  153. $iniString = $writer->setConfig($config)->render();
  154. $expected = <<<ECS
  155. test = "foo"
  156. test2.test3 = "bar"
  157. ECS;
  158. $this->assertEquals($expected, $iniString);
  159. }
  160. public function testRenderWithoutSections2()
  161. {
  162. $config = new Zend_Config_Ini(dirname(__FILE__) . '/files/allsections.ini', null, array('skipExtends' => true));
  163. $writer = new Zend_Config_Writer_Ini();
  164. $writer->setRenderWithoutSections();
  165. $iniString = $writer->setConfig($config)->render();
  166. $expected = <<<ECS
  167. all.hostname = "all"
  168. all.name = "thisname"
  169. all.db.host = "127.0.0.1"
  170. all.db.user = "username"
  171. all.db.pass = "password"
  172. all.db.name = "live"
  173. all.one.two.three = "multi"
  174. staging.hostname = "staging"
  175. staging.db.name = "dbstaging"
  176. staging.debug = ""
  177. debug.hostname = "debug"
  178. debug.debug = "1"
  179. debug.values.changed = "1"
  180. debug.db.name = "dbdebug"
  181. debug.special.no = ""
  182. debug.special.null = ""
  183. debug.special.false = ""
  184. other_staging.only_in = "otherStaging"
  185. other_staging.db.pass = "anotherpwd"
  186. ECS;
  187. $this->assertEquals($expected, $iniString);
  188. }
  189. public function testZF6521_NoDoubleQuoutesInValue()
  190. {
  191. $config = new Zend_Config(array('default' => array('test' => 'fo"o')));
  192. try {
  193. $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $this->_tempName));
  194. $writer->write();
  195. $this->fail('An expected Zend_Config_Exception has not been raised');
  196. } catch (Zend_Config_Exception $expected) {
  197. $this->assertContains('Value can not contain double quotes "', $expected->getMessage());
  198. }
  199. }
  200. }