ConfigureTest.php 18 KB

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