IniConfigTest.php 7.1 KB

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