JsonTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_Json
  28. */
  29. require_once 'Zend/Config/Json.php';
  30. /**
  31. * Zend_Config_Writer_Json
  32. */
  33. require_once 'Zend/Config/Writer/Json.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. */
  41. class Zend_Config_Writer_JsonTest extends PHPUnit_Framework_TestCase
  42. {
  43. protected $_tempName;
  44. public function setUp()
  45. {
  46. $this->_tempName = tempnam(dirname(__FILE__) . '/temp', 'tmp');
  47. }
  48. public function tearDown()
  49. {
  50. @unlink($this->_tempName);
  51. }
  52. public function testNoFilenameSet()
  53. {
  54. $writer = new Zend_Config_Writer_Json(array('config' => new Zend_Config(array())));
  55. try {
  56. $writer->write();
  57. $this->fail('An expected Zend_Config_Exception has not been raised');
  58. } catch (Zend_Config_Exception $expected) {
  59. $this->assertContains('No filename was set', $expected->getMessage());
  60. }
  61. }
  62. public function testNoConfigSet()
  63. {
  64. $writer = new Zend_Config_Writer_Json(array('filename' => $this->_tempName));
  65. try {
  66. $writer->write();
  67. $this->fail('An expected Zend_Config_Exception has not been raised');
  68. } catch (Zend_Config_Exception $expected) {
  69. $this->assertContains('No config was set', $expected->getMessage());
  70. }
  71. }
  72. public function testFileNotWritable()
  73. {
  74. $writer = new Zend_Config_Writer_Json(array('config' => new Zend_Config(array()), 'filename' => '/../../../'));
  75. try {
  76. $writer->write();
  77. $this->fail('An expected Zend_Config_Exception has not been raised');
  78. } catch (Zend_Config_Exception $expected) {
  79. $this->assertContains('Could not write to file', $expected->getMessage());
  80. }
  81. }
  82. public function testWriteAndRead()
  83. {
  84. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  85. $writer = new Zend_Config_Writer_Json(array('config' => $config, 'filename' => $this->_tempName));
  86. $writer->write();
  87. $config = new Zend_Config_Json($this->_tempName, null);
  88. $this->assertEquals('foo', $config->default->test);
  89. }
  90. public function testNoSection()
  91. {
  92. $config = new Zend_Config(array('test' => 'foo', 'test2' => array('test3' => 'bar')));
  93. $writer = new Zend_Config_Writer_Json(array('config' => $config, 'filename' => $this->_tempName));
  94. $writer->write();
  95. $config = new Zend_Config_Json($this->_tempName);
  96. $this->assertEquals('foo', $config->test);
  97. $this->assertEquals('bar', $config->test2->test3);
  98. }
  99. public function testWriteAndReadOriginalFile()
  100. {
  101. $config = new Zend_Config_Json(dirname(__FILE__) . '/files/allsections.json', null, array('skip_extends' => true));
  102. $writer = new Zend_Config_Writer_Json(array('config' => $config, 'filename' => $this->_tempName));
  103. $writer->write();
  104. $config = new Zend_Config_Json($this->_tempName, null);
  105. $this->assertEquals('multi', $config->staging->one->two->three, var_export($config->toArray(), 1));
  106. $config = new Zend_Config_Json($this->_tempName, null, array('skip_extends' => true));
  107. $this->assertFalse(isset($config->staging->one));
  108. }
  109. public function testWriteAndReadSingleSection()
  110. {
  111. $config = new Zend_Config_Json(dirname(__FILE__) . '/files/allsections.json', 'staging', array('skip_extends' => true));
  112. $writer = new Zend_Config_Writer_Json(array('config' => $config, 'filename' => $this->_tempName));
  113. $writer->write();
  114. $config = new Zend_Config_Json($this->_tempName, null);
  115. $this->assertEquals('staging', $config->staging->hostname);
  116. $this->assertEquals('', $config->staging->debug);
  117. $this->assertEquals(null, @$config->production);
  118. }
  119. public function testArgumentOverride()
  120. {
  121. $config = new Zend_Config(array('default' => array('test' => 'foo')));
  122. $writer = new Zend_Config_Writer_Json();
  123. $writer->write($this->_tempName, $config);
  124. $config = new Zend_Config_Json($this->_tempName, null);
  125. $this->assertEquals('foo', $config->default->test);
  126. }
  127. public function testCanWritePrettyPrintedVersion()
  128. {
  129. $config = new Zend_Config_Json(dirname(__FILE__) . '/files/allsections-pretty.json');
  130. $writer = new Zend_Config_Writer_Json(array('config' => $config, 'filename' => $this->_tempName));
  131. $writer->setPrettyPrint(true);
  132. $writer->write();
  133. $testOutput = file_get_contents($this->_tempName);
  134. $this->assertRegExp('/^\s+/m', $testOutput);
  135. }
  136. }