ConfigureTest.php 14 KB

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