XmlTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_Xml
  28. */
  29. require_once 'Zend/Config/Xml.php';
  30. /**
  31. * Zend_Config_Writer_Xml
  32. */
  33. require_once 'Zend/Config/Writer/Xml.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_XmlTest 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_Xml(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_Xml(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_Xml(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_Xml(array('config' => $config, 'filename' => $this->_tempName));
  87. $writer->write();
  88. $config = new Zend_Config_Xml($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_Xml(array('config' => $config, 'filename' => $this->_tempName));
  95. $writer->write();
  96. $config = new Zend_Config_Xml($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_Xml(dirname(__FILE__) . '/files/allsections.xml', null, array('skipExtends' => true));
  103. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  104. $writer->write();
  105. $config = new Zend_Config_Xml($this->_tempName, null);
  106. $this->assertEquals('multi', $config->staging->one->two->three);
  107. $config = new Zend_Config_Xml($this->_tempName, null, array('skipExtends' => true));
  108. $this->assertFalse(isset($config->staging->one));
  109. }
  110. public function testWriteAndReadSingleSection()
  111. {
  112. $config = new Zend_Config_Xml(dirname(__FILE__) . '/files/allsections.xml', 'staging', array('skipExtends' => true));
  113. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  114. $writer->write();
  115. $config = new Zend_Config_Xml($this->_tempName, null);
  116. $this->assertEquals('staging', $config->staging->hostname);
  117. $this->assertEquals('false', $config->staging->debug);
  118. $this->assertEquals(null, @$config->production);
  119. }
  120. /**
  121. * @group ZF-6773
  122. */
  123. public function testWriteMultidimensionalArrayWithNumericKeys()
  124. {
  125. $writer = new Zend_Config_Writer_Xml;
  126. $writer->write($this->_tempName, new Zend_Config(array(
  127. 'notification' => array(
  128. 'adress' => array(
  129. 0 => array(
  130. 'name' => 'Matthew',
  131. 'mail' => 'matthew@example.com'
  132. ),
  133. 1 => array(
  134. 'name' => 'Thomas',
  135. 'mail' => 'thomas@example.com'
  136. )
  137. )
  138. )
  139. )));
  140. }
  141. public function testNumericArray()
  142. {
  143. $config = new Zend_Config(array('foo' => array('bar' => array(1 => 'a', 2 => 'b', 5 => 'c'))));
  144. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  145. $writer->write();
  146. $config = new Zend_Config_Xml($this->_tempName, null);
  147. $this->assertEquals('a', $config->foo->bar->{0});
  148. $this->assertEquals('b', $config->foo->bar->{1});
  149. $this->assertEquals('c', $config->foo->bar->{2});
  150. }
  151. public function testMixedArrayFailure()
  152. {
  153. $config = new Zend_Config(array('foo' => array('bar' => array('a', 'b', 'c' => 'd'))));
  154. try {
  155. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  156. $writer->write();
  157. $this->fail('Expected Zend_Config_Exception not raised');
  158. } catch (Zend_Config_Exception $e) {
  159. $this->assertEquals('Mixing of string and numeric keys is not allowed', $e->getMessage());
  160. }
  161. }
  162. public function testArgumentOverride()
  163. {
  164. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  165. $writer = new Zend_Config_Writer_Xml();
  166. $writer->write($this->_tempName, $config);
  167. $config = new Zend_Config_Xml($this->_tempName, null);
  168. $this->assertEquals('foo', $config->default->test);
  169. }
  170. /**
  171. * @group ZF-8234
  172. */
  173. public function testRender()
  174. {
  175. $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
  176. $writer = new Zend_Config_Writer_Xml();
  177. $configString = $writer->setConfig($config)->render();
  178. $expected = <<<ECS
  179. <?xml version="1.0"?>
  180. <zend-config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  181. <test>foo</test>
  182. <bar>baz</bar>
  183. <bar>foo</bar>
  184. </zend-config>
  185. ECS;
  186. $this->assertEquals($expected, $configString);
  187. }
  188. }