PhpConfigTest.php 4.8 KB

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