IniConfigTest.php 6.9 KB

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