ConfigureTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Cache\Cache;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Configure\Engine\PhpConfig;
  20. use Cake\Core\Plugin;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * ConfigureTest
  24. *
  25. */
  26. class ConfigureTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. Cache::disable();
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @return void
  42. */
  43. public function tearDown()
  44. {
  45. parent::tearDown();
  46. if (file_exists(TMP . 'cache/persistent/cake_core_core_paths')) {
  47. unlink(TMP . 'cache/persistent/cake_core_core_paths');
  48. }
  49. if (file_exists(TMP . 'cache/persistent/cake_core_dir_map')) {
  50. unlink(TMP . 'cache/persistent/cake_core_dir_map');
  51. }
  52. if (file_exists(TMP . 'cache/persistent/cake_core_file_map')) {
  53. unlink(TMP . 'cache/persistent/cake_core_file_map');
  54. }
  55. if (file_exists(TMP . 'cache/persistent/cake_core_object_map')) {
  56. unlink(TMP . 'cache/persistent/cake_core_object_map');
  57. }
  58. if (file_exists(TMP . 'cache/persistent/test.config.php')) {
  59. unlink(TMP . 'cache/persistent/test.config.php');
  60. }
  61. if (file_exists(TMP . 'cache/persistent/test.php')) {
  62. unlink(TMP . 'cache/persistent/test.php');
  63. }
  64. Configure::drop('test');
  65. }
  66. /**
  67. * testRead method
  68. *
  69. * @return void
  70. */
  71. public function testRead()
  72. {
  73. $expected = 'ok';
  74. Configure::write('level1.level2.level3_1', $expected);
  75. Configure::write('level1.level2.level3_2', 'something_else');
  76. $result = Configure::read('level1.level2.level3_1');
  77. $this->assertEquals($expected, $result);
  78. $result = Configure::read('level1.level2.level3_2');
  79. $this->assertEquals('something_else', $result);
  80. $result = Configure::read('debug');
  81. $this->assertTrue($result >= 0);
  82. $result = Configure::read();
  83. $this->assertTrue(is_array($result));
  84. $this->assertTrue(isset($result['debug']));
  85. $this->assertTrue(isset($result['level1']));
  86. $result = Configure::read('something_I_just_made_up_now');
  87. $this->assertEquals(null, $result, 'Missing key should return null.');
  88. }
  89. /**
  90. * testWrite method
  91. *
  92. * @return void
  93. */
  94. public function testWrite()
  95. {
  96. $writeResult = Configure::write('SomeName.someKey', 'myvalue');
  97. $this->assertTrue($writeResult);
  98. $result = Configure::read('SomeName.someKey');
  99. $this->assertEquals('myvalue', $result);
  100. $writeResult = Configure::write('SomeName.someKey', null);
  101. $this->assertTrue($writeResult);
  102. $result = Configure::read('SomeName.someKey');
  103. $this->assertEquals(null, $result);
  104. $expected = ['One' => ['Two' => ['Three' => ['Four' => ['Five' => 'cool']]]]];
  105. $writeResult = Configure::write('Key', $expected);
  106. $this->assertTrue($writeResult);
  107. $result = Configure::read('Key');
  108. $this->assertEquals($expected, $result);
  109. $result = Configure::read('Key.One');
  110. $this->assertEquals($expected['One'], $result);
  111. $result = Configure::read('Key.One.Two');
  112. $this->assertEquals($expected['One']['Two'], $result);
  113. $result = Configure::read('Key.One.Two.Three.Four.Five');
  114. $this->assertEquals('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. {
  126. $this->skipIf(
  127. defined('HHVM_VERSION'),
  128. 'Cannot change display_errors at runtime in HHVM'
  129. );
  130. Configure::write('debug', false);
  131. $result = ini_get('display_errors');
  132. $this->assertEquals(0, $result);
  133. Configure::write('debug', true);
  134. $result = ini_get('display_errors');
  135. $this->assertEquals(1, $result);
  136. }
  137. /**
  138. * testDelete method
  139. *
  140. * @return void
  141. */
  142. public function testDelete()
  143. {
  144. Configure::write('SomeName.someKey', 'myvalue');
  145. $result = Configure::read('SomeName.someKey');
  146. $this->assertEquals('myvalue', $result);
  147. Configure::delete('SomeName.someKey');
  148. $result = Configure::read('SomeName.someKey');
  149. $this->assertTrue($result === null);
  150. Configure::write('SomeName', ['someKey' => 'myvalue', 'otherKey' => 'otherValue']);
  151. $result = Configure::read('SomeName.someKey');
  152. $this->assertEquals('myvalue', $result);
  153. $result = Configure::read('SomeName.otherKey');
  154. $this->assertEquals('otherValue', $result);
  155. Configure::delete('SomeName');
  156. $result = Configure::read('SomeName.someKey');
  157. $this->assertTrue($result === null);
  158. $result = Configure::read('SomeName.otherKey');
  159. $this->assertTrue($result === null);
  160. }
  161. /**
  162. * testCheck method
  163. *
  164. * @return void
  165. */
  166. public function testCheck()
  167. {
  168. Configure::write('ConfigureTestCase', 'value');
  169. $this->assertTrue(Configure::check('ConfigureTestCase'));
  170. $this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
  171. }
  172. /**
  173. * testCheckingSavedEmpty method
  174. *
  175. * @return void
  176. */
  177. public function testCheckingSavedEmpty()
  178. {
  179. $this->assertTrue(Configure::write('ConfigureTestCase', 0));
  180. $this->assertTrue(Configure::check('ConfigureTestCase'));
  181. $this->assertTrue(Configure::write('ConfigureTestCase', '0'));
  182. $this->assertTrue(Configure::check('ConfigureTestCase'));
  183. $this->assertTrue(Configure::write('ConfigureTestCase', false));
  184. $this->assertTrue(Configure::check('ConfigureTestCase'));
  185. $this->assertTrue(Configure::write('ConfigureTestCase', null));
  186. $this->assertFalse(Configure::check('ConfigureTestCase'));
  187. }
  188. /**
  189. * testCheckKeyWithSpaces method
  190. *
  191. * @return void
  192. */
  193. public function testCheckKeyWithSpaces()
  194. {
  195. $this->assertTrue(Configure::write('Configure Test', "test"));
  196. $this->assertTrue(Configure::check('Configure Test'));
  197. Configure::delete('Configure Test');
  198. $this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
  199. $this->assertTrue(Configure::check('Configure Test.Test Case'));
  200. }
  201. /**
  202. * testCheckEmpty
  203. *
  204. * @return void
  205. */
  206. public function testCheckEmpty()
  207. {
  208. $this->assertFalse(Configure::check(''));
  209. $this->assertFalse(Configure::check(null));
  210. }
  211. /**
  212. * testLoad method
  213. *
  214. * @expectedException \RuntimeException
  215. * @return void
  216. */
  217. public function testLoadExceptionOnNonExistantFile()
  218. {
  219. Configure::config('test', new PhpConfig());
  220. Configure::load('non_existing_configuration_file', 'test');
  221. }
  222. /**
  223. * test load method for default config creation
  224. *
  225. * @return void
  226. */
  227. public function testLoadDefaultConfig()
  228. {
  229. try {
  230. Configure::load('non_existing_configuration_file');
  231. } catch (\Exception $e) {
  232. $result = Configure::configured('default');
  233. $this->assertTrue($result);
  234. }
  235. }
  236. /**
  237. * test load with merging
  238. *
  239. * @return void
  240. */
  241. public function testLoadWithMerge()
  242. {
  243. Configure::config('test', new PhpConfig(CONFIG));
  244. $result = Configure::load('var_test', 'test');
  245. $this->assertTrue($result);
  246. $this->assertEquals('value', Configure::read('Read'));
  247. $result = Configure::load('var_test2', 'test', true);
  248. $this->assertTrue($result);
  249. $this->assertEquals('value2', Configure::read('Read'));
  250. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  251. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
  252. $this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
  253. $this->assertEquals('one', Configure::read('TestAcl.custom'));
  254. }
  255. /**
  256. * test loading with overwrite
  257. *
  258. * @return void
  259. */
  260. public function testLoadNoMerge()
  261. {
  262. Configure::config('test', new PhpConfig(CONFIG));
  263. $result = Configure::load('var_test', 'test');
  264. $this->assertTrue($result);
  265. $this->assertEquals('value', Configure::read('Read'));
  266. $result = Configure::load('var_test2', 'test', false);
  267. $this->assertTrue($result);
  268. $this->assertEquals('value2', Configure::read('Read'));
  269. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  270. $this->assertNull(Configure::read('Deep.Deeper.Deepest'));
  271. }
  272. /**
  273. * Test load() replacing existing data
  274. *
  275. * @return void
  276. */
  277. public function testLoadWithExistingData()
  278. {
  279. Configure::config('test', new PhpConfig(CONFIG));
  280. Configure::write('my_key', 'value');
  281. Configure::load('var_test', 'test');
  282. $this->assertEquals('value', Configure::read('my_key'), 'Should not overwrite existing data.');
  283. $this->assertEquals('value', Configure::read('Read'), 'Should load new data.');
  284. }
  285. /**
  286. * Test load() merging on top of existing data
  287. *
  288. * @return void
  289. */
  290. public function testLoadMergeWithExistingData()
  291. {
  292. Configure::config('test', new PhpConfig());
  293. Configure::write('my_key', 'value');
  294. Configure::write('Read', 'old');
  295. Configure::write('Deep.old', 'old');
  296. Configure::write('TestAcl.classname', 'old');
  297. Configure::load('var_test', 'test', true);
  298. $this->assertEquals('value', Configure::read('Read'), 'Should load new data.');
  299. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'), 'Should load new data');
  300. $this->assertEquals('old', Configure::read('Deep.old'), 'Should not destroy old data.');
  301. $this->assertEquals('value', Configure::read('my_key'), 'Should not destroy data.');
  302. $this->assertEquals('Original', Configure::read('TestAcl.classname'), 'No arrays');
  303. }
  304. /**
  305. * testLoad method
  306. *
  307. * @return void
  308. */
  309. public function testLoadPlugin()
  310. {
  311. Configure::config('test', new PhpConfig());
  312. Plugin::load('TestPlugin');
  313. $result = Configure::load('TestPlugin.load', 'test');
  314. $this->assertTrue($result);
  315. $expected = '/test_app/Plugin/TestPlugin/Config/load.php';
  316. $config = Configure::read('plugin_load');
  317. $this->assertEquals($expected, $config);
  318. $result = Configure::load('TestPlugin.more.load', 'test');
  319. $this->assertTrue($result);
  320. $expected = '/test_app/Plugin/TestPlugin/Config/more.load.php';
  321. $config = Configure::read('plugin_more_load');
  322. $this->assertEquals($expected, $config);
  323. Plugin::unload();
  324. }
  325. /**
  326. * testStore method
  327. *
  328. * @return void
  329. */
  330. public function testStoreAndRestore()
  331. {
  332. Cache::enable();
  333. Cache::config('configure', [
  334. 'className' => 'File',
  335. 'path' => TMP . 'tests'
  336. ]);
  337. Configure::write('Testing', 'yummy');
  338. $this->assertTrue(Configure::store('store_test', 'configure'));
  339. Configure::delete('Testing');
  340. $this->assertNull(Configure::read('Testing'));
  341. Configure::restore('store_test', 'configure');
  342. $this->assertEquals('yummy', Configure::read('Testing'));
  343. Cache::delete('store_test', 'configure');
  344. Cache::drop('configure');
  345. }
  346. /**
  347. * test that store and restore only store/restore the provided data.
  348. *
  349. * @return void
  350. */
  351. public function testStoreAndRestoreWithData()
  352. {
  353. Cache::enable();
  354. Cache::config('configure', [
  355. 'className' => 'File',
  356. 'path' => TMP . 'tests'
  357. ]);
  358. Configure::write('testing', 'value');
  359. Configure::store('store_test', 'configure', ['store_test' => 'one']);
  360. Configure::delete('testing');
  361. $this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
  362. Configure::restore('store_test', 'configure');
  363. $this->assertEquals('one', Configure::read('store_test'));
  364. $this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
  365. Cache::delete('store_test', 'configure');
  366. Cache::drop('configure');
  367. }
  368. /**
  369. * testVersion method
  370. *
  371. * @return void
  372. */
  373. public function testVersion()
  374. {
  375. $result = Configure::version();
  376. $this->assertTrue(version_compare($result, '1.2', '>='));
  377. }
  378. /**
  379. * test adding new engines.
  380. *
  381. * @return void
  382. */
  383. public function testEngineSetup()
  384. {
  385. $engine = new PhpConfig();
  386. Configure::config('test', $engine);
  387. $configured = Configure::configured();
  388. $this->assertTrue(in_array('test', $configured));
  389. $this->assertTrue(Configure::configured('test'));
  390. $this->assertFalse(Configure::configured('fake_garbage'));
  391. $this->assertTrue(Configure::drop('test'));
  392. $this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
  393. }
  394. /**
  395. * Test that clear wipes all values.
  396. *
  397. * @return void
  398. */
  399. public function testClear()
  400. {
  401. Configure::write('test', 'value');
  402. $this->assertTrue(Configure::clear());
  403. $this->assertNull(Configure::read('debug'));
  404. $this->assertNull(Configure::read('test'));
  405. }
  406. /**
  407. * @expectedException \Cake\Core\Exception\Exception
  408. * @return void
  409. */
  410. public function testDumpNoAdapter()
  411. {
  412. Configure::dump(TMP . 'test.php', 'does_not_exist');
  413. }
  414. /**
  415. * test dump integrated with the PhpConfig.
  416. *
  417. * @return void
  418. */
  419. public function testDump()
  420. {
  421. Configure::config('test_Engine', new PhpConfig(TMP));
  422. $result = Configure::dump('config_test', 'test_Engine');
  423. $this->assertTrue($result > 0);
  424. $result = file_get_contents(TMP . 'config_test.php');
  425. $this->assertContains('<?php', $result);
  426. $this->assertContains('return ', $result);
  427. if (file_exists(TMP . 'config_test.php')) {
  428. unlink(TMP . 'config_test.php');
  429. }
  430. }
  431. /**
  432. * Test dumping only some of the data.
  433. *
  434. * @return void
  435. */
  436. public function testDumpPartial()
  437. {
  438. Configure::config('test_Engine', new PhpConfig(TMP));
  439. Configure::write('Error', ['test' => 'value']);
  440. $result = Configure::dump('config_test', 'test_Engine', ['Error']);
  441. $this->assertTrue($result > 0);
  442. $result = file_get_contents(TMP . 'config_test.php');
  443. $this->assertContains('<?php', $result);
  444. $this->assertContains('return ', $result);
  445. $this->assertContains('Error', $result);
  446. $this->assertNotContains('debug', $result);
  447. if (file_exists(TMP . 'config_test.php')) {
  448. unlink(TMP . 'config_test.php');
  449. }
  450. }
  451. /**
  452. * Test the consume method.
  453. *
  454. * @return void
  455. */
  456. public function testConsume()
  457. {
  458. $this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
  459. Configure::write('Test', ['key' => 'value', 'key2' => 'value2']);
  460. $result = Configure::consume('Test.key');
  461. $this->assertEquals('value', $result);
  462. $result = Configure::read('Test.key2');
  463. $this->assertEquals('value2', $result, 'Other values should remain.');
  464. $result = Configure::consume('Test');
  465. $expected = ['key2' => 'value2'];
  466. $this->assertEquals($expected, $result);
  467. }
  468. /**
  469. * testConsumeEmpty
  470. *
  471. * @return void
  472. */
  473. public function testConsumeEmpty()
  474. {
  475. Configure::write('Test', ['key' => 'value', 'key2' => 'value2']);
  476. $result = Configure::consume('');
  477. $this->assertNull($result);
  478. $result = Configure::consume(null);
  479. $this->assertNull($result);
  480. }
  481. }