IniConfigTest.php 7.1 KB

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