ConfigureTest.php 17 KB

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