PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/shubam39/CakeTooDoo
PHP | 304 lines | 155 code | 40 blank | 109 comment | 0 complexity | c10f81e019bfa6a2fb827fbce10f2365 MD5 | raw file
  1. <?php
  2. /**
  3. * IniReaderTest
  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. * @package Cake.Test.Case.Configure
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('IniReader', 'Configure');
  19. /**
  20. * Class IniReaderTest
  21. *
  22. * @package Cake.Test.Case.Configure
  23. */
  24. class IniReaderTest extends CakeTestCase {
  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 = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  52. }
  53. /**
  54. * test construct
  55. *
  56. * @return void
  57. */
  58. public function testConstruct() {
  59. $reader = new IniReader($this->path);
  60. $config = $reader->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. $reader = new IniReader($this->path);
  72. $config = $reader->read('nested');
  73. $this->assertTrue($config['bools']['test_on']);
  74. $config = $reader->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. $reader = new IniReader($this->path, 'admin');
  84. $config = $reader->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. $reader = new IniReader($this->path);
  95. $config = $reader->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. $reader = new IniReader($this->path);
  107. $config = $reader->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. $reader = new IniReader($this->path);
  121. $config = $reader->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. $reader = new IniReader($this->path);
  135. $config = $reader->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 ConfigureException
  148. * @return void
  149. */
  150. public function testReadWithExistentFileWithoutExtension() {
  151. $reader = new IniReader($this->path);
  152. $reader->read('no_ini_extension');
  153. }
  154. /**
  155. * Test an exception is thrown by reading files that don't exist.
  156. *
  157. * @expectedException ConfigureException
  158. * @return void
  159. */
  160. public function testReadWithNonExistentFile() {
  161. $reader = new IniReader($this->path);
  162. $reader->read('fake_values');
  163. }
  164. /**
  165. * Test reading an empty file.
  166. *
  167. * @return void
  168. */
  169. public function testReadEmptyFile() {
  170. $reader = new IniReader($this->path);
  171. $config = $reader->read('empty');
  172. $this->assertEquals(array(), $config);
  173. }
  174. /**
  175. * Test reading keys with ../ doesn't work.
  176. *
  177. * @expectedException ConfigureException
  178. * @return void
  179. */
  180. public function testReadWithDots() {
  181. $reader = new IniReader($this->path);
  182. $reader->read('../empty');
  183. }
  184. /**
  185. * Test reading from plugins.
  186. *
  187. * @return void
  188. */
  189. public function testReadPluginValue() {
  190. App::build(array(
  191. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  192. ), App::RESET);
  193. CakePlugin::load('TestPlugin');
  194. $reader = new IniReader($this->path);
  195. $result = $reader->read('TestPlugin.nested');
  196. $this->assertTrue(isset($result['database']['db']['username']));
  197. $this->assertEquals('bar', $result['database']['db']['username']);
  198. $this->assertFalse(isset($result['database.db.username']));
  199. $this->assertFalse(isset($result['database']['db.username']));
  200. $result = $reader->read('TestPlugin.nested.ini');
  201. $this->assertEquals('foo', $result['database']['db']['password']);
  202. CakePlugin::unload();
  203. }
  204. /**
  205. * Test reading acl.ini.php from plugins.
  206. *
  207. * @return void
  208. */
  209. public function testReadPluginSpecialAclIniPhpValue() {
  210. App::build(array(
  211. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  212. ), App::RESET);
  213. CakePlugin::load('TestPlugin');
  214. $reader = new IniReader($this->path);
  215. $result = $reader->read('TestPlugin.acl.ini.php');
  216. $this->assertTrue(isset($result['admin']));
  217. $this->assertTrue(isset($result['paul']['groups']));
  218. $this->assertEquals('ads', $result['admin']['deny']);
  219. CakePlugin::unload();
  220. }
  221. /**
  222. * Test dump method.
  223. *
  224. * @return void
  225. */
  226. public function testDump() {
  227. $reader = new IniReader(TMP);
  228. $result = $reader->dump('test.ini', $this->testData);
  229. $this->assertTrue($result > 0);
  230. $expected = <<<INI
  231. [One]
  232. two = value
  233. three.four = value four
  234. is_null = null
  235. bool_false = false
  236. bool_true = true
  237. [Asset]
  238. timestamp = force
  239. INI;
  240. $file = TMP . 'test.ini';
  241. $result = file_get_contents($file);
  242. unlink($file);
  243. $this->assertTextEquals($expected, $result);
  244. $result = $reader->dump('test', $this->testData);
  245. $this->assertTrue($result > 0);
  246. $contents = file_get_contents($file);
  247. $this->assertTextEquals($expected, $contents);
  248. unlink($file);
  249. }
  250. /**
  251. * Test that dump() makes files read() can read.
  252. *
  253. * @return void
  254. */
  255. public function testDumpRead() {
  256. $reader = new IniReader(TMP);
  257. $reader->dump('test.ini', $this->testData);
  258. $result = $reader->read('test.ini');
  259. unlink(TMP . 'test.ini');
  260. $expected = $this->testData;
  261. $expected['One']['is_null'] = false;
  262. $this->assertEquals($expected, $result);
  263. }
  264. }