JsonConfigTest.php 4.9 KB

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