ConfigureTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * ConfigureTest file
  4. *
  5. * Holds several tests
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package Cake.Test.Case.Core
  18. * @since CakePHP(tm) v 1.2.0.5432
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('PhpReader', 'Configure');
  22. /**
  23. * ConfigureTest
  24. *
  25. * @package Cake.Test.Case.Core
  26. */
  27. class ConfigureTest extends CakeTestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. $this->_cacheDisable = Configure::read('Cache.disable');
  35. $this->_debug = Configure::read('debug');
  36. Configure::write('Cache.disable', true);
  37. App::build();
  38. App::objects('plugin', null, true);
  39. }
  40. /**
  41. * tearDown method
  42. *
  43. * @return void
  44. */
  45. public function tearDown() {
  46. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
  47. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
  48. }
  49. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
  50. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
  51. }
  52. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
  53. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
  54. }
  55. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
  56. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
  57. }
  58. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
  59. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
  60. }
  61. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
  62. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
  63. }
  64. Configure::write('debug', $this->_debug);
  65. Configure::write('Cache.disable', $this->_cacheDisable);
  66. Configure::drop('test');
  67. }
  68. /**
  69. * testRead method
  70. *
  71. * @return void
  72. */
  73. public function testRead() {
  74. $expected = 'ok';
  75. Configure::write('level1.level2.level3_1', $expected);
  76. Configure::write('level1.level2.level3_2', 'something_else');
  77. $result = Configure::read('level1.level2.level3_1');
  78. $this->assertEqual($expected, $result);
  79. $result = Configure::read('level1.level2.level3_2');
  80. $this->assertEqual($result, 'something_else');
  81. $result = Configure::read('debug');
  82. $this->assertTrue($result >= 0);
  83. $result = Configure::read();
  84. $this->assertTrue(is_array($result));
  85. $this->assertTrue(isset($result['debug']));
  86. $this->assertTrue(isset($result['level1']));
  87. $result = Configure::read('something_I_just_made_up_now');
  88. $this->assertEquals(null, $result, 'Missing key should return null.');
  89. }
  90. /**
  91. * testWrite method
  92. *
  93. * @return void
  94. */
  95. public function testWrite() {
  96. $writeResult = Configure::write('SomeName.someKey', 'myvalue');
  97. $this->assertTrue($writeResult);
  98. $result = Configure::read('SomeName.someKey');
  99. $this->assertEqual($result, 'myvalue');
  100. $writeResult = Configure::write('SomeName.someKey', null);
  101. $this->assertTrue($writeResult);
  102. $result = Configure::read('SomeName.someKey');
  103. $this->assertEqual($result, null);
  104. $expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
  105. $writeResult = Configure::write('Key', $expected);
  106. $this->assertTrue($writeResult);
  107. $result = Configure::read('Key');
  108. $this->assertEqual($expected, $result);
  109. $result = Configure::read('Key.One');
  110. $this->assertEqual($expected['One'], $result);
  111. $result = Configure::read('Key.One.Two');
  112. $this->assertEqual($expected['One']['Two'], $result);
  113. $result = Configure::read('Key.One.Two.Three.Four.Five');
  114. $this->assertEqual('cool', $result);
  115. Configure::write('one.two.three.four', '4');
  116. $result = Configure::read('one.two.three.four');
  117. $this->assertEquals('4', $result);
  118. }
  119. /**
  120. * test setting display_errors with debug.
  121. *
  122. * @return void
  123. */
  124. public function testDebugSettingDisplayErrors() {
  125. Configure::write('debug', 0);
  126. $result = ini_get('display_errors');
  127. $this->assertEqual($result, 0);
  128. Configure::write('debug', 2);
  129. $result = ini_get('display_errors');
  130. $this->assertEqual($result, 1);
  131. }
  132. /**
  133. * testDelete method
  134. *
  135. * @return void
  136. */
  137. public function testDelete() {
  138. Configure::write('SomeName.someKey', 'myvalue');
  139. $result = Configure::read('SomeName.someKey');
  140. $this->assertEqual($result, 'myvalue');
  141. Configure::delete('SomeName.someKey');
  142. $result = Configure::read('SomeName.someKey');
  143. $this->assertTrue($result === null);
  144. Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
  145. $result = Configure::read('SomeName.someKey');
  146. $this->assertEqual($result, 'myvalue');
  147. $result = Configure::read('SomeName.otherKey');
  148. $this->assertEqual($result, 'otherValue');
  149. Configure::delete('SomeName');
  150. $result = Configure::read('SomeName.someKey');
  151. $this->assertTrue($result === null);
  152. $result = Configure::read('SomeName.otherKey');
  153. $this->assertTrue($result === null);
  154. }
  155. /**
  156. * testLoad method
  157. *
  158. * @expectedException RuntimeException
  159. * @return void
  160. */
  161. public function testLoadExceptionOnNonExistantFile() {
  162. Configure::config('test', new PhpReader());
  163. $result = Configure::load('non_existing_configuration_file', 'test');
  164. }
  165. /**
  166. * test load method for default config creation
  167. *
  168. * @return void
  169. */
  170. public function testLoadDefaultConfig() {
  171. try {
  172. Configure::load('non_existing_configuration_file');
  173. } catch (Exception $e) {
  174. $result = Configure::configured('default');
  175. $this->assertTrue($result);
  176. }
  177. }
  178. /**
  179. * test load with merging
  180. *
  181. * @return void
  182. */
  183. public function testLoadWithMerge() {
  184. Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
  185. $result = Configure::load('var_test', 'test');
  186. $this->assertTrue($result);
  187. $this->assertEquals('value', Configure::read('Read'));
  188. $result = Configure::load('var_test2', 'test', true);
  189. $this->assertTrue($result);
  190. $this->assertEquals('value2', Configure::read('Read'));
  191. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  192. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
  193. }
  194. /**
  195. * test loading with overwrite
  196. *
  197. * @return void
  198. */
  199. public function testLoadNoMerge() {
  200. Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
  201. $result = Configure::load('var_test', 'test');
  202. $this->assertTrue($result);
  203. $this->assertEquals('value', Configure::read('Read'));
  204. $result = Configure::load('var_test2', 'test', false);
  205. $this->assertTrue($result);
  206. $this->assertEquals('value2', Configure::read('Read'));
  207. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  208. $this->assertNull(Configure::read('Deep.Deeper.Deepest'));
  209. }
  210. /**
  211. * testLoad method
  212. *
  213. * @return void
  214. */
  215. public function testLoadPlugin() {
  216. App::build(array('plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)), true);
  217. Configure::config('test', new PhpReader());
  218. CakePlugin::load('TestPlugin');
  219. $result = Configure::load('TestPlugin.load', 'test');
  220. $this->assertTrue($result);
  221. $expected = '/test_app/plugins/test_plugin/config/load.php';
  222. $config = Configure::read('plugin_load');
  223. $this->assertEqual($config, $expected);
  224. $result = Configure::load('TestPlugin.more.load', 'test');
  225. $this->assertTrue($result);
  226. $expected = '/test_app/plugins/test_plugin/config/more.load.php';
  227. $config = Configure::read('plugin_more_load');
  228. $this->assertEqual($config, $expected);
  229. CakePlugin::unload();
  230. }
  231. /**
  232. * testStore method
  233. *
  234. * @return void
  235. */
  236. public function testStoreAndRestore() {
  237. Configure::write('Cache.disable', false);
  238. Configure::write('Testing', 'yummy');
  239. $this->assertTrue(Configure::store('store_test', 'default'));
  240. Configure::delete('Testing');
  241. $this->assertNull(Configure::read('Testing'));
  242. Configure::restore('store_test', 'default');
  243. $this->assertEquals('yummy', Configure::read('Testing'));
  244. Cache::delete('store_test', 'default');
  245. }
  246. /**
  247. * test that store and restore only store/restore the provided data.
  248. *
  249. * @return void
  250. */
  251. public function testStoreAndRestoreWithData() {
  252. Configure::write('Cache.disable', false);
  253. Configure::write('testing', 'value');
  254. Configure::store('store_test', 'default', array('store_test' => 'one'));
  255. Configure::delete('testing');
  256. $this->assertNull(Configure::read('store_test'), 'Calling store with data shouldnt modify runtime.');
  257. Configure::restore('store_test', 'default');
  258. $this->assertEquals('one', Configure::read('store_test'));
  259. $this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
  260. Cache::delete('store_test', 'default');
  261. }
  262. /**
  263. * testVersion method
  264. *
  265. * @return void
  266. */
  267. public function testVersion() {
  268. $result = Configure::version();
  269. $this->assertTrue(version_compare($result, '1.2', '>='));
  270. }
  271. /**
  272. * test adding new readers.
  273. *
  274. * @return void
  275. */
  276. public function testReaderSetup() {
  277. $reader = new PhpReader();
  278. Configure::config('test', $reader);
  279. $configured = Configure::configured();
  280. $this->assertTrue(in_array('test', $configured));
  281. $this->assertTrue(Configure::configured('test'));
  282. $this->assertFalse(Configure::configured('fake_garbage'));
  283. $this->assertTrue(Configure::drop('test'));
  284. $this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
  285. }
  286. /**
  287. * test reader() throwing exceptions on missing interface.
  288. *
  289. * @expectedException Exception
  290. * @return void
  291. */
  292. public function testReaderExceptionOnIncorrectClass() {
  293. $reader = new StdClass();
  294. Configure::config('test', $reader);
  295. }
  296. }