ConfigureTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. }
  206. /**
  207. * testLoad method
  208. *
  209. * @expectedException \RuntimeException
  210. * @return void
  211. */
  212. public function testLoadExceptionOnNonExistantFile()
  213. {
  214. Configure::config('test', new PhpConfig());
  215. Configure::load('non_existing_configuration_file', 'test');
  216. }
  217. /**
  218. * test load method for default config creation
  219. *
  220. * @return void
  221. */
  222. public function testLoadDefaultConfig()
  223. {
  224. try {
  225. Configure::load('non_existing_configuration_file');
  226. } catch (\Exception $e) {
  227. $result = Configure::configured('default');
  228. $this->assertTrue($result);
  229. }
  230. }
  231. /**
  232. * test load with merging
  233. *
  234. * @return void
  235. */
  236. public function testLoadWithMerge()
  237. {
  238. Configure::config('test', new PhpConfig(CONFIG));
  239. $result = Configure::load('var_test', 'test');
  240. $this->assertTrue($result);
  241. $this->assertEquals('value', Configure::read('Read'));
  242. $result = Configure::load('var_test2', 'test', true);
  243. $this->assertTrue($result);
  244. $this->assertEquals('value2', Configure::read('Read'));
  245. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  246. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
  247. $this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
  248. $this->assertEquals('one', Configure::read('TestAcl.custom'));
  249. }
  250. /**
  251. * test loading with overwrite
  252. *
  253. * @return void
  254. */
  255. public function testLoadNoMerge()
  256. {
  257. Configure::config('test', new PhpConfig(CONFIG));
  258. $result = Configure::load('var_test', 'test');
  259. $this->assertTrue($result);
  260. $this->assertEquals('value', Configure::read('Read'));
  261. $result = Configure::load('var_test2', 'test', false);
  262. $this->assertTrue($result);
  263. $this->assertEquals('value2', Configure::read('Read'));
  264. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  265. $this->assertNull(Configure::read('Deep.Deeper.Deepest'));
  266. }
  267. /**
  268. * Test load() replacing existing data
  269. *
  270. * @return void
  271. */
  272. public function testLoadWithExistingData()
  273. {
  274. Configure::config('test', new PhpConfig(CONFIG));
  275. Configure::write('my_key', 'value');
  276. Configure::load('var_test', 'test');
  277. $this->assertEquals('value', Configure::read('my_key'), 'Should not overwrite existing data.');
  278. $this->assertEquals('value', Configure::read('Read'), 'Should load new data.');
  279. }
  280. /**
  281. * Test load() merging on top of existing data
  282. *
  283. * @return void
  284. */
  285. public function testLoadMergeWithExistingData()
  286. {
  287. Configure::config('test', new PhpConfig());
  288. Configure::write('my_key', 'value');
  289. Configure::write('Read', 'old');
  290. Configure::write('Deep.old', 'old');
  291. Configure::write('TestAcl.classname', 'old');
  292. Configure::load('var_test', 'test', true);
  293. $this->assertEquals('value', Configure::read('Read'), 'Should load new data.');
  294. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'), 'Should load new data');
  295. $this->assertEquals('old', Configure::read('Deep.old'), 'Should not destroy old data.');
  296. $this->assertEquals('value', Configure::read('my_key'), 'Should not destroy data.');
  297. $this->assertEquals('Original', Configure::read('TestAcl.classname'), 'No arrays');
  298. }
  299. /**
  300. * testLoad method
  301. *
  302. * @return void
  303. */
  304. public function testLoadPlugin()
  305. {
  306. Configure::config('test', new PhpConfig());
  307. Plugin::load('TestPlugin');
  308. $result = Configure::load('TestPlugin.load', 'test');
  309. $this->assertTrue($result);
  310. $expected = '/test_app/Plugin/TestPlugin/Config/load.php';
  311. $config = Configure::read('plugin_load');
  312. $this->assertEquals($expected, $config);
  313. $result = Configure::load('TestPlugin.more.load', 'test');
  314. $this->assertTrue($result);
  315. $expected = '/test_app/Plugin/TestPlugin/Config/more.load.php';
  316. $config = Configure::read('plugin_more_load');
  317. $this->assertEquals($expected, $config);
  318. Plugin::unload();
  319. }
  320. /**
  321. * testStore method
  322. *
  323. * @return void
  324. */
  325. public function testStoreAndRestore()
  326. {
  327. Cache::enable();
  328. Cache::config('configure', [
  329. 'className' => 'File',
  330. 'path' => TMP . 'tests'
  331. ]);
  332. Configure::write('Testing', 'yummy');
  333. $this->assertTrue(Configure::store('store_test', 'configure'));
  334. Configure::delete('Testing');
  335. $this->assertNull(Configure::read('Testing'));
  336. Configure::restore('store_test', 'configure');
  337. $this->assertEquals('yummy', Configure::read('Testing'));
  338. Cache::delete('store_test', 'configure');
  339. Cache::drop('configure');
  340. }
  341. /**
  342. * test that store and restore only store/restore the provided data.
  343. *
  344. * @return void
  345. */
  346. public function testStoreAndRestoreWithData()
  347. {
  348. Cache::enable();
  349. Cache::config('configure', [
  350. 'className' => 'File',
  351. 'path' => TMP . 'tests'
  352. ]);
  353. Configure::write('testing', 'value');
  354. Configure::store('store_test', 'configure', ['store_test' => 'one']);
  355. Configure::delete('testing');
  356. $this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
  357. Configure::restore('store_test', 'configure');
  358. $this->assertEquals('one', Configure::read('store_test'));
  359. $this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
  360. Cache::delete('store_test', 'configure');
  361. Cache::drop('configure');
  362. }
  363. /**
  364. * testVersion method
  365. *
  366. * @return void
  367. */
  368. public function testVersion()
  369. {
  370. $result = Configure::version();
  371. $this->assertTrue(version_compare($result, '1.2', '>='));
  372. }
  373. /**
  374. * test adding new engines.
  375. *
  376. * @return void
  377. */
  378. public function testEngineSetup()
  379. {
  380. $engine = new PhpConfig();
  381. Configure::config('test', $engine);
  382. $configured = Configure::configured();
  383. $this->assertTrue(in_array('test', $configured));
  384. $this->assertTrue(Configure::configured('test'));
  385. $this->assertFalse(Configure::configured('fake_garbage'));
  386. $this->assertTrue(Configure::drop('test'));
  387. $this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
  388. }
  389. /**
  390. * test engine() throwing exceptions on missing interface.
  391. *
  392. * @expectedException \PHPUnit_Framework_Error
  393. * @return void
  394. */
  395. public function testEngineExceptionOnIncorrectClass()
  396. {
  397. $engine = new \StdClass();
  398. Configure::config('test', $engine);
  399. }
  400. /**
  401. * Test that clear wipes all values.
  402. *
  403. * @return void
  404. */
  405. public function testClear()
  406. {
  407. Configure::write('test', 'value');
  408. $this->assertTrue(Configure::clear());
  409. $this->assertNull(Configure::read('debug'));
  410. $this->assertNull(Configure::read('test'));
  411. }
  412. /**
  413. * @expectedException \Cake\Core\Exception\Exception
  414. * @return void
  415. */
  416. public function testDumpNoAdapter()
  417. {
  418. Configure::dump(TMP . 'test.php', 'does_not_exist');
  419. }
  420. /**
  421. * test dump integrated with the PhpConfig.
  422. *
  423. * @return void
  424. */
  425. public function testDump()
  426. {
  427. Configure::config('test_Engine', new PhpConfig(TMP));
  428. $result = Configure::dump('config_test.php', 'test_Engine');
  429. $this->assertTrue($result > 0);
  430. $result = file_get_contents(TMP . 'config_test.php');
  431. $this->assertContains('<?php', $result);
  432. $this->assertContains('$config = ', $result);
  433. if (file_exists(TMP . 'config_test.php')) {
  434. unlink(TMP . 'config_test.php');
  435. }
  436. }
  437. /**
  438. * Test dumping only some of the data.
  439. *
  440. * @return void
  441. */
  442. public function testDumpPartial()
  443. {
  444. Configure::config('test_Engine', new PhpConfig(TMP));
  445. Configure::write('Error', ['test' => 'value']);
  446. $result = Configure::dump('config_test.php', 'test_Engine', ['Error']);
  447. $this->assertTrue($result > 0);
  448. $result = file_get_contents(TMP . 'config_test.php');
  449. $this->assertContains('<?php', $result);
  450. $this->assertContains('$config = ', $result);
  451. $this->assertContains('Error', $result);
  452. $this->assertNotContains('debug', $result);
  453. if (file_exists(TMP . 'config_test.php')) {
  454. unlink(TMP . 'config_test.php');
  455. }
  456. }
  457. /**
  458. * Test the consume method.
  459. *
  460. * @return void
  461. */
  462. public function testConsume()
  463. {
  464. $this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
  465. Configure::write('Test', ['key' => 'value', 'key2' => 'value2']);
  466. $result = Configure::consume('Test.key');
  467. $this->assertEquals('value', $result);
  468. $result = Configure::read('Test.key2');
  469. $this->assertEquals('value2', $result, 'Other values should remain.');
  470. $result = Configure::consume('Test');
  471. $expected = ['key2' => 'value2'];
  472. $this->assertEquals($expected, $result);
  473. }
  474. }