ConfigureTest.php 19 KB

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