XmlTest.php 7.3 KB

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