ConfigureTest.php 14 KB

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