XmlTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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-2009 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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Config_Writer_XmlTest extends PHPUnit_Framework_TestCase
  46. {
  47. protected $_tempName;
  48. public function setUp()
  49. {
  50. $this->_tempName = tempnam(dirname(__FILE__) . '/temp', 'tmp');
  51. }
  52. public function tearDown()
  53. {
  54. @unlink($this->_tempName);
  55. }
  56. public function testNoFilenameSet()
  57. {
  58. $writer = new Zend_Config_Writer_Xml(array('config' => new Zend_Config(array())));
  59. try {
  60. $writer->write();
  61. $this->fail('An expected Zend_Config_Exception has not been raised');
  62. } catch (Zend_Config_Exception $expected) {
  63. $this->assertContains('No filename was set', $expected->getMessage());
  64. }
  65. }
  66. public function testNoConfigSet()
  67. {
  68. $writer = new Zend_Config_Writer_Xml(array('filename' => $this->_tempName));
  69. try {
  70. $writer->write();
  71. $this->fail('An expected Zend_Config_Exception has not been raised');
  72. } catch (Zend_Config_Exception $expected) {
  73. $this->assertContains('No config was set', $expected->getMessage());
  74. }
  75. }
  76. public function testFileNotWritable()
  77. {
  78. $writer = new Zend_Config_Writer_Xml(array('config' => new Zend_Config(array()), 'filename' => '/../../../'));
  79. try {
  80. $writer->write();
  81. $this->fail('An expected Zend_Config_Exception has not been raised');
  82. } catch (Zend_Config_Exception $expected) {
  83. $this->assertContains('Could not write to file', $expected->getMessage());
  84. }
  85. }
  86. public function testWriteAndRead()
  87. {
  88. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  89. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  90. $writer->write();
  91. $config = new Zend_Config_Xml($this->_tempName, null);
  92. $this->assertEquals('foo', $config->default->test);
  93. }
  94. public function testNoSection()
  95. {
  96. $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
  97. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  98. $writer->write();
  99. $config = new Zend_Config_Xml($this->_tempName, null);
  100. $this->assertEquals('foo', $config->test);
  101. $this->assertEquals('bar', $config->test2->test3);
  102. }
  103. public function testWriteAndReadOriginalFile()
  104. {
  105. $config = new Zend_Config_Xml(dirname(__FILE__) . '/files/allsections.xml', null, array('skipExtends' => true));
  106. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  107. $writer->write();
  108. $config = new Zend_Config_Xml($this->_tempName, null);
  109. $this->assertEquals('multi', $config->staging->one->two->three);
  110. $config = new Zend_Config_Xml($this->_tempName, null, array('skipExtends' => true));
  111. $this->assertFalse(isset($config->staging->one));
  112. }
  113. public function testWriteAndReadSingleSection()
  114. {
  115. $config = new Zend_Config_Xml(dirname(__FILE__) . '/files/allsections.xml', 'staging', array('skipExtends' => true));
  116. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  117. $writer->write();
  118. $config = new Zend_Config_Xml($this->_tempName, null);
  119. $this->assertEquals('staging', $config->staging->hostname);
  120. $this->assertEquals('false', $config->staging->debug);
  121. $this->assertEquals(null, @$config->production);
  122. }
  123. public function testNumericArray()
  124. {
  125. $config = new Zend_Config(array('foo' => array('bar' => array(1 => 'a', 2 => 'b', 5 => 'c'))));
  126. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  127. $writer->write();
  128. $config = new Zend_Config_Xml($this->_tempName, null);
  129. $this->assertEquals('a', $config->foo->bar->{0});
  130. $this->assertEquals('b', $config->foo->bar->{1});
  131. $this->assertEquals('c', $config->foo->bar->{2});
  132. }
  133. public function testMixedArrayFailure()
  134. {
  135. $config = new Zend_Config(array('foo' => array('bar' => array('a', 'b', 'c' => 'd'))));
  136. try {
  137. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  138. $writer->write();
  139. $this->fail('Expected Zend_Config_Exception not raised');
  140. } catch (Zend_Config_Exception $e) {
  141. $this->assertEquals('Mixing of string and numeric keys is not allowed', $e->getMessage());
  142. }
  143. }
  144. public function testArgumentOverride()
  145. {
  146. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  147. $writer = new Zend_Config_Writer_Xml();
  148. $writer->write($this->_tempName, $config);
  149. $config = new Zend_Config_Xml($this->_tempName, null);
  150. $this->assertEquals('foo', $config->default->test);
  151. }
  152. }