ConfigureTest.php 17 KB

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