PhpConfigTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * PhpConfigReaderTest
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Configure\Engine;
  18. use Cake\Configure\Engine\PhpConfig;
  19. use Cake\Core\App;
  20. use Cake\Core\Plugin;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Class PhpConfigTest
  24. *
  25. */
  26. class PhpConfigTest extends TestCase {
  27. /**
  28. * Test data to serialize and unserialize.
  29. *
  30. * @var array
  31. */
  32. public $testData = array(
  33. 'One' => array(
  34. 'two' => 'value',
  35. 'three' => array(
  36. 'four' => 'value four'
  37. ),
  38. 'is_null' => null,
  39. 'bool_false' => false,
  40. 'bool_true' => true,
  41. ),
  42. 'Asset' => array(
  43. 'timestamp' => 'force'
  44. ),
  45. );
  46. /**
  47. * Setup.
  48. *
  49. * @return void
  50. */
  51. public function setUp() {
  52. parent::setUp();
  53. $this->path = TEST_APP . 'TestApp/Config' . DS;
  54. }
  55. /**
  56. * Test reading files.
  57. *
  58. * @return void
  59. */
  60. public function testRead() {
  61. $engine = new PhpConfig($this->path);
  62. $values = $engine->read('var_test');
  63. $this->assertEquals('value', $values['Read']);
  64. $this->assertEquals('buried', $values['Deep']['Deeper']['Deepest']);
  65. $values = $engine->read('var_test.php');
  66. $this->assertEquals('value', $values['Read']);
  67. }
  68. /**
  69. * Test an exception is thrown by reading files that exist without .php extension.
  70. *
  71. * @expectedException Cake\Error\ConfigureException
  72. * @return void
  73. */
  74. public function testReadWithExistentFileWithoutExtension() {
  75. $engine = new PhpConfig($this->path);
  76. $engine->read('no_php_extension');
  77. }
  78. /**
  79. * Test an exception is thrown by reading files that don't exist.
  80. *
  81. * @expectedException Cake\Error\ConfigureException
  82. * @return void
  83. */
  84. public function testReadWithNonExistentFile() {
  85. $engine = new PhpConfig($this->path);
  86. $engine->read('fake_values');
  87. }
  88. /**
  89. * Test reading an empty file.
  90. *
  91. * @expectedException Cake\Error\ConfigureException
  92. * @return void
  93. */
  94. public function testReadEmptyFile() {
  95. $engine = new PhpConfig($this->path);
  96. $engine->read('empty');
  97. }
  98. /**
  99. * Test reading keys with ../ doesn't work.
  100. *
  101. * @expectedException Cake\Error\ConfigureException
  102. * @return void
  103. */
  104. public function testReadWithDots() {
  105. $engine = new PhpConfig($this->path);
  106. $engine->read('../empty');
  107. }
  108. /**
  109. * Test reading from plugins.
  110. *
  111. * @return void
  112. */
  113. public function testReadPluginValue() {
  114. Plugin::load('TestPlugin');
  115. $engine = new PhpConfig($this->path);
  116. $result = $engine->read('TestPlugin.load');
  117. $this->assertTrue(isset($result['plugin_load']));
  118. $result = $engine->read('TestPlugin.load.php');
  119. $this->assertTrue(isset($result['plugin_load']));
  120. Plugin::unload();
  121. }
  122. /**
  123. * Test dumping data to PHP format.
  124. *
  125. * @return void
  126. */
  127. public function testDump() {
  128. $engine = new PhpConfig(TMP);
  129. $result = $engine->dump('test.php', $this->testData);
  130. $this->assertTrue($result > 0);
  131. $expected = <<<PHP
  132. <?php
  133. \$config = array (
  134. 'One' =>
  135. array (
  136. 'two' => 'value',
  137. 'three' =>
  138. array (
  139. 'four' => 'value four',
  140. ),
  141. 'is_null' => NULL,
  142. 'bool_false' => false,
  143. 'bool_true' => true,
  144. ),
  145. 'Asset' =>
  146. array (
  147. 'timestamp' => 'force',
  148. ),
  149. );
  150. PHP;
  151. $file = TMP . 'test.php';
  152. $contents = file_get_contents($file);
  153. unlink($file);
  154. $this->assertTextEquals($expected, $contents);
  155. $result = $engine->dump('test', $this->testData);
  156. $this->assertTrue($result > 0);
  157. $contents = file_get_contents($file);
  158. $this->assertTextEquals($expected, $contents);
  159. unlink($file);
  160. }
  161. /**
  162. * Test that dump() makes files read() can read.
  163. *
  164. * @return void
  165. */
  166. public function testDumpRead() {
  167. $engine = new PhpConfig(TMP);
  168. $engine->dump('test.php', $this->testData);
  169. $result = $engine->read('test.php');
  170. unlink(TMP . 'test.php');
  171. $this->assertEquals($this->testData, $result);
  172. }
  173. }