ConfigureTest.php 18 KB

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