PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/configure.test.php

https://github.com/hardsshah/bookmarks
PHP | 538 lines | 302 code | 72 blank | 164 comment | 13 complexity | c2483ed47c220f32e122990e0a65ff6b MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ConfigureTest file
  5. *
  6. * Holds several tests
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.5432
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', 'Configure');
  28. /**
  29. * ConfigureTest
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs
  33. */
  34. class ConfigureTest extends CakeTestCase {
  35. /**
  36. * setUp method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function setUp() {
  42. parent::setUp();
  43. Configure::write('Cache.disable', true);
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. function tearDown() {
  52. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
  53. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
  54. }
  55. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
  56. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
  57. }
  58. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
  59. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
  60. }
  61. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
  62. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
  63. }
  64. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
  65. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
  66. }
  67. if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
  68. unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
  69. }
  70. Configure::write('debug', 2);
  71. parent::tearDown();
  72. }
  73. /**
  74. * testListObjects method
  75. *
  76. * @access public
  77. * @return void
  78. */
  79. function testListObjects() {
  80. $result = Configure::listObjects('class', TEST_CAKE_CORE_INCLUDE_PATH . 'libs');
  81. $this->assertTrue(in_array('Xml', $result));
  82. $this->assertTrue(in_array('Cache', $result));
  83. $this->assertTrue(in_array('HttpSocket', $result));
  84. $result = Configure::listObjects('behavior');
  85. $this->assertTrue(in_array('Tree', $result));
  86. $result = Configure::listObjects('controller');
  87. $this->assertTrue(in_array('Pages', $result));
  88. $result = Configure::listObjects('component');
  89. $this->assertTrue(in_array('Auth', $result));
  90. $result = Configure::listObjects('view');
  91. $this->assertTrue(in_array('Media', $result));
  92. $result = Configure::listObjects('helper');
  93. $this->assertTrue(in_array('Html', $result));
  94. $result = Configure::listObjects('model');
  95. $notExpected = array('AppModel', 'Behavior', 'ConnectionManager', 'DbAcl', 'Model', 'Schema');
  96. foreach ($notExpected as $class) {
  97. $this->assertFalse(in_array($class, $result));
  98. }
  99. $result = Configure::listObjects('file');
  100. $this->assertFalse($result);
  101. $result = Configure::listObjects('file', 'non_existing_configure');
  102. $expected = array();
  103. $this->assertEqual($result, $expected);
  104. $result = Configure::listObjects('NonExistingType');
  105. $this->assertFalse($result);
  106. }
  107. /**
  108. * testRead method
  109. *
  110. * @access public
  111. * @return void
  112. */
  113. function testRead() {
  114. $expected = 'ok';
  115. Configure::write('level1.level2.level3_1', $expected);
  116. Configure::write('level1.level2.level3_2', 'something_else');
  117. $result = Configure::read('level1.level2.level3_1');
  118. $this->assertEqual($expected, $result);
  119. $result = Configure::read('level1.level2.level3_2');
  120. $this->assertEqual($result, 'something_else');
  121. $result = Configure::read('debug');
  122. $this->assertTrue($result >= 0);
  123. }
  124. /**
  125. * testWrite method
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. function testWrite() {
  131. Configure::write('SomeName.someKey', 'myvalue');
  132. $result = Configure::read('SomeName.someKey');
  133. $this->assertEqual($result, 'myvalue');
  134. Configure::write('SomeName.someKey', null);
  135. $result = Configure::read('SomeName.someKey');
  136. $this->assertEqual($result, null);
  137. }
  138. /**
  139. * testSetErrorReporting Level
  140. *
  141. * @return void
  142. **/
  143. function testSetErrorReportingLevel() {
  144. Configure::write('debug', 0);
  145. $result = ini_get('error_reporting');
  146. $this->assertEqual($result, 0);
  147. Configure::write('debug', 2);
  148. $result = ini_get('error_reporting');
  149. $this->assertEqual($result, E_ALL);
  150. $result = ini_get('display_errors');
  151. $this->assertEqual($result, 1);
  152. Configure::write('debug', 0);
  153. $result = ini_get('error_reporting');
  154. $this->assertEqual($result, 0);
  155. }
  156. /**
  157. * testDelete method
  158. *
  159. * @access public
  160. * @return void
  161. */
  162. function testDelete() {
  163. Configure::write('SomeName.someKey', 'myvalue');
  164. $result = Configure::read('SomeName.someKey');
  165. $this->assertEqual($result, 'myvalue');
  166. Configure::delete('SomeName.someKey');
  167. $result = Configure::read('SomeName.someKey');
  168. $this->assertTrue($result === null);
  169. Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
  170. $result = Configure::read('SomeName.someKey');
  171. $this->assertEqual($result, 'myvalue');
  172. $result = Configure::read('SomeName.otherKey');
  173. $this->assertEqual($result, 'otherValue');
  174. Configure::delete('SomeName');
  175. $result = Configure::read('SomeName.someKey');
  176. $this->assertTrue($result === null);
  177. $result = Configure::read('SomeName.otherKey');
  178. $this->assertTrue($result === null);
  179. }
  180. /**
  181. * testLoad method
  182. *
  183. * @access public
  184. * @return void
  185. */
  186. function testLoad() {
  187. $result = Configure::load('non_existing_configuration_file');
  188. $this->assertFalse($result);
  189. $result = Configure::load('config');
  190. $this->assertTrue($result === null);
  191. }
  192. /**
  193. * testStore method
  194. *
  195. * @access public
  196. * @return void
  197. */
  198. function testStoreAndLoad() {
  199. Configure::write('Cache.disable', false);
  200. $expected = array('data' => 'value');
  201. Configure::store('SomeExample', 'test', $expected);
  202. Configure::load('test');
  203. $config = Configure::read('SomeExample');
  204. $this->assertEqual($config, $expected);
  205. $expected = array('data' => array('first' => 'value', 'second' => 'value2'));
  206. Configure::store('AnotherExample', 'test.config', $expected);
  207. Configure::load('test.config');
  208. $config = Configure::read('AnotherExample');
  209. $this->assertEqual($config, $expected);
  210. }
  211. /**
  212. * testVersion method
  213. *
  214. * @access public
  215. * @return void
  216. */
  217. function testVersion() {
  218. $result = Configure::version();
  219. $this->assertTrue(version_compare($result, '1.2', '>='));
  220. }
  221. /**
  222. * testBuildPaths method
  223. *
  224. * @access public
  225. * @return void
  226. */
  227. function testBuildPaths() {
  228. Configure::buildPaths(array());
  229. $models = Configure::read('modelPaths');
  230. $this->assertTrue(!empty($models));
  231. }
  232. }
  233. /**
  234. * AppImportTest class
  235. *
  236. * @package cake
  237. * @subpackage cake.tests.cases.libs
  238. */
  239. class AppImportTest extends UnitTestCase {
  240. /**
  241. * testClassLoading method
  242. *
  243. * @access public
  244. * @return void
  245. */
  246. function testClassLoading() {
  247. $file = App::import();
  248. $this->assertTrue($file);
  249. $file = App::import('Core', 'Model', false);
  250. $this->assertTrue($file);
  251. $file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
  252. $this->assertFalse($file);
  253. $file = App::import('Model', 'AppModel', false);
  254. $this->assertTrue($file);
  255. $file = App::import('WrongType', null, true, array(), '');
  256. $this->assertTrue($file);
  257. $file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
  258. $this->assertFalse($file);
  259. $file = App::import('Core', 'NonExistingPlugin.NonExistingModel', false);
  260. $this->assertFalse($file);
  261. $file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
  262. $this->assertFalse($file);
  263. $file = App::import('Core', array('NonExistingPlugin.NonExistingModel'), false);
  264. $this->assertFalse($file);
  265. $file = App::import('Core', array('NonExistingPlugin.NonExistingModel.AnotherChild'), false);
  266. $this->assertFalse($file);
  267. if (!class_exists('AppController')) {
  268. $classes = array_flip(get_declared_classes());
  269. if (PHP5) {
  270. $this->assertFalse(isset($classes['PagesController']));
  271. $this->assertFalse(isset($classes['AppController']));
  272. } else {
  273. $this->assertFalse(isset($classes['pagescontroller']));
  274. $this->assertFalse(isset($classes['appcontroller']));
  275. }
  276. $file = App::import('Controller', 'Pages');
  277. $this->assertTrue($file);
  278. $classes = array_flip(get_declared_classes());
  279. if (PHP5) {
  280. $this->assertTrue(isset($classes['PagesController']));
  281. $this->assertTrue(isset($classes['AppController']));
  282. } else {
  283. $this->assertTrue(isset($classes['pagescontroller']));
  284. $this->assertTrue(isset($classes['appcontroller']));
  285. }
  286. $file = App::import('Behavior', 'Containable');
  287. $this->assertTrue($file);
  288. $file = App::import('Component', 'RequestHandler');
  289. $this->assertTrue($file);
  290. $file = App::import('Helper', 'Form');
  291. $this->assertTrue($file);
  292. $file = App::import('Model', 'NonExistingModel');
  293. $this->assertFalse($file);
  294. }
  295. $_back = Configure::read('pluginPaths');
  296. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  297. $result = App::import('Controller', 'TestPlugin.Tests');
  298. $this->assertTrue($result);
  299. $this->assertTrue(class_exists('TestPluginAppController'));
  300. $this->assertTrue(class_exists('TestsController'));
  301. $result = App::import('Helper', 'TestPlugin.OtherHelper');
  302. $this->assertTrue($result);
  303. $this->assertTrue(class_exists('OtherHelperHelper'));
  304. Configure::write('pluginPaths', $_back);
  305. }
  306. /**
  307. * testFileLoading method
  308. *
  309. * @access public
  310. * @return void
  311. */
  312. function testFileLoading () {
  313. $file = App::import('File', 'RealFile', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php');
  314. $this->assertTrue($file);
  315. $file = App::import('File', 'NoFile', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'cake' . DS . 'config.php');
  316. $this->assertFalse($file);
  317. }
  318. // import($type = null, $name = null, $parent = true, $file = null, $search = array(), $return = false) {
  319. /**
  320. * testFileLoadingWithArray method
  321. *
  322. * @access public
  323. * @return void
  324. */
  325. function testFileLoadingWithArray() {
  326. $type = array('type' => 'File', 'name' => 'SomeName', 'parent' => false,
  327. 'file' => TEST_CAKE_CORE_INCLUDE_PATH . DS . 'config' . DS . 'config.php');
  328. $file = App::import($type);
  329. $this->assertTrue($file);
  330. $type = array('type' => 'File', 'name' => 'NoFile', 'parent' => false,
  331. 'file' => TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'cake' . DS . 'config.php');
  332. $file = App::import($type);
  333. $this->assertFalse($file);
  334. }
  335. /**
  336. * testFileLoadingReturnValue method
  337. *
  338. * @access public
  339. * @return void
  340. */
  341. function testFileLoadingReturnValue () {
  342. $file = App::import('File', 'Name', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php', true);
  343. $this->assertTrue($file);
  344. $this->assertTrue(isset($file['Cake.version']));
  345. $type = array('type' => 'File', 'name' => 'OtherName', 'parent' => false,
  346. 'file' => TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php', 'return' => true);
  347. $file = App::import($type);
  348. $this->assertTrue($file);
  349. $this->assertTrue(isset($file['Cake.version']));
  350. }
  351. /**
  352. * testLoadingWithSearch method
  353. *
  354. * @access public
  355. * @return void
  356. */
  357. function testLoadingWithSearch () {
  358. $file = App::import('File', 'NewName', false, array(TEST_CAKE_CORE_INCLUDE_PATH ), 'config.php');
  359. $this->assertTrue($file);
  360. $file = App::import('File', 'AnotherNewName', false, array(LIBS), 'config.php');
  361. $this->assertFalse($file);
  362. }
  363. /**
  364. * testLoadingWithSearchArray method
  365. *
  366. * @access public
  367. * @return void
  368. */
  369. function testLoadingWithSearchArray () {
  370. $type = array('type' => 'File', 'name' => 'RandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(TEST_CAKE_CORE_INCLUDE_PATH ));
  371. $file = App::import($type);
  372. $this->assertTrue($file);
  373. $type = array('type' => 'File', 'name' => 'AnotherRandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(LIBS));
  374. $file = App::import($type);
  375. $this->assertFalse($file);
  376. }
  377. /**
  378. * testMultipleLoading method
  379. *
  380. * @access public
  381. * @return void
  382. */
  383. function testMultipleLoading() {
  384. $toLoad = array('I18n', 'Socket');
  385. $classes = array_flip(get_declared_classes());
  386. $this->assertFalse(isset($classes['i18n']));
  387. $this->assertFalse(isset($classes['Socket']));
  388. $load = App::import($toLoad);
  389. $this->assertTrue($load);
  390. $classes = array_flip(get_declared_classes());
  391. if (PHP5) {
  392. $this->assertTrue(isset($classes['I18n']));
  393. } else {
  394. $this->assertTrue(isset($classes['i18n']));
  395. }
  396. $load = App::import(array('I18n', 'SomeNotFoundClass', 'Socket'));
  397. $this->assertFalse($load);
  398. $load = App::import($toLoad);
  399. $this->assertTrue($load);
  400. }
  401. /**
  402. * This test only works if you have plugins/my_plugin set up.
  403. * plugins/my_plugin/models/my_plugin.php and other_model.php
  404. */
  405. /*
  406. function testMultipleLoadingByType() {
  407. $classes = array_flip(get_declared_classes());
  408. $this->assertFalse(isset($classes['OtherPlugin']));
  409. $this->assertFalse(isset($classes['MyPlugin']));
  410. $load = App::import('Model', array('MyPlugin.OtherPlugin', 'MyPlugin.MyPlugin'));
  411. $this->assertTrue($load);
  412. $classes = array_flip(get_declared_classes());
  413. $this->assertTrue(isset($classes['OtherPlugin']));
  414. $this->assertTrue(isset($classes['MyPlugin']));
  415. }
  416. */
  417. function testLoadingVendor() {
  418. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  419. Configure::write('vendorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS));
  420. ob_start();
  421. $result = App::import('Vendor', 'TestPlugin.TestPluginAsset', array('ext' => 'css'));
  422. $text = ob_get_clean();
  423. $this->assertTrue($result);
  424. $this->assertEqual($text, 'this is the test plugin asset css file');
  425. ob_start();
  426. $result = App::import('Vendor', 'TestAsset', array('ext' => 'css'));
  427. $text = ob_get_clean();
  428. $this->assertTrue($result);
  429. $this->assertEqual($text, 'this is the test asset css file');
  430. $result = App::import('Vendor', 'TestPlugin.SamplePlugin');
  431. $this->assertTrue($result);
  432. $this->assertTrue(class_exists('SamplePluginClassTestName'));
  433. $result = App::import('Vendor', 'ConfigureTestVendorSample');
  434. $this->assertTrue($result);
  435. $this->assertTrue(class_exists('ConfigureTestVendorSample'));
  436. ob_start();
  437. $result = App::import('Vendor', 'SomeName', array('file' => 'some.name.php'));
  438. $text = ob_get_clean();
  439. $this->assertTrue($result);
  440. $this->assertEqual($text, 'This is a file with dot in file name');
  441. ob_start();
  442. $result = App::import('Vendor', 'TestHello', array('file' => 'Test'.DS.'hello.php'));
  443. $text = ob_get_clean();
  444. $this->assertTrue($result);
  445. $this->assertEqual($text, 'This is the hello.php file in Test directory');
  446. ob_start();
  447. $result = App::import('Vendor', 'MyTest', array('file' => 'Test'.DS.'MyTest.php'));
  448. $text = ob_get_clean();
  449. $this->assertTrue($result);
  450. $this->assertEqual($text, 'This is the MyTest.php file');
  451. ob_start();
  452. $result = App::import('Vendor', 'Welcome');
  453. $text = ob_get_clean();
  454. $this->assertTrue($result);
  455. $this->assertEqual($text, 'This is the welcome.php file in vendors directory');
  456. ob_start();
  457. $result = App::import('Vendor', 'TestPlugin.Welcome');
  458. $text = ob_get_clean();
  459. $this->assertTrue($result);
  460. $this->assertEqual($text, 'This is the welcome.php file in test_plugin/vendors directory');
  461. }
  462. }
  463. ?>