PhpConfigTest.php 4.6 KB

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