IniConfigTest.php 7.1 KB

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