PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Core/ConfigureTest.php

https://gitlab.com/195/food-delivery
PHP | 518 lines | 279 code | 81 blank | 158 comment | 8 complexity | 40c06bc14d225399bc0846bd5b48073b MD5 | raw file
  1. <?php
  2. /**
  3. * ConfigureTest file
  4. *
  5. * Holds several tests
  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.Core
  17. * @since CakePHP(tm) v 1.2.0.5432
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('PhpReader', 'Configure');
  21. /**
  22. * ConfigureTest
  23. *
  24. * @package Cake.Test.Case.Core
  25. */
  26. class ConfigureTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. Configure::write('Cache.disable', true);
  35. App::build();
  36. App::objects('plugin', null, true);
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @return void
  42. */
  43. public function tearDown() {
  44. parent::tearDown();
  45. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
  46. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
  47. }
  48. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
  49. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
  50. }
  51. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
  52. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
  53. }
  54. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
  55. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
  56. }
  57. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
  58. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
  59. }
  60. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
  61. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
  62. }
  63. Configure::drop('test');
  64. }
  65. /**
  66. * Test to ensure bootrapping doesn't overwrite prior configs set under 'App' key
  67. * @return void
  68. */
  69. public function testBootstrap() {
  70. $expected = array(
  71. 'foo' => 'bar'
  72. );
  73. Configure::write('App', $expected);
  74. Configure::bootstrap(true);
  75. $result = Configure::read('App');
  76. $this->assertEquals($expected['foo'], $result['foo']);
  77. $this->assertFalse($result['base']);
  78. }
  79. /**
  80. * testRead method
  81. *
  82. * @return void
  83. */
  84. public function testRead() {
  85. $expected = 'ok';
  86. Configure::write('level1.level2.level3_1', $expected);
  87. Configure::write('level1.level2.level3_2', 'something_else');
  88. $result = Configure::read('level1.level2.level3_1');
  89. $this->assertEquals($expected, $result);
  90. $result = Configure::read('level1.level2.level3_2');
  91. $this->assertEquals('something_else', $result);
  92. $result = Configure::read('debug');
  93. $this->assertTrue($result >= 0);
  94. $result = Configure::read();
  95. $this->assertTrue(is_array($result));
  96. $this->assertTrue(isset($result['debug']));
  97. $this->assertTrue(isset($result['level1']));
  98. $result = Configure::read('something_I_just_made_up_now');
  99. $this->assertEquals(null, $result, 'Missing key should return null.');
  100. }
  101. /**
  102. * testWrite method
  103. *
  104. * @return void
  105. */
  106. public function testWrite() {
  107. $writeResult = Configure::write('SomeName.someKey', 'myvalue');
  108. $this->assertTrue($writeResult);
  109. $result = Configure::read('SomeName.someKey');
  110. $this->assertEquals('myvalue', $result);
  111. $writeResult = Configure::write('SomeName.someKey', null);
  112. $this->assertTrue($writeResult);
  113. $result = Configure::read('SomeName.someKey');
  114. $this->assertEquals(null, $result);
  115. $expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
  116. $writeResult = Configure::write('Key', $expected);
  117. $this->assertTrue($writeResult);
  118. $result = Configure::read('Key');
  119. $this->assertEquals($expected, $result);
  120. $result = Configure::read('Key.One');
  121. $this->assertEquals($expected['One'], $result);
  122. $result = Configure::read('Key.One.Two');
  123. $this->assertEquals($expected['One']['Two'], $result);
  124. $result = Configure::read('Key.One.Two.Three.Four.Five');
  125. $this->assertEquals('cool', $result);
  126. Configure::write('one.two.three.four', '4');
  127. $result = Configure::read('one.two.three.four');
  128. $this->assertEquals('4', $result);
  129. }
  130. /**
  131. * Test the consume method.
  132. *
  133. * @return void
  134. */
  135. public function testConsume() {
  136. $this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
  137. Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
  138. $result = Configure::consume('Test.key');
  139. $this->assertEquals('value', $result);
  140. $result = Configure::read('Test.key2');
  141. $this->assertEquals('value2', $result, 'Other values should remain.');
  142. $result = Configure::consume('Test');
  143. $expected = array('key2' => 'value2');
  144. $this->assertEquals($expected, $result);
  145. }
  146. /**
  147. * testConsumeEmpty
  148. *
  149. * @return void
  150. */
  151. public function testConsumeEmpty() {
  152. Configure::write('Test', array('key' => 'value', 'key2' => 'value2'));
  153. $result = Configure::consume('');
  154. $this->assertNull($result);
  155. $result = Configure::consume(null);
  156. $this->assertNull($result);
  157. }
  158. /**
  159. * test setting display_errors with debug.
  160. *
  161. * @return void
  162. */
  163. public function testDebugSettingDisplayErrors() {
  164. Configure::write('debug', 0);
  165. $result = ini_get('display_errors');
  166. $this->assertEquals(0, $result);
  167. Configure::write('debug', 2);
  168. $result = ini_get('display_errors');
  169. $this->assertEquals(1, $result);
  170. }
  171. /**
  172. * testDelete method
  173. *
  174. * @return void
  175. */
  176. public function testDelete() {
  177. Configure::write('SomeName.someKey', 'myvalue');
  178. $result = Configure::read('SomeName.someKey');
  179. $this->assertEquals('myvalue', $result);
  180. Configure::delete('SomeName.someKey');
  181. $result = Configure::read('SomeName.someKey');
  182. $this->assertNull($result);
  183. Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
  184. $result = Configure::read('SomeName.someKey');
  185. $this->assertEquals('myvalue', $result);
  186. $result = Configure::read('SomeName.otherKey');
  187. $this->assertEquals('otherValue', $result);
  188. Configure::delete('SomeName');
  189. $result = Configure::read('SomeName.someKey');
  190. $this->assertNull($result);
  191. $result = Configure::read('SomeName.otherKey');
  192. $this->assertNull($result);
  193. }
  194. /**
  195. * testCheck method
  196. *
  197. * @return void
  198. */
  199. public function testCheck() {
  200. Configure::write('ConfigureTestCase', 'value');
  201. $this->assertTrue(Configure::check('ConfigureTestCase'));
  202. $this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
  203. }
  204. /**
  205. * testCheckingSavedEmpty method
  206. *
  207. * @return void
  208. */
  209. public function testCheckingSavedEmpty() {
  210. $this->assertTrue(Configure::write('ConfigureTestCase', 0));
  211. $this->assertTrue(Configure::check('ConfigureTestCase'));
  212. $this->assertTrue(Configure::write('ConfigureTestCase', '0'));
  213. $this->assertTrue(Configure::check('ConfigureTestCase'));
  214. $this->assertTrue(Configure::write('ConfigureTestCase', false));
  215. $this->assertTrue(Configure::check('ConfigureTestCase'));
  216. $this->assertTrue(Configure::write('ConfigureTestCase', null));
  217. $this->assertFalse(Configure::check('ConfigureTestCase'));
  218. }
  219. /**
  220. * testCheckKeyWithSpaces method
  221. *
  222. * @return void
  223. */
  224. public function testCheckKeyWithSpaces() {
  225. $this->assertTrue(Configure::write('Configure Test', "test"));
  226. $this->assertTrue(Configure::check('Configure Test'));
  227. Configure::delete('Configure Test');
  228. $this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
  229. $this->assertTrue(Configure::check('Configure Test.Test Case'));
  230. }
  231. /**
  232. * testCheckEmpty
  233. *
  234. * @return void
  235. */
  236. public function testCheckEmpty() {
  237. $this->assertFalse(Configure::check(''));
  238. $this->assertFalse(Configure::check(null));
  239. }
  240. /**
  241. * testLoad method
  242. *
  243. * @expectedException RuntimeException
  244. * @return void
  245. */
  246. public function testLoadExceptionOnNonExistantFile() {
  247. Configure::config('test', new PhpReader());
  248. Configure::load('non_existing_configuration_file', 'test');
  249. }
  250. /**
  251. * test load method for default config creation
  252. *
  253. * @return void
  254. */
  255. public function testLoadDefaultConfig() {
  256. try {
  257. Configure::load('non_existing_configuration_file');
  258. } catch (Exception $e) {
  259. $result = Configure::configured('default');
  260. $this->assertTrue($result);
  261. }
  262. }
  263. /**
  264. * test load with merging
  265. *
  266. * @return void
  267. */
  268. public function testLoadWithMerge() {
  269. Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
  270. $result = Configure::load('var_test', 'test');
  271. $this->assertTrue($result);
  272. $this->assertEquals('value', Configure::read('Read'));
  273. $result = Configure::load('var_test2', 'test', true);
  274. $this->assertTrue($result);
  275. $this->assertEquals('value2', Configure::read('Read'));
  276. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  277. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
  278. $this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
  279. $this->assertEquals('one', Configure::read('TestAcl.custom'));
  280. }
  281. /**
  282. * test loading with overwrite
  283. *
  284. * @return void
  285. */
  286. public function testLoadNoMerge() {
  287. Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
  288. $result = Configure::load('var_test', 'test');
  289. $this->assertTrue($result);
  290. $this->assertEquals('value', Configure::read('Read'));
  291. $result = Configure::load('var_test2', 'test', false);
  292. $this->assertTrue($result);
  293. $this->assertEquals('value2', Configure::read('Read'));
  294. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  295. $this->assertNull(Configure::read('Deep.Deeper.Deepest'));
  296. }
  297. /**
  298. * testLoad method
  299. *
  300. * @return void
  301. */
  302. public function testLoadPlugin() {
  303. App::build(array(
  304. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  305. ), App::RESET);
  306. Configure::config('test', new PhpReader());
  307. CakePlugin::load('TestPlugin');
  308. $result = Configure::load('TestPlugin.load', 'test');
  309. $this->assertTrue($result);
  310. $expected = '/test_app/plugins/test_plugin/config/load.php';
  311. $config = Configure::read('plugin_load');
  312. $this->assertEquals($expected, $config);
  313. $result = Configure::load('TestPlugin.more.load', 'test');
  314. $this->assertTrue($result);
  315. $expected = '/test_app/plugins/test_plugin/config/more.load.php';
  316. $config = Configure::read('plugin_more_load');
  317. $this->assertEquals($expected, $config);
  318. CakePlugin::unload();
  319. }
  320. /**
  321. * testStore method
  322. *
  323. * @return void
  324. */
  325. public function testStoreAndRestore() {
  326. Configure::write('Cache.disable', false);
  327. Configure::write('Testing', 'yummy');
  328. $this->assertTrue(Configure::store('store_test', 'default'));
  329. Configure::delete('Testing');
  330. $this->assertNull(Configure::read('Testing'));
  331. Configure::restore('store_test', 'default');
  332. $this->assertEquals('yummy', Configure::read('Testing'));
  333. Cache::delete('store_test', 'default');
  334. }
  335. /**
  336. * test that store and restore only store/restore the provided data.
  337. *
  338. * @return void
  339. */
  340. public function testStoreAndRestoreWithData() {
  341. Configure::write('Cache.disable', false);
  342. Configure::write('testing', 'value');
  343. Configure::store('store_test', 'default', array('store_test' => 'one'));
  344. Configure::delete('testing');
  345. $this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
  346. Configure::restore('store_test', 'default');
  347. $this->assertEquals('one', Configure::read('store_test'));
  348. $this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
  349. Cache::delete('store_test', 'default');
  350. }
  351. /**
  352. * testVersion method
  353. *
  354. * @return void
  355. */
  356. public function testVersion() {
  357. $result = Configure::version();
  358. $this->assertTrue(version_compare($result, '1.2', '>='));
  359. }
  360. /**
  361. * test adding new readers.
  362. *
  363. * @return void
  364. */
  365. public function testReaderSetup() {
  366. $reader = new PhpReader();
  367. Configure::config('test', $reader);
  368. $configured = Configure::configured();
  369. $this->assertTrue(in_array('test', $configured));
  370. $this->assertTrue(Configure::configured('test'));
  371. $this->assertFalse(Configure::configured('fake_garbage'));
  372. $this->assertTrue(Configure::drop('test'));
  373. $this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
  374. }
  375. /**
  376. * test reader() throwing exceptions on missing interface.
  377. *
  378. * @expectedException PHPUnit_Framework_Error
  379. * @return void
  380. */
  381. public function testReaderExceptionOnIncorrectClass() {
  382. $reader = new StdClass();
  383. Configure::config('test', $reader);
  384. }
  385. /**
  386. * Test that clear wipes all values.
  387. *
  388. * @return void
  389. */
  390. public function testClear() {
  391. Configure::write('test', 'value');
  392. $this->assertTrue(Configure::clear());
  393. $this->assertNull(Configure::read('debug'));
  394. $this->assertNull(Configure::read('test'));
  395. }
  396. /**
  397. * @expectedException ConfigureException
  398. * @return void
  399. */
  400. public function testDumpNoAdapter() {
  401. Configure::dump(TMP . 'test.php', 'does_not_exist');
  402. }
  403. /**
  404. * test dump integrated with the PhpReader.
  405. *
  406. * @return void
  407. */
  408. public function testDump() {
  409. Configure::config('test_reader', new PhpReader(TMP));
  410. $result = Configure::dump('config_test.php', 'test_reader');
  411. $this->assertTrue($result > 0);
  412. $result = file_get_contents(TMP . 'config_test.php');
  413. $this->assertContains('<?php', $result);
  414. $this->assertContains('$config = ', $result);
  415. if (file_exists(TMP . 'config_test.php')) {
  416. unlink(TMP . 'config_test.php');
  417. }
  418. }
  419. /**
  420. * Test dumping only some of the data.
  421. *
  422. * @return void
  423. */
  424. public function testDumpPartial() {
  425. Configure::config('test_reader', new PhpReader(TMP));
  426. $result = Configure::dump('config_test.php', 'test_reader', array('Error'));
  427. $this->assertTrue($result > 0);
  428. $result = file_get_contents(TMP . 'config_test.php');
  429. $this->assertContains('<?php', $result);
  430. $this->assertContains('$config = ', $result);
  431. $this->assertContains('Error', $result);
  432. $this->assertNotContains('debug', $result);
  433. if (file_exists(TMP . 'config_test.php')) {
  434. unlink(TMP . 'config_test.php');
  435. }
  436. }
  437. }