JsonConfigTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  12. * @since 3.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\JsonConfig;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Class JsonConfigTest
  22. *
  23. */
  24. class JsonConfigTest 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 JsonConfig($this->path);
  63. $values = $engine->read('json_test');
  64. $this->assertEquals('value', $values['Json']);
  65. $this->assertEquals('buried', $values['Deep']['Deeper']['Deepest']);
  66. }
  67. /**
  68. * Test an exception is thrown by reading files that exist without .php extension.
  69. *
  70. * @expectedException \Cake\Core\Exception\Exception
  71. * @return void
  72. */
  73. public function testReadWithExistentFileWithoutExtension()
  74. {
  75. $engine = new JsonConfig($this->path);
  76. $engine->read('no_json_extension');
  77. }
  78. /**
  79. * Test an exception is thrown by reading files that don't exist.
  80. *
  81. * @expectedException \Cake\Core\Exception\Exception
  82. * @return void
  83. */
  84. public function testReadWithNonExistentFile()
  85. {
  86. $engine = new JsonConfig($this->path);
  87. $engine->read('fake_values');
  88. }
  89. /**
  90. * Test reading an empty file.
  91. *
  92. * @expectedException \Cake\Core\Exception\Exception
  93. * @expcetedExceptionMessage Decoding JSON config file "empty.json" did not return any array
  94. * @return void
  95. */
  96. public function testReadEmptyFile()
  97. {
  98. $engine = new JsonConfig($this->path);
  99. $config = $engine->read('empty');
  100. }
  101. /**
  102. * Test an exception is thrown by reading files that contain invalid JSON.
  103. *
  104. * @expectedException \Cake\Core\Exception\Exception
  105. * @expectedExceptionMessage Error parsing JSON string fetched from config file "invalid.json"
  106. * @return void
  107. */
  108. public function testReadWithInvalidJson()
  109. {
  110. $engine = new JsonConfig($this->path);
  111. $engine->read('invalid');
  112. }
  113. /**
  114. * Test reading keys with ../ doesn't work.
  115. *
  116. * @expectedException \Cake\Core\Exception\Exception
  117. * @return void
  118. */
  119. public function testReadWithDots()
  120. {
  121. $engine = new JsonConfig($this->path);
  122. $engine->read('../empty');
  123. }
  124. /**
  125. * Test reading from plugins.
  126. *
  127. * @return void
  128. */
  129. public function testReadPluginValue()
  130. {
  131. Plugin::load('TestPlugin');
  132. $engine = new JsonConfig($this->path);
  133. $result = $engine->read('TestPlugin.load');
  134. $this->assertTrue(isset($result['plugin_load']));
  135. Plugin::unload();
  136. }
  137. /**
  138. * Test dumping data to json format.
  139. *
  140. * @return void
  141. */
  142. public function testDump()
  143. {
  144. $engine = new JsonConfig(TMP);
  145. $result = $engine->dump('test', $this->testData);
  146. $this->assertTrue($result > 0);
  147. $expected = '{"One":{"two":"value","three":{"four":"value four"},"is_null":null,"bool_false":false,"bool_true":true},"Asset":{"timestamp":"force"}}';
  148. $file = TMP . 'test.json';
  149. $contents = file_get_contents($file);
  150. unlink($file);
  151. $this->assertTextEquals($expected, $contents);
  152. $result = $engine->dump('test', $this->testData);
  153. $this->assertTrue($result > 0);
  154. $contents = file_get_contents($file);
  155. $this->assertTextEquals($expected, $contents);
  156. unlink($file);
  157. }
  158. /**
  159. * Test that dump() makes files read() can read.
  160. *
  161. * @return void
  162. */
  163. public function testDumpRead()
  164. {
  165. $engine = new JsonConfig(TMP);
  166. $engine->dump('test', $this->testData);
  167. $result = $engine->read('test');
  168. unlink(TMP . 'test.json');
  169. $this->assertEquals($this->testData, $result);
  170. }
  171. }