/lib/Cake/Test/Case/Configure/IniReaderTest.php

https://github.com/kunit/cakephp · PHP · 306 lines · 155 code · 40 blank · 111 comment · 0 complexity · 50f2e0a12de33b7194b6584809498c80 MD5 · raw file

  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Configure
  17. * @since CakePHP(tm) v 2.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('IniReader', 'Configure');
  21. /**
  22. * Class IniReaderTest
  23. *
  24. * @package Cake.Test.Case.Configure
  25. */
  26. class IniReaderTest extends CakeTestCase {
  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 = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  54. }
  55. /**
  56. * test construct
  57. *
  58. * @return void
  59. */
  60. public function testConstruct() {
  61. $reader = new IniReader($this->path);
  62. $config = $reader->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. $reader = new IniReader($this->path);
  74. $config = $reader->read('nested');
  75. $this->assertTrue($config['bools']['test_on']);
  76. $config = $reader->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. $reader = new IniReader($this->path, 'admin');
  86. $config = $reader->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. $reader = new IniReader($this->path);
  97. $config = $reader->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. $reader = new IniReader($this->path);
  109. $config = $reader->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. $reader = new IniReader($this->path);
  123. $config = $reader->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. $reader = new IniReader($this->path);
  137. $config = $reader->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 ConfigureException
  150. * @return void
  151. */
  152. public function testReadWithExistentFileWithoutExtension() {
  153. $reader = new IniReader($this->path);
  154. $reader->read('no_ini_extension');
  155. }
  156. /**
  157. * Test an exception is thrown by reading files that don't exist.
  158. *
  159. * @expectedException ConfigureException
  160. * @return void
  161. */
  162. public function testReadWithNonExistentFile() {
  163. $reader = new IniReader($this->path);
  164. $reader->read('fake_values');
  165. }
  166. /**
  167. * Test reading an empty file.
  168. *
  169. * @return void
  170. */
  171. public function testReadEmptyFile() {
  172. $reader = new IniReader($this->path);
  173. $config = $reader->read('empty');
  174. $this->assertEquals(array(), $config);
  175. }
  176. /**
  177. * Test reading keys with ../ doesn't work.
  178. *
  179. * @expectedException ConfigureException
  180. * @return void
  181. */
  182. public function testReadWithDots() {
  183. $reader = new IniReader($this->path);
  184. $reader->read('../empty');
  185. }
  186. /**
  187. * Test reading from plugins.
  188. *
  189. * @return void
  190. */
  191. public function testReadPluginValue() {
  192. App::build(array(
  193. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  194. ), App::RESET);
  195. CakePlugin::load('TestPlugin');
  196. $reader = new IniReader($this->path);
  197. $result = $reader->read('TestPlugin.nested');
  198. $this->assertTrue(isset($result['database']['db']['username']));
  199. $this->assertEquals('bar', $result['database']['db']['username']);
  200. $this->assertFalse(isset($result['database.db.username']));
  201. $this->assertFalse(isset($result['database']['db.username']));
  202. $result = $reader->read('TestPlugin.nested.ini');
  203. $this->assertEquals('foo', $result['database']['db']['password']);
  204. CakePlugin::unload();
  205. }
  206. /**
  207. * Test reading acl.ini.php from plugins.
  208. *
  209. * @return void
  210. */
  211. public function testReadPluginSpecialAclIniPhpValue() {
  212. App::build(array(
  213. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  214. ), App::RESET);
  215. CakePlugin::load('TestPlugin');
  216. $reader = new IniReader($this->path);
  217. $result = $reader->read('TestPlugin.acl.ini.php');
  218. $this->assertTrue(isset($result['admin']));
  219. $this->assertTrue(isset($result['paul']['groups']));
  220. $this->assertEquals('ads', $result['admin']['deny']);
  221. CakePlugin::unload();
  222. }
  223. /**
  224. * Test dump method.
  225. *
  226. * @return void
  227. */
  228. public function testDump() {
  229. $reader = new IniReader(TMP);
  230. $result = $reader->dump('test.ini', $this->testData);
  231. $this->assertTrue($result > 0);
  232. $expected = <<<INI
  233. [One]
  234. two = value
  235. three.four = value four
  236. is_null = null
  237. bool_false = false
  238. bool_true = true
  239. [Asset]
  240. timestamp = force
  241. INI;
  242. $file = TMP . 'test.ini';
  243. $result = file_get_contents($file);
  244. unlink($file);
  245. $this->assertTextEquals($expected, $result);
  246. $result = $reader->dump('test', $this->testData);
  247. $this->assertTrue($result > 0);
  248. $contents = file_get_contents($file);
  249. $this->assertTextEquals($expected, $contents);
  250. unlink($file);
  251. }
  252. /**
  253. * Test that dump() makes files read() can read.
  254. *
  255. * @return void
  256. */
  257. public function testDumpRead() {
  258. $reader = new IniReader(TMP);
  259. $reader->dump('test.ini', $this->testData);
  260. $result = $reader->read('test.ini');
  261. unlink(TMP . 'test.ini');
  262. $expected = $this->testData;
  263. $expected['One']['is_null'] = false;
  264. $this->assertEquals($expected, $result);
  265. }
  266. }