JsonConfigTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 3.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\JsonConfig;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * JsonConfigTest
  21. */
  22. class JsonConfigTest 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 JsonConfig($this->path);
  61. $values = $engine->read('json_test');
  62. $this->assertEquals('value', $values['Json']);
  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 JsonConfig($this->path);
  74. $engine->read('no_json_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 JsonConfig($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. $this->expectExceptionMessage('config file "empty.json"');
  96. $engine = new JsonConfig($this->path);
  97. $config = $engine->read('empty');
  98. }
  99. /**
  100. * Test an exception is thrown by reading files that contain invalid JSON.
  101. *
  102. * @return void
  103. */
  104. public function testReadWithInvalidJson()
  105. {
  106. $this->expectException(\Cake\Core\Exception\Exception::class);
  107. $this->expectExceptionMessage('Error parsing JSON string fetched from config file "invalid.json"');
  108. $engine = new JsonConfig($this->path);
  109. $engine->read('invalid');
  110. }
  111. /**
  112. * Test reading keys with ../ doesn't work.
  113. *
  114. * @return void
  115. */
  116. public function testReadWithDots()
  117. {
  118. $this->expectException(\Cake\Core\Exception\Exception::class);
  119. $engine = new JsonConfig($this->path);
  120. $engine->read('../empty');
  121. }
  122. /**
  123. * Test reading from plugins.
  124. *
  125. * @return void
  126. */
  127. public function testReadPluginValue()
  128. {
  129. Plugin::load('TestPlugin');
  130. $engine = new JsonConfig($this->path);
  131. $result = $engine->read('TestPlugin.load');
  132. $this->assertArrayHasKey('plugin_load', $result);
  133. Plugin::unload();
  134. }
  135. /**
  136. * Test dumping data to json format.
  137. *
  138. * @return void
  139. */
  140. public function testDump()
  141. {
  142. $engine = new JsonConfig(TMP);
  143. $result = $engine->dump('test', $this->testData);
  144. $this->assertGreaterThan(0, $result);
  145. $expected = '{
  146. "One": {
  147. "two": "value",
  148. "three": {
  149. "four": "value four"
  150. },
  151. "is_null": null,
  152. "bool_false": false,
  153. "bool_true": true
  154. },
  155. "Asset": {
  156. "timestamp": "force"
  157. }
  158. }';
  159. $file = TMP . 'test.json';
  160. $contents = file_get_contents($file);
  161. unlink($file);
  162. $this->assertTextEquals($expected, $contents);
  163. $result = $engine->dump('test', $this->testData);
  164. $this->assertGreaterThan(0, $result);
  165. $contents = file_get_contents($file);
  166. $this->assertTextEquals($expected, $contents);
  167. unlink($file);
  168. }
  169. /**
  170. * Test that dump() makes files read() can read.
  171. *
  172. * @return void
  173. */
  174. public function testDumpRead()
  175. {
  176. $engine = new JsonConfig(TMP);
  177. $engine->dump('test', $this->testData);
  178. $result = $engine->read('test');
  179. unlink(TMP . 'test.json');
  180. $this->assertEquals($this->testData, $result);
  181. }
  182. }