IniConfigTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * IniConfigTest
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Configure\Engine;
  18. use Cake\Configure\Engine\IniConfig;
  19. use Cake\Core\App;
  20. use Cake\Core\Plugin;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Class IniConfigTest
  24. *
  25. */
  26. class IniConfigTest extends TestCase {
  27. /**
  28. * Test data to serialize and unserialize.
  29. *
  30. * @var array
  31. */
  32. public $testData = array(
  33. 'One' => array(
  34. 'two' => 'value',
  35. 'three' => array(
  36. 'four' => 'value four'
  37. ),
  38. 'is_null' => null,
  39. 'bool_false' => false,
  40. 'bool_true' => true,
  41. ),
  42. 'Asset' => array(
  43. 'timestamp' => 'force'
  44. ),
  45. );
  46. /**
  47. * setup
  48. *
  49. * @return void
  50. */
  51. public function setUp() {
  52. parent::setUp();
  53. $this->path = TEST_APP . 'TestApp/Config' . DS;
  54. }
  55. /**
  56. * test construct
  57. *
  58. * @return void
  59. */
  60. public function testConstruct() {
  61. $engine = new IniConfig($this->path);
  62. $config = $engine->read('acl.ini');
  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. $engine = new IniConfig($this->path);
  74. $config = $engine->read('nested');
  75. $this->assertTrue($config['bools']['test_on']);
  76. $config = $engine->read('nested.ini');
  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. $engine = new IniConfig($this->path, 'admin');
  86. $config = $engine->read('acl.ini');
  87. $this->assertTrue(isset($config['groups']));
  88. $this->assertEquals('administrators', $config['groups']);
  89. }
  90. /**
  91. * Test reading acl.ini.php.
  92. *
  93. * @return void
  94. */
  95. public function testReadSpecialAclIniPhp() {
  96. $engine = new IniConfig($this->path);
  97. $config = $engine->read('acl.ini.php');
  98. $this->assertTrue(isset($config['admin']));
  99. $this->assertTrue(isset($config['paul']['groups']));
  100. $this->assertEquals('ads', $config['admin']['deny']);
  101. }
  102. /**
  103. * Test without section.
  104. *
  105. * @return void
  106. */
  107. public function testReadWithoutSection() {
  108. $engine = new IniConfig($this->path);
  109. $config = $engine->read('no_section.ini');
  110. $expected = array(
  111. 'some_key' => 'some_value',
  112. 'bool_key' => true
  113. );
  114. $this->assertEquals($expected, $config);
  115. }
  116. /**
  117. * Test that names with .'s get exploded into arrays.
  118. *
  119. * @return void
  120. */
  121. public function testReadValuesWithDots() {
  122. $engine = new IniConfig($this->path);
  123. $config = $engine->read('nested.ini');
  124. $this->assertTrue(isset($config['database']['db']['username']));
  125. $this->assertEquals('mark', $config['database']['db']['username']);
  126. $this->assertEquals(3, $config['nesting']['one']['two']['three']);
  127. $this->assertFalse(isset($config['database.db.username']));
  128. $this->assertFalse(isset($config['database']['db.username']));
  129. }
  130. /**
  131. * Test boolean reading.
  132. *
  133. * @return void
  134. */
  135. public function testBooleanReading() {
  136. $engine = new IniConfig($this->path);
  137. $config = $engine->read('nested.ini');
  138. $this->assertTrue($config['bools']['test_on']);
  139. $this->assertFalse($config['bools']['test_off']);
  140. $this->assertTrue($config['bools']['test_yes']);
  141. $this->assertFalse($config['bools']['test_no']);
  142. $this->assertTrue($config['bools']['test_true']);
  143. $this->assertFalse($config['bools']['test_false']);
  144. $this->assertFalse($config['bools']['test_null']);
  145. }
  146. /**
  147. * Test an exception is thrown by reading files that exist without .ini extension.
  148. *
  149. * @expectedException \Cake\Error\Exception
  150. * @return void
  151. */
  152. public function testReadWithExistentFileWithoutExtension() {
  153. $engine = new IniConfig($this->path);
  154. $engine->read('no_ini_extension');
  155. }
  156. /**
  157. * Test an exception is thrown by reading files that don't exist.
  158. *
  159. * @expectedException \Cake\Error\Exception
  160. * @return void
  161. */
  162. public function testReadWithNonExistentFile() {
  163. $engine = new IniConfig($this->path);
  164. $engine->read('fake_values');
  165. }
  166. /**
  167. * Test reading an empty file.
  168. *
  169. * @return void
  170. */
  171. public function testReadEmptyFile() {
  172. $engine = new IniConfig($this->path);
  173. $config = $engine->read('empty');
  174. $this->assertEquals(array(), $config);
  175. }
  176. /**
  177. * Test reading keys with ../ doesn't work.
  178. *
  179. * @expectedException \Cake\Error\Exception
  180. * @return void
  181. */
  182. public function testReadWithDots() {
  183. $engine = new IniConfig($this->path);
  184. $engine->read('../empty');
  185. }
  186. /**
  187. * Test reading from plugins.
  188. *
  189. * @return void
  190. */
  191. public function testReadPluginValue() {
  192. Plugin::load('TestPlugin');
  193. $engine = new IniConfig($this->path);
  194. $result = $engine->read('TestPlugin.nested');
  195. $this->assertTrue(isset($result['database']['db']['username']));
  196. $this->assertEquals('bar', $result['database']['db']['username']);
  197. $this->assertFalse(isset($result['database.db.username']));
  198. $this->assertFalse(isset($result['database']['db.username']));
  199. $result = $engine->read('TestPlugin.nested.ini');
  200. $this->assertEquals('foo', $result['database']['db']['password']);
  201. Plugin::unload();
  202. }
  203. /**
  204. * Test reading acl.ini.php from plugins.
  205. *
  206. * @return void
  207. */
  208. public function testReadPluginSpecialAclIniPhpValue() {
  209. Plugin::load('TestPlugin');
  210. $engine = new IniConfig($this->path);
  211. $result = $engine->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. Plugin::unload();
  216. }
  217. /**
  218. * Test dump method.
  219. *
  220. * @return void
  221. */
  222. public function testDump() {
  223. $engine = new IniConfig(TMP);
  224. $result = $engine->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 = $engine->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. $engine = new IniConfig(TMP);
  253. $engine->dump('test.ini', $this->testData);
  254. $result = $engine->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. }