ConfigureTest.php 18 KB

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