IniReaderTest.php 7.1 KB

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