ConfigureTest.php 17 KB

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