XmlTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * @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. public function testNumericArray()
  125. {
  126. $config = new Zend_Config(array('foo' => array('bar' => array(1 => 'a', 2 => 'b', 5 => 'c'))));
  127. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  128. $writer->write();
  129. $config = new Zend_Config_Xml($this->_tempName, null);
  130. $this->assertEquals('a', $config->foo->bar->{0});
  131. $this->assertEquals('b', $config->foo->bar->{1});
  132. $this->assertEquals('c', $config->foo->bar->{2});
  133. }
  134. public function testMixedArrayFailure()
  135. {
  136. $config = new Zend_Config(array('foo' => array('bar' => array('a', 'b', 'c' => 'd'))));
  137. try {
  138. $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => $this->_tempName));
  139. $writer->write();
  140. $this->fail('Expected Zend_Config_Exception not raised');
  141. } catch (Zend_Config_Exception $e) {
  142. $this->assertEquals('Mixing of string and numeric keys is not allowed', $e->getMessage());
  143. }
  144. }
  145. public function testArgumentOverride()
  146. {
  147. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  148. $writer = new Zend_Config_Writer_Xml();
  149. $writer->write($this->_tempName, $config);
  150. $config = new Zend_Config_Xml($this->_tempName, null);
  151. $this->assertEquals('foo', $config->default->test);
  152. }
  153. }