ConfigureTest.php 17 KB

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