ConfigureTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. * ConfigureTest file
  4. *
  5. * Holds several tests
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  18. * @since CakePHP(tm) v 1.2.0.5432
  19. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  20. */
  21. namespace Cake\Test\TestCase\Core;
  22. use Cake\Cache\Cache;
  23. use Cake\Configure\Engine\PhpConfig;
  24. use Cake\Core\App;
  25. use Cake\Core\Configure;
  26. use Cake\Core\Plugin;
  27. use Cake\TestSuite\TestCase;
  28. /**
  29. * ConfigureTest
  30. *
  31. */
  32. class ConfigureTest extends TestCase {
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. Cache::disable();
  41. App::objects('Plugin', null, true);
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown() {
  49. parent::tearDown();
  50. if (file_exists(TMP . 'cache/persistent/cake_core_core_paths')) {
  51. unlink(TMP . 'cache/persistent/cake_core_core_paths');
  52. }
  53. if (file_exists(TMP . 'cache/persistent/cake_core_dir_map')) {
  54. unlink(TMP . 'cache/persistent/cake_core_dir_map');
  55. }
  56. if (file_exists(TMP . 'cache/persistent/cake_core_file_map')) {
  57. unlink(TMP . 'cache/persistent/cake_core_file_map');
  58. }
  59. if (file_exists(TMP . 'cache/persistent/cake_core_object_map')) {
  60. unlink(TMP . 'cache/persistent/cake_core_object_map');
  61. }
  62. if (file_exists(TMP . 'cache/persistent/test.config.php')) {
  63. unlink(TMP . 'cache/persistent/test.config.php');
  64. }
  65. if (file_exists(TMP . 'cache/persistent/test.php')) {
  66. unlink(TMP . 'cache/persistent/test.php');
  67. }
  68. Configure::drop('test');
  69. }
  70. /**
  71. * testRead method
  72. *
  73. * @return void
  74. */
  75. public function testRead() {
  76. $expected = 'ok';
  77. Configure::write('level1.level2.level3_1', $expected);
  78. Configure::write('level1.level2.level3_2', 'something_else');
  79. $result = Configure::read('level1.level2.level3_1');
  80. $this->assertEquals($expected, $result);
  81. $result = Configure::read('level1.level2.level3_2');
  82. $this->assertEquals('something_else', $result);
  83. $result = Configure::read('debug');
  84. $this->assertTrue($result >= 0);
  85. $result = Configure::read();
  86. $this->assertTrue(is_array($result));
  87. $this->assertTrue(isset($result['debug']));
  88. $this->assertTrue(isset($result['level1']));
  89. $result = Configure::read('something_I_just_made_up_now');
  90. $this->assertEquals(null, $result, 'Missing key should return null.');
  91. }
  92. /**
  93. * testWrite method
  94. *
  95. * @return void
  96. */
  97. public function testWrite() {
  98. $writeResult = Configure::write('SomeName.someKey', 'myvalue');
  99. $this->assertTrue($writeResult);
  100. $result = Configure::read('SomeName.someKey');
  101. $this->assertEquals('myvalue', $result);
  102. $writeResult = Configure::write('SomeName.someKey', null);
  103. $this->assertTrue($writeResult);
  104. $result = Configure::read('SomeName.someKey');
  105. $this->assertEquals(null, $result);
  106. $expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
  107. $writeResult = Configure::write('Key', $expected);
  108. $this->assertTrue($writeResult);
  109. $result = Configure::read('Key');
  110. $this->assertEquals($expected, $result);
  111. $result = Configure::read('Key.One');
  112. $this->assertEquals($expected['One'], $result);
  113. $result = Configure::read('Key.One.Two');
  114. $this->assertEquals($expected['One']['Two'], $result);
  115. $result = Configure::read('Key.One.Two.Three.Four.Five');
  116. $this->assertEquals('cool', $result);
  117. Configure::write('one.two.three.four', '4');
  118. $result = Configure::read('one.two.three.four');
  119. $this->assertEquals('4', $result);
  120. }
  121. /**
  122. * test setting display_errors with debug.
  123. *
  124. * @return void
  125. */
  126. public function testDebugSettingDisplayErrors() {
  127. Configure::write('debug', 0);
  128. $result = ini_get('display_errors');
  129. $this->assertEquals(0, $result);
  130. Configure::write('debug', 2);
  131. $result = ini_get('display_errors');
  132. $this->assertEquals(1, $result);
  133. }
  134. /**
  135. * testDelete method
  136. *
  137. * @return void
  138. */
  139. public function testDelete() {
  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', array('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. Configure::write('ConfigureTestCase', 'value');
  164. $this->assertTrue(Configure::check('ConfigureTestCase'));
  165. $this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
  166. }
  167. /**
  168. * testCheckingSavedEmpty method
  169. *
  170. * @return void
  171. */
  172. public function testCheckingSavedEmpty() {
  173. $this->assertTrue(Configure::write('ConfigureTestCase', 0));
  174. $this->assertTrue(Configure::check('ConfigureTestCase'));
  175. $this->assertTrue(Configure::write('ConfigureTestCase', '0'));
  176. $this->assertTrue(Configure::check('ConfigureTestCase'));
  177. $this->assertTrue(Configure::write('ConfigureTestCase', false));
  178. $this->assertTrue(Configure::check('ConfigureTestCase'));
  179. $this->assertTrue(Configure::write('ConfigureTestCase', null));
  180. $this->assertFalse(Configure::check('ConfigureTestCase'));
  181. }
  182. /**
  183. * testCheckKeyWithSpaces method
  184. *
  185. * @return void
  186. */
  187. public function testCheckKeyWithSpaces() {
  188. $this->assertTrue(Configure::write('Configure Test', "test"));
  189. $this->assertTrue(Configure::check('Configure Test'));
  190. Configure::delete('Configure Test');
  191. $this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
  192. $this->assertTrue(Configure::check('Configure Test.Test Case'));
  193. }
  194. /**
  195. * testCheckEmpty
  196. *
  197. * @return void
  198. */
  199. public function testCheckEmpty() {
  200. $this->assertFalse(Configure::check());
  201. }
  202. /**
  203. * testLoad method
  204. *
  205. * @expectedException RuntimeException
  206. * @return void
  207. */
  208. public function testLoadExceptionOnNonExistantFile() {
  209. Configure::config('test', new PhpConfig());
  210. Configure::load('non_existing_configuration_file', 'test');
  211. }
  212. /**
  213. * test load method for default config creation
  214. *
  215. * @return void
  216. */
  217. public function testLoadDefaultConfig() {
  218. try {
  219. Configure::load('non_existing_configuration_file');
  220. } catch (\Exception $e) {
  221. $result = Configure::configured('default');
  222. $this->assertTrue($result);
  223. }
  224. }
  225. /**
  226. * test load with merging
  227. *
  228. * @return void
  229. */
  230. public function testLoadWithMerge() {
  231. Configure::config('test', new PhpConfig(CAKE . 'Test/TestApp/Config/'));
  232. $result = Configure::load('var_test', 'test');
  233. $this->assertTrue($result);
  234. $this->assertEquals('value', Configure::read('Read'));
  235. $result = Configure::load('var_test2', 'test', true);
  236. $this->assertTrue($result);
  237. $this->assertEquals('value2', Configure::read('Read'));
  238. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  239. $this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
  240. $this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
  241. $this->assertEquals('one', Configure::read('TestAcl.custom'));
  242. }
  243. /**
  244. * test loading with overwrite
  245. *
  246. * @return void
  247. */
  248. public function testLoadNoMerge() {
  249. Configure::config('test', new PhpConfig(CAKE . 'Test/TestApp/Config/'));
  250. $result = Configure::load('var_test', 'test');
  251. $this->assertTrue($result);
  252. $this->assertEquals('value', Configure::read('Read'));
  253. $result = Configure::load('var_test2', 'test', false);
  254. $this->assertTrue($result);
  255. $this->assertEquals('value2', Configure::read('Read'));
  256. $this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
  257. $this->assertNull(Configure::read('Deep.Deeper.Deepest'));
  258. }
  259. /**
  260. * testLoad method
  261. *
  262. * @return void
  263. */
  264. public function testLoadPlugin() {
  265. Configure::config('test', new PhpConfig());
  266. Plugin::load('TestPlugin');
  267. $result = Configure::load('TestPlugin.load', 'test');
  268. $this->assertTrue($result);
  269. $expected = '/test_app/plugins/test_plugin/config/load.php';
  270. $config = Configure::read('plugin_load');
  271. $this->assertEquals($expected, $config);
  272. $result = Configure::load('TestPlugin.more.load', 'test');
  273. $this->assertTrue($result);
  274. $expected = '/test_app/plugins/test_plugin/config/more.load.php';
  275. $config = Configure::read('plugin_more_load');
  276. $this->assertEquals($expected, $config);
  277. Plugin::unload();
  278. }
  279. /**
  280. * testStore method
  281. *
  282. * @return void
  283. */
  284. public function testStoreAndRestore() {
  285. Cache::enable();
  286. Cache::config('configure', [
  287. 'className' => 'File',
  288. 'path' => TMP . 'tests'
  289. ]);
  290. Configure::write('Testing', 'yummy');
  291. $this->assertTrue(Configure::store('store_test', 'configure'));
  292. Configure::delete('Testing');
  293. $this->assertNull(Configure::read('Testing'));
  294. Configure::restore('store_test', 'configure');
  295. $this->assertEquals('yummy', Configure::read('Testing'));
  296. Cache::delete('store_test', 'configure');
  297. Cache::drop('configure');
  298. }
  299. /**
  300. * test that store and restore only store/restore the provided data.
  301. *
  302. * @return void
  303. */
  304. public function testStoreAndRestoreWithData() {
  305. Cache::enable();
  306. Cache::config('configure', [
  307. 'className' => 'File',
  308. 'path' => TMP . 'tests'
  309. ]);
  310. Configure::write('testing', 'value');
  311. Configure::store('store_test', 'configure', array('store_test' => 'one'));
  312. Configure::delete('testing');
  313. $this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
  314. Configure::restore('store_test', 'configure');
  315. $this->assertEquals('one', Configure::read('store_test'));
  316. $this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
  317. Cache::delete('store_test', 'configure');
  318. Cache::drop('configure');
  319. }
  320. /**
  321. * testVersion method
  322. *
  323. * @return void
  324. */
  325. public function testVersion() {
  326. $result = Configure::version();
  327. $this->assertTrue(version_compare($result, '1.2', '>='));
  328. }
  329. /**
  330. * test adding new engines.
  331. *
  332. * @return void
  333. */
  334. public function testEngineSetup() {
  335. $engine = new PhpConfig();
  336. Configure::config('test', $engine);
  337. $configured = Configure::configured();
  338. $this->assertTrue(in_array('test', $configured));
  339. $this->assertTrue(Configure::configured('test'));
  340. $this->assertFalse(Configure::configured('fake_garbage'));
  341. $this->assertTrue(Configure::drop('test'));
  342. $this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
  343. }
  344. /**
  345. * test engine() throwing exceptions on missing interface.
  346. *
  347. * @expectedException PHPUnit_Framework_Error
  348. * @return void
  349. */
  350. public function testEngineExceptionOnIncorrectClass() {
  351. $engine = new \StdClass();
  352. Configure::config('test', $engine);
  353. }
  354. /**
  355. * Test that clear wipes all values.
  356. *
  357. * @return void
  358. */
  359. public function testClear() {
  360. Configure::write('test', 'value');
  361. $this->assertTrue(Configure::clear());
  362. $this->assertNull(Configure::read('debug'));
  363. $this->assertNull(Configure::read('test'));
  364. }
  365. /**
  366. * @expectedException Cake\Error\ConfigureException
  367. */
  368. public function testDumpNoAdapter() {
  369. Configure::dump(TMP . 'test.php', 'does_not_exist');
  370. }
  371. /**
  372. * test dump integrated with the PhpConfig.
  373. *
  374. * @return void
  375. */
  376. public function testDump() {
  377. Configure::config('test_Engine', new PhpConfig(TMP));
  378. $result = Configure::dump('config_test.php', 'test_Engine');
  379. $this->assertTrue($result > 0);
  380. $result = file_get_contents(TMP . 'config_test.php');
  381. $this->assertContains('<?php', $result);
  382. $this->assertContains('$config = ', $result);
  383. if (file_exists(TMP . 'config_test.php')) {
  384. unlink(TMP . 'config_test.php');
  385. }
  386. }
  387. /**
  388. * Test dumping only some of the data.
  389. *
  390. * @return void
  391. */
  392. public function testDumpPartial() {
  393. Configure::config('test_Engine', new PhpConfig(TMP));
  394. Configure::write('Error', ['test' => 'value']);
  395. $result = Configure::dump('config_test.php', 'test_Engine', ['Error']);
  396. $this->assertTrue($result > 0);
  397. $result = file_get_contents(TMP . 'config_test.php');
  398. $this->assertContains('<?php', $result);
  399. $this->assertContains('$config = ', $result);
  400. $this->assertContains('Error', $result);
  401. $this->assertNotContains('debug', $result);
  402. if (file_exists(TMP . 'config_test.php')) {
  403. unlink(TMP . 'config_test.php');
  404. }
  405. }
  406. /**
  407. * Test the consume method.
  408. *
  409. * @return void
  410. */
  411. public function testConsume() {
  412. $this->assertNull(Configure::consume('DoesNotExist'), 'Should be null on empty value');
  413. Configure::write('Test', ['key' => 'value', 'key2' => 'value2']);
  414. $result = Configure::consume('Test.key');
  415. $this->assertEquals('value', $result);
  416. $result = Configure::read('Test.key2');
  417. $this->assertEquals('value2', $result, 'Other values should remain.');
  418. $result = Configure::consume('Test');
  419. $expected = ['key2' => 'value2'];
  420. $this->assertEquals($expected, $result);
  421. }
  422. }