ArrayTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_Writer_Array
  28. */
  29. require_once 'Zend/Config/Writer/Array.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Config
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Config
  37. */
  38. class Zend_Config_Writer_ArrayTest extends PHPUnit_Framework_TestCase
  39. {
  40. protected $_tempName;
  41. public function setUp()
  42. {
  43. $this->_tempName = tempnam(dirname(__FILE__) . '/temp', 'tmp');
  44. }
  45. public function tearDown()
  46. {
  47. @unlink($this->_tempName);
  48. }
  49. public function testNoFilenameSet()
  50. {
  51. $writer = new Zend_Config_Writer_Array(array('config' => new Zend_Config(array())));
  52. try {
  53. $writer->write();
  54. $this->fail('An expected Zend_Config_Exception has not been raised');
  55. } catch (Zend_Config_Exception $expected) {
  56. $this->assertContains('No filename was set', $expected->getMessage());
  57. }
  58. }
  59. public function testNoConfigSet()
  60. {
  61. $writer = new Zend_Config_Writer_Array(array('filename' => $this->_tempName));
  62. try {
  63. $writer->write();
  64. $this->fail('An expected Zend_Config_Exception has not been raised');
  65. } catch (Zend_Config_Exception $expected) {
  66. $this->assertContains('No config was set', $expected->getMessage());
  67. }
  68. }
  69. public function testFileNotWritable()
  70. {
  71. $writer = new Zend_Config_Writer_Array(array('config' => new Zend_Config(array()), 'filename' => '/../../../'));
  72. try {
  73. $writer->write();
  74. $this->fail('An expected Zend_Config_Exception has not been raised');
  75. } catch (Zend_Config_Exception $expected) {
  76. $this->assertContains('Could not write to file', $expected->getMessage());
  77. }
  78. }
  79. public function testWriteAndRead()
  80. {
  81. $config = new Zend_Config(array('test' => 'foo'));
  82. $writer = new Zend_Config_Writer_Array(array('config' => $config, 'filename' => $this->_tempName));
  83. $writer->write();
  84. $config = new Zend_Config(include $this->_tempName);
  85. $this->assertEquals('foo', $config->test);
  86. }
  87. public function testArgumentOverride()
  88. {
  89. $config = new Zend_Config(array('test' => 'foo'));
  90. $writer = new Zend_Config_Writer_Array();
  91. $writer->write($this->_tempName, $config);
  92. $config = new Zend_Config(include $this->_tempName);
  93. $this->assertEquals('foo', $config->test);
  94. }
  95. /**
  96. * @group ZF-8234
  97. */
  98. public function testRender()
  99. {
  100. $config = new Zend_Config(array('test' => 'foo', 'bar' => array(0 => 'baz', 1 => 'foo')));
  101. $writer = new Zend_Config_Writer_Array();
  102. $configString = $writer->setConfig($config)->render();
  103. // build string line by line as we are trailing-whitespace sensitive.
  104. $expected = "<?php\n";
  105. $expected .= "return array (\n";
  106. $expected .= " 'test' => 'foo',\n";
  107. $expected .= " 'bar' => \n";
  108. $expected .= " array (\n";
  109. $expected .= " 0 => 'baz',\n";
  110. $expected .= " 1 => 'foo',\n";
  111. $expected .= " ),\n";
  112. $expected .= ");\n";
  113. $this->assertEquals($expected, $configString);
  114. }
  115. }