IniConfigTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 2.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\IniConfig;
  18. use Cake\Core\Exception\CakeException;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * IniConfigTest
  22. */
  23. class IniConfigTest extends TestCase
  24. {
  25. /**
  26. * Test data to serialize and unserialize.
  27. *
  28. * @var array
  29. */
  30. protected $testData = [
  31. 'One' => [
  32. 'two' => 'value',
  33. 'three' => [
  34. 'four' => 'value four',
  35. ],
  36. 'is_null' => null,
  37. 'bool_false' => false,
  38. 'bool_true' => true,
  39. ],
  40. 'Asset' => [
  41. 'timestamp' => 'force',
  42. ],
  43. ];
  44. /**
  45. * @var string
  46. */
  47. protected $path;
  48. /**
  49. * setup
  50. */
  51. public function setUp(): void
  52. {
  53. parent::setUp();
  54. $this->path = CONFIG;
  55. }
  56. /**
  57. * test construct
  58. */
  59. public function testConstruct(): void
  60. {
  61. $engine = new IniConfig($this->path);
  62. $config = $engine->read('acl');
  63. $this->assertArrayHasKey('admin', $config);
  64. $this->assertTrue(isset($config['paul']['groups']));
  65. $this->assertSame('ads', $config['admin']['deny']);
  66. }
  67. /**
  68. * Test reading files.
  69. */
  70. public function testRead(): void
  71. {
  72. $engine = new IniConfig($this->path);
  73. $config = $engine->read('nested');
  74. $this->assertTrue($config['bools']['test_on']);
  75. $config = $engine->read('nested');
  76. $this->assertTrue($config['bools']['test_on']);
  77. }
  78. /**
  79. * No other sections should exist.
  80. */
  81. public function testReadOnlyOneSection(): void
  82. {
  83. $engine = new IniConfig($this->path, 'admin');
  84. $config = $engine->read('acl');
  85. $this->assertArrayHasKey('groups', $config);
  86. $this->assertSame('administrators', $config['groups']);
  87. }
  88. /**
  89. * Test without section.
  90. */
  91. public function testReadWithoutSection(): void
  92. {
  93. $engine = new IniConfig($this->path);
  94. $config = $engine->read('no_section');
  95. $expected = [
  96. 'some_key' => 'some_value',
  97. 'bool_key' => true,
  98. ];
  99. $this->assertEquals($expected, $config);
  100. }
  101. /**
  102. * Test that names with .'s get exploded into arrays.
  103. */
  104. public function testReadValuesWithDots(): void
  105. {
  106. $engine = new IniConfig($this->path);
  107. $config = $engine->read('nested');
  108. $this->assertTrue(isset($config['database']['db']['username']));
  109. $this->assertSame('mark', $config['database']['db']['username']);
  110. $this->assertSame('3', $config['nesting']['one']['two']['three']);
  111. $this->assertFalse(isset($config['database.db.username']));
  112. $this->assertFalse(isset($config['database']['db.username']));
  113. }
  114. /**
  115. * Test boolean reading.
  116. */
  117. public function testBooleanReading(): void
  118. {
  119. $engine = new IniConfig($this->path);
  120. $config = $engine->read('nested');
  121. $this->assertTrue($config['bools']['test_on']);
  122. $this->assertFalse($config['bools']['test_off']);
  123. $this->assertTrue($config['bools']['test_yes']);
  124. $this->assertFalse($config['bools']['test_no']);
  125. $this->assertTrue($config['bools']['test_true']);
  126. $this->assertFalse($config['bools']['test_false']);
  127. $this->assertFalse($config['bools']['test_null']);
  128. }
  129. /**
  130. * Test an exception is thrown by reading files that exist without .ini extension.
  131. */
  132. public function testReadWithExistentFileWithoutExtension(): void
  133. {
  134. $this->expectException(CakeException::class);
  135. $engine = new IniConfig($this->path);
  136. $engine->read('no_ini_extension');
  137. }
  138. /**
  139. * Test an exception is thrown by reading files that don't exist.
  140. */
  141. public function testReadWithNonExistentFile(): void
  142. {
  143. $this->expectException(CakeException::class);
  144. $engine = new IniConfig($this->path);
  145. $engine->read('fake_values');
  146. }
  147. /**
  148. * Test reading an empty file.
  149. */
  150. public function testReadEmptyFile(): void
  151. {
  152. $engine = new IniConfig($this->path);
  153. $config = $engine->read('empty');
  154. $this->assertEquals([], $config);
  155. }
  156. /**
  157. * Test reading keys with ../ doesn't work.
  158. */
  159. public function testReadWithDots(): void
  160. {
  161. $this->expectException(CakeException::class);
  162. $engine = new IniConfig($this->path);
  163. $engine->read('../empty');
  164. }
  165. /**
  166. * Test reading from plugins.
  167. */
  168. public function testReadPluginValue(): void
  169. {
  170. $this->loadPlugins(['TestPlugin']);
  171. $engine = new IniConfig($this->path);
  172. $result = $engine->read('TestPlugin.nested');
  173. $this->assertTrue(isset($result['database']['db']['username']));
  174. $this->assertSame('bar', $result['database']['db']['username']);
  175. $this->assertFalse(isset($result['database.db.username']));
  176. $this->assertFalse(isset($result['database']['db.username']));
  177. $result = $engine->read('TestPlugin.nested');
  178. $this->assertSame('foo', $result['database']['db']['password']);
  179. $this->clearPlugins();
  180. }
  181. /**
  182. * Test dump method.
  183. */
  184. public function testDump(): void
  185. {
  186. $engine = new IniConfig(TMP);
  187. $result = $engine->dump('test', $this->testData);
  188. $this->assertGreaterThan(0, $result);
  189. $expected = <<<INI
  190. [One]
  191. two = value
  192. three.four = value four
  193. is_null = null
  194. bool_false = false
  195. bool_true = true
  196. [Asset]
  197. timestamp = force
  198. INI;
  199. $file = TMP . 'test.ini';
  200. $result = file_get_contents($file);
  201. unlink($file);
  202. $this->assertTextEquals($expected, $result);
  203. $result = $engine->dump('test', $this->testData);
  204. $this->assertGreaterThan(0, $result);
  205. $contents = file_get_contents($file);
  206. $this->assertTextEquals($expected, $contents);
  207. unlink($file);
  208. }
  209. /**
  210. * Test that dump() makes files read() can read.
  211. */
  212. public function testDumpRead(): void
  213. {
  214. $engine = new IniConfig(TMP);
  215. $engine->dump('test', $this->testData);
  216. $result = $engine->read('test');
  217. unlink(TMP . 'test.ini');
  218. $expected = $this->testData;
  219. $expected['One']['is_null'] = false;
  220. $this->assertEquals($expected, $result);
  221. }
  222. }