ConfigureTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. 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->assertEquals('something_else', $result);
  102. $result = Configure::read('debug');
  103. $this->assertGreaterThanOrEqual(0, $result);
  104. $result = Configure::read();
  105. $this->assertInternalType('array', $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. $writeResult = Configure::write('SomeName.someKey', 'myvalue');
  125. $this->assertTrue($writeResult);
  126. $result = Configure::read('SomeName.someKey');
  127. $this->assertEquals('myvalue', $result);
  128. $writeResult = Configure::write('SomeName.someKey', null);
  129. $this->assertTrue($writeResult);
  130. $result = Configure::read('SomeName.someKey');
  131. $this->assertNull($result);
  132. $expected = ['One' => ['Two' => ['Three' => ['Four' => ['Five' => 'cool']]]]];
  133. $writeResult = Configure::write('Key', $expected);
  134. $this->assertTrue($writeResult);
  135. $result = Configure::read('Key');
  136. $this->assertEquals($expected, $result);
  137. $result = Configure::read('Key.One');
  138. $this->assertEquals($expected['One'], $result);
  139. $result = Configure::read('Key.One.Two');
  140. $this->assertEquals($expected['One']['Two'], $result);
  141. $result = Configure::read('Key.One.Two.Three.Four.Five');
  142. $this->assertEquals('cool', $result);
  143. Configure::write('one.two.three.four', '4');
  144. $result = Configure::read('one.two.three.four');
  145. $this->assertEquals('4', $result);
  146. }
  147. /**
  148. * test setting display_errors with debug.
  149. *
  150. * @return void
  151. */
  152. public function testDebugSettingDisplayErrors()
  153. {
  154. $this->skipIf(
  155. defined('HHVM_VERSION'),
  156. 'Cannot change display_errors at runtime in HHVM'
  157. );
  158. Configure::write('debug', false);
  159. $result = ini_get('display_errors');
  160. $this->assertEquals(0, $result);
  161. Configure::write('debug', true);
  162. $result = ini_get('display_errors');
  163. $this->assertEquals(1, $result);
  164. }
  165. /**
  166. * testDelete method
  167. *
  168. * @return void
  169. */
  170. public function testDelete()
  171. {
  172. Configure::write('SomeName.someKey', 'myvalue');
  173. $result = Configure::read('SomeName.someKey');
  174. $this->assertEquals('myvalue', $result);
  175. Configure::delete('SomeName.someKey');
  176. $result = Configure::read('SomeName.someKey');
  177. $this->assertNull($result);
  178. Configure::write('SomeName', ['someKey' => 'myvalue', 'otherKey' => 'otherValue']);
  179. $result = Configure::read('SomeName.someKey');
  180. $this->assertEquals('myvalue', $result);
  181. $result = Configure::read('SomeName.otherKey');
  182. $this->assertEquals('otherValue', $result);
  183. Configure::delete('SomeName');
  184. $result = Configure::read('SomeName.someKey');
  185. $this->assertNull($result);
  186. $result = Configure::read('SomeName.otherKey');
  187. $this->assertNull($result);
  188. }
  189. /**
  190. * testCheck method
  191. *
  192. * @return void
  193. */
  194. public function testCheck()
  195. {
  196. Configure::write('ConfigureTestCase', 'value');
  197. $this->assertTrue(Configure::check('ConfigureTestCase'));
  198. $this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
  199. }
  200. /**
  201. * testCheckingSavedEmpty method
  202. *
  203. * @return void
  204. */
  205. public function testCheckingSavedEmpty()
  206. {
  207. $this->assertTrue(Configure::write('ConfigureTestCase', 0));
  208. $this->assertTrue(Configure::check('ConfigureTestCase'));
  209. $this->assertTrue(Configure::write('ConfigureTestCase', '0'));
  210. $this->assertTrue(Configure::check('ConfigureTestCase'));
  211. $this->assertTrue(Configure::write('ConfigureTestCase', false));
  212. $this->assertTrue(Configure::check('ConfigureTestCase'));
  213. $this->assertTrue(Configure::write('ConfigureTestCase', null));
  214. $this->assertFalse(Configure::check('ConfigureTestCase'));
  215. }
  216. /**
  217. * testCheckKeyWithSpaces method
  218. *
  219. * @return void
  220. */
  221. public function testCheckKeyWithSpaces()
  222. {
  223. $this->assertTrue(Configure::write('Configure Test', 'test'));
  224. $this->assertTrue(Configure::check('Configure Test'));
  225. Configure::delete('Configure Test');
  226. $this->assertTrue(Configure::write('Configure Test.Test Case', 'test'));
  227. $this->assertTrue(Configure::check('Configure Test.Test Case'));
  228. }
  229. /**
  230. * testCheckEmpty
  231. *
  232. * @return void
  233. */
  234. public function testCheckEmpty()
  235. {
  236. $this->assertFalse(Configure::check(''));
  237. $this->assertFalse(Configure::check(null));
  238. }
  239. /**
  240. * testLoad method
  241. *
  242. * @return void
  243. */
  244. public function testLoadExceptionOnNonExistentFile()
  245. {
  246. $this->expectException(\RuntimeException::class);
  247. Configure::config('test', new PhpConfig());
  248. Configure::load('non_existing_configuration_file', 'test');
  249. }
  250. /**
  251. * test load method for default config creation
  252. *
  253. * @return void
  254. */
  255. public function testLoadDefaultConfig()
  256. {
  257. try {
  258. Configure::load('non_existing_configuration_file');
  259. } catch (\Exception $e) {
  260. $result = Configure::configured('default');
  261. $this->assertTrue($result);
  262. }
  263. }
  264. /**
  265. * test load with merging
  266. *
  267. * @return void
  268. */
  269. public function testLoadWithMerge()
  270. {
  271. Configure::config('test', new PhpConfig(CONFIG));
  272. $result = Configure::load('var_test', 'test');
  273. $this->assertTrue($result);
  274. $this->assertEquals('value', Configure::read('Read'));
  275. $result = Configure::load('var_test2', 'test', true);
  276. $this->assertTrue($result);
  277. $this->assertEquals('value2', Configure::read('Read'));
  278. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  279. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
  280. $this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
  281. $this->assertEquals('one', Configure::read('TestAcl.custom'));
  282. }
  283. /**
  284. * test loading with overwrite
  285. *
  286. * @return void
  287. */
  288. public function testLoadNoMerge()
  289. {
  290. Configure::config('test', new PhpConfig(CONFIG));
  291. $result = Configure::load('var_test', 'test');
  292. $this->assertTrue($result);
  293. $this->assertEquals('value', Configure::read('Read'));
  294. $result = Configure::load('var_test2', 'test', false);
  295. $this->assertTrue($result);
  296. $this->assertEquals('value2', Configure::read('Read'));
  297. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  298. $this->assertNull(Configure::read('Deep.Deeper.Deepest'));
  299. }
  300. /**
  301. * Test load() replacing existing data
  302. *
  303. * @return void
  304. */
  305. public function testLoadWithExistingData()
  306. {
  307. Configure::config('test', new PhpConfig(CONFIG));
  308. Configure::write('my_key', 'value');
  309. Configure::load('var_test', 'test');
  310. $this->assertEquals('value', Configure::read('my_key'), 'Should not overwrite existing data.');
  311. $this->assertEquals('value', Configure::read('Read'), 'Should load new data.');
  312. }
  313. /**
  314. * Test load() merging on top of existing data
  315. *
  316. * @return void
  317. */
  318. public function testLoadMergeWithExistingData()
  319. {
  320. Configure::config('test', new PhpConfig());
  321. Configure::write('my_key', 'value');
  322. Configure::write('Read', 'old');
  323. Configure::write('Deep.old', 'old');
  324. Configure::write('TestAcl.classname', 'old');
  325. Configure::load('var_test', 'test', true);
  326. $this->assertEquals('value', Configure::read('Read'), 'Should load new data.');
  327. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'), 'Should load new data');
  328. $this->assertEquals('old', Configure::read('Deep.old'), 'Should not destroy old data.');
  329. $this->assertEquals('value', Configure::read('my_key'), 'Should not destroy data.');
  330. $this->assertEquals('Original', Configure::read('TestAcl.classname'), 'No arrays');
  331. }
  332. /**
  333. * testLoad method
  334. *
  335. * @return void
  336. */
  337. public function testLoadPlugin()
  338. {
  339. Configure::config('test', new PhpConfig());
  340. Plugin::load('TestPlugin');
  341. $result = Configure::load('TestPlugin.load', 'test');
  342. $this->assertTrue($result);
  343. $expected = '/test_app/Plugin/TestPlugin/Config/load.php';
  344. $config = Configure::read('plugin_load');
  345. $this->assertEquals($expected, $config);
  346. $result = Configure::load('TestPlugin.more.load', 'test');
  347. $this->assertTrue($result);
  348. $expected = '/test_app/Plugin/TestPlugin/Config/more.load.php';
  349. $config = Configure::read('plugin_more_load');
  350. $this->assertEquals($expected, $config);
  351. Plugin::unload();
  352. }
  353. /**
  354. * testStore method
  355. *
  356. * @return void
  357. */
  358. public function testStoreAndRestore()
  359. {
  360. Cache::enable();
  361. Cache::setConfig('configure', [
  362. 'className' => 'File',
  363. 'path' => TMP . 'tests'
  364. ]);
  365. Configure::write('Testing', 'yummy');
  366. $this->assertTrue(Configure::store('store_test', 'configure'));
  367. Configure::delete('Testing');
  368. $this->assertNull(Configure::read('Testing'));
  369. Configure::restore('store_test', 'configure');
  370. $this->assertEquals('yummy', Configure::read('Testing'));
  371. Cache::delete('store_test', 'configure');
  372. Cache::drop('configure');
  373. }
  374. /**
  375. * test that store and restore only store/restore the provided data.
  376. *
  377. * @return void
  378. */
  379. public function testStoreAndRestoreWithData()
  380. {
  381. Cache::enable();
  382. Cache::setConfig('configure', [
  383. 'className' => 'File',
  384. 'path' => TMP . 'tests'
  385. ]);
  386. Configure::write('testing', 'value');
  387. Configure::store('store_test', 'configure', ['store_test' => 'one']);
  388. Configure::delete('testing');
  389. $this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
  390. Configure::restore('store_test', 'configure');
  391. $this->assertEquals('one', Configure::read('store_test'));
  392. $this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
  393. Cache::delete('store_test', 'configure');
  394. Cache::drop('configure');
  395. }
  396. /**
  397. * testVersion method
  398. *
  399. * @return void
  400. */
  401. public function testVersion()
  402. {
  403. $result = Configure::version();
  404. $this->assertTrue(version_compare($result, '1.2', '>='));
  405. }
  406. /**
  407. * test 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::configured('test'));
  418. $this->assertFalse(Configure::configured('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. * Test that clear wipes all values.
  424. *
  425. * @return void
  426. */
  427. public function testClear()
  428. {
  429. Configure::write('test', 'value');
  430. $this->assertTrue(Configure::clear());
  431. $this->assertNull(Configure::read('debug'));
  432. $this->assertNull(Configure::read('test'));
  433. }
  434. /**
  435. * @return void
  436. */
  437. public function testDumpNoAdapter()
  438. {
  439. $this->expectException(\Cake\Core\Exception\Exception::class);
  440. Configure::dump(TMP . 'test.php', 'does_not_exist');
  441. }
  442. /**
  443. * test dump integrated with the PhpConfig.
  444. *
  445. * @return void
  446. */
  447. public function testDump()
  448. {
  449. Configure::config('test_Engine', new PhpConfig(TMP));
  450. $result = Configure::dump('config_test', 'test_Engine');
  451. $this->assertGreaterThan(0, $result);
  452. $result = file_get_contents(TMP . 'config_test.php');
  453. $this->assertContains('<?php', $result);
  454. $this->assertContains('return ', $result);
  455. if (file_exists(TMP . 'config_test.php')) {
  456. unlink(TMP . 'config_test.php');
  457. }
  458. }
  459. /**
  460. * Test dumping only some of the data.
  461. *
  462. * @return void
  463. */
  464. public function testDumpPartial()
  465. {
  466. Configure::config('test_Engine', new PhpConfig(TMP));
  467. Configure::write('Error', ['test' => 'value']);
  468. $result = Configure::dump('config_test', 'test_Engine', ['Error']);
  469. $this->assertGreaterThan(0, $result);
  470. $result = file_get_contents(TMP . 'config_test.php');
  471. $this->assertContains('<?php', $result);
  472. $this->assertContains('return ', $result);
  473. $this->assertContains('Error', $result);
  474. $this->assertNotContains('debug', $result);
  475. if (file_exists(TMP . 'config_test.php')) {
  476. unlink(TMP . 'config_test.php');
  477. }
  478. }
  479. /**
  480. * Test the consume method.
  481. *
  482. * @return void
  483. */
  484. public function testConsume()
  485. {
  486. $this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
  487. Configure::write('Test', ['key' => 'value', 'key2' => 'value2']);
  488. $result = Configure::consume('Test.key');
  489. $this->assertEquals('value', $result);
  490. $result = Configure::read('Test.key2');
  491. $this->assertEquals('value2', $result, 'Other values should remain.');
  492. $result = Configure::consume('Test');
  493. $expected = ['key2' => 'value2'];
  494. $this->assertEquals($expected, $result);
  495. }
  496. /**
  497. * testConsumeEmpty
  498. *
  499. * @return void
  500. */
  501. public function testConsumeEmpty()
  502. {
  503. Configure::write('Test', ['key' => 'value', 'key2' => 'value2']);
  504. $result = Configure::consume('');
  505. $this->assertNull($result);
  506. $result = Configure::consume(null);
  507. $this->assertNull($result);
  508. }
  509. /**
  510. * testConsumeOrFail method
  511. *
  512. * @return void
  513. */
  514. public function testConsumeOrFail()
  515. {
  516. $expected = 'ok';
  517. Configure::write('This.Key.Exists', $expected);
  518. $result = Configure::consumeOrFail('This.Key.Exists');
  519. $this->assertEquals($expected, $result);
  520. }
  521. /**
  522. * testConsumeOrFail method
  523. *
  524. * @return void
  525. */
  526. public function testConsumeOrFailThrowingException()
  527. {
  528. $this->expectException(\RuntimeException::class);
  529. $this->expectExceptionMessage('Expected configuration key "This.Key.Does.Not.exist" not found');
  530. Configure::consumeOrFail('This.Key.Does.Not.exist');
  531. }
  532. }