PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Core/AppTest.php

http://github.com/cakephp/cakephp
PHP | 816 lines | 546 code | 128 blank | 142 comment | 3 complexity | 0c7b95036ec7b62de8e95a7ee253b996 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * AppTest file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Core
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * AppTest class
  21. *
  22. * @package Cake.Test.Case.Core
  23. */
  24. class AppTest extends CakeTestCase {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown() {
  31. CakePlugin::unload();
  32. }
  33. /**
  34. * testBuild method
  35. *
  36. * @return void
  37. */
  38. public function testBuild() {
  39. $old = App::path('Model');
  40. $expected = array(
  41. APP . 'Model' . DS,
  42. APP . 'models' . DS
  43. );
  44. $this->assertEquals($expected, $old);
  45. App::build(array('Model' => array('/path/to/models/')));
  46. $new = App::path('Model');
  47. $expected = array(
  48. '/path/to/models/',
  49. APP . 'Model' . DS,
  50. APP . 'models' . DS
  51. );
  52. $this->assertEquals($expected, $new);
  53. App::build();
  54. App::build(array('Model' => array('/path/to/models/')), App::PREPEND);
  55. $new = App::path('Model');
  56. $expected = array(
  57. '/path/to/models/',
  58. APP . 'Model' . DS,
  59. APP . 'models' . DS
  60. );
  61. $this->assertEquals($expected, $new);
  62. App::build();
  63. App::build(array('Model' => array('/path/to/models/')), App::APPEND);
  64. $new = App::path('Model');
  65. $expected = array(
  66. APP . 'Model' . DS,
  67. APP . 'models' . DS,
  68. '/path/to/models/'
  69. );
  70. $this->assertEquals($expected, $new);
  71. App::build();
  72. App::build(array(
  73. 'Model' => array('/path/to/models/'),
  74. 'Controller' => array('/path/to/controllers/'),
  75. ), App::APPEND);
  76. $new = App::path('Model');
  77. $expected = array(
  78. APP . 'Model' . DS,
  79. APP . 'models' . DS,
  80. '/path/to/models/'
  81. );
  82. $this->assertEquals($expected, $new);
  83. $new = App::path('Controller');
  84. $expected = array(
  85. APP . 'Controller' . DS,
  86. APP . 'controllers' . DS,
  87. '/path/to/controllers/'
  88. );
  89. $this->assertEquals($expected, $new);
  90. App::build(); //reset defaults
  91. $defaults = App::path('Model');
  92. $this->assertEquals($old, $defaults);
  93. }
  94. /**
  95. * tests that it is possible to set up paths using the cake 1.3 notation for them (models, behaviors, controllers...)
  96. *
  97. * @return void
  98. */
  99. public function testCompatibleBuild() {
  100. $old = App::path('models');
  101. $expected = array(
  102. APP . 'Model' . DS,
  103. APP . 'models' . DS
  104. );
  105. $this->assertEquals($expected, $old);
  106. App::build(array('models' => array('/path/to/models/')));
  107. $new = App::path('models');
  108. $expected = array(
  109. '/path/to/models/',
  110. APP . 'Model' . DS,
  111. APP . 'models' . DS
  112. );
  113. $this->assertEquals($expected, $new);
  114. $this->assertEquals($expected, App::path('Model'));
  115. App::build(array('datasources' => array('/path/to/datasources/')));
  116. $expected = array(
  117. '/path/to/datasources/',
  118. APP . 'Model' . DS . 'Datasource' . DS,
  119. APP . 'models' . DS . 'datasources' . DS
  120. );
  121. $result = App::path('datasources');
  122. $this->assertEquals($expected, $result);
  123. $this->assertEquals($expected, App::path('Model/Datasource'));
  124. App::build(array('behaviors' => array('/path/to/behaviors/')));
  125. $expected = array(
  126. '/path/to/behaviors/',
  127. APP . 'Model' . DS . 'Behavior' . DS,
  128. APP . 'models' . DS . 'behaviors' . DS
  129. );
  130. $result = App::path('behaviors');
  131. $this->assertEquals($expected, $result);
  132. $this->assertEquals($expected, App::path('Model/Behavior'));
  133. App::build(array('controllers' => array('/path/to/controllers/')));
  134. $expected = array(
  135. '/path/to/controllers/',
  136. APP . 'Controller' . DS,
  137. APP . 'controllers' . DS
  138. );
  139. $result = App::path('controllers');
  140. $this->assertEquals($expected, $result);
  141. $this->assertEquals($expected, App::path('Controller'));
  142. App::build(array('components' => array('/path/to/components/')));
  143. $expected = array(
  144. '/path/to/components/',
  145. APP . 'Controller' . DS . 'Component' . DS,
  146. APP . 'controllers' . DS . 'components' . DS
  147. );
  148. $result = App::path('components');
  149. $this->assertEquals($expected, $result);
  150. $this->assertEquals($expected, App::path('Controller/Component'));
  151. App::build(array('views' => array('/path/to/views/')));
  152. $expected = array(
  153. '/path/to/views/',
  154. APP . 'View' . DS,
  155. APP . 'views' . DS
  156. );
  157. $result = App::path('views');
  158. $this->assertEquals($expected, $result);
  159. $this->assertEquals($expected, App::path('View'));
  160. App::build(array('helpers' => array('/path/to/helpers/')));
  161. $expected = array(
  162. '/path/to/helpers/',
  163. APP . 'View' . DS . 'Helper' .DS,
  164. APP . 'views' . DS . 'helpers' . DS
  165. );
  166. $result = App::path('helpers');
  167. $this->assertEquals($expected, $result);
  168. $this->assertEquals($expected, App::path('View/Helper'));
  169. App::build(array('shells' => array('/path/to/shells/')));
  170. $expected = array(
  171. '/path/to/shells/',
  172. APP . 'Console' . DS . 'Command' . DS,
  173. APP . 'console' . DS . 'shells' . DS,
  174. );
  175. $result = App::path('shells');
  176. $this->assertEquals($expected, $result);
  177. $this->assertEquals($expected, App::path('Console/Command'));
  178. App::build(); //reset defaults
  179. $defaults = App::path('Model');
  180. $this->assertEquals($old, $defaults);
  181. }
  182. /**
  183. * test path() with a plugin.
  184. *
  185. * @return void
  186. */
  187. public function testPathWithPlugins() {
  188. $basepath = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
  189. App::build(array(
  190. 'Plugin' => array($basepath),
  191. ));
  192. CakePlugin::load('TestPlugin');
  193. $result = App::path('Vendor', 'TestPlugin');
  194. $this->assertEquals($basepath . 'TestPlugin' . DS . 'Vendor' . DS, $result[0]);
  195. }
  196. /**
  197. * testBuildWithReset method
  198. *
  199. * @return void
  200. */
  201. public function testBuildWithReset() {
  202. $old = App::path('Model');
  203. $expected = array(
  204. APP . 'Model' . DS,
  205. APP . 'models' . DS
  206. );
  207. $this->assertEquals($expected, $old);
  208. App::build(array('Model' => array('/path/to/models/')), App::RESET);
  209. $new = App::path('Model');
  210. $expected = array(
  211. '/path/to/models/'
  212. );
  213. $this->assertEquals($expected, $new);
  214. App::build(); //reset defaults
  215. $defaults = App::path('Model');
  216. $this->assertEquals($old, $defaults);
  217. }
  218. /**
  219. * testCore method
  220. *
  221. * @return void
  222. */
  223. public function testCore() {
  224. $model = App::core('Model');
  225. $this->assertEquals(array(CAKE . 'Model' . DS), $model);
  226. $view = App::core('View');
  227. $this->assertEquals(array(CAKE . 'View' . DS), $view);
  228. $controller = App::core('Controller');
  229. $this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
  230. $component = App::core('Controller/Component');
  231. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
  232. $auth = App::core('Controller/Component/Auth');
  233. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
  234. $datasource = App::core('Model/Datasource');
  235. $this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
  236. }
  237. /**
  238. * testListObjects method
  239. *
  240. * @return void
  241. */
  242. public function testListObjects() {
  243. $result = App::objects('class', CAKE . 'Routing', false);
  244. $this->assertTrue(in_array('Dispatcher', $result));
  245. $this->assertTrue(in_array('Router', $result));
  246. App::build(array(
  247. 'Model/Behavior' => App::core('Model/Behavior'),
  248. 'Controller' => App::core('Controller'),
  249. 'Controller/Component' => App::core('Controller/Component'),
  250. 'View' => App::core('View'),
  251. 'Model' => App::core('Model'),
  252. 'View/Helper' => App::core('View/Helper'),
  253. ), App::RESET);
  254. $result = App::objects('behavior', null, false);
  255. $this->assertTrue(in_array('TreeBehavior', $result));
  256. $result = App::objects('Model/Behavior', null, false);
  257. $this->assertTrue(in_array('TreeBehavior', $result));
  258. $result = App::objects('controller', null, false);
  259. $this->assertTrue(in_array('PagesController', $result));
  260. $result = App::objects('Controller', null, false);
  261. $this->assertTrue(in_array('PagesController', $result));
  262. $result = App::objects('component', null, false);
  263. $this->assertTrue(in_array('AuthComponent', $result));
  264. $result = App::objects('Controller/Component', null, false);
  265. $this->assertTrue(in_array('AuthComponent', $result));
  266. $result = App::objects('view', null, false);
  267. $this->assertTrue(in_array('MediaView', $result));
  268. $result = App::objects('View', null, false);
  269. $this->assertTrue(in_array('MediaView', $result));
  270. $result = App::objects('helper', null, false);
  271. $this->assertTrue(in_array('HtmlHelper', $result));
  272. $result = App::objects('View/Helper', null, false);
  273. $this->assertTrue(in_array('HtmlHelper', $result));
  274. $result = App::objects('model', null, false);
  275. $this->assertTrue(in_array('AcoAction', $result));
  276. $result = App::objects('Model', null, false);
  277. $this->assertTrue(in_array('AcoAction', $result));
  278. $result = App::objects('file');
  279. $this->assertFalse($result);
  280. $result = App::objects('file', 'non_existing_configure');
  281. $expected = array();
  282. $this->assertEquals($expected, $result);
  283. $result = App::objects('NonExistingType');
  284. $this->assertEquals($result, array());
  285. App::build(array(
  286. 'plugins' => array(
  287. CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS
  288. )
  289. ));
  290. $result = App::objects('plugin', null, false);
  291. $this->assertTrue(in_array('Cache', $result));
  292. $this->assertTrue(in_array('Log', $result));
  293. App::build();
  294. }
  295. /**
  296. * Make sure that .svn and friends are excluded from App::objects('plugin')
  297. */
  298. public function testListObjectsIgnoreDotDirectories() {
  299. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
  300. $this->skipIf(!is_writable($path), $path . ' is not writable.');
  301. App::build(array(
  302. 'plugins' => array($path)
  303. ), App::RESET);
  304. mkdir($path . '.svn');
  305. $result = App::objects('plugin', null, false);
  306. rmdir($path . '.svn');
  307. $this->assertNotContains('.svn', $result);
  308. }
  309. /**
  310. * Tests listing objects within a plugin
  311. *
  312. * @return void
  313. */
  314. public function testListObjectsInPlugin() {
  315. App::build(array(
  316. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  317. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  318. ), App::RESET);
  319. CakePlugin::loadAll();
  320. $result = App::objects('TestPlugin.model');
  321. $this->assertTrue(in_array('TestPluginPost', $result));
  322. $result = App::objects('TestPlugin.Model');
  323. $this->assertTrue(in_array('TestPluginPost', $result));
  324. $result = App::objects('TestPlugin.behavior');
  325. $this->assertTrue(in_array('TestPluginPersisterOne', $result));
  326. $result = App::objects('TestPlugin.Model/Behavior');
  327. $this->assertTrue(in_array('TestPluginPersisterOne', $result));
  328. $result = App::objects('TestPlugin.helper');
  329. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  330. $this->assertEquals($expected, $result);
  331. $result = App::objects('TestPlugin.View/Helper');
  332. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  333. $this->assertEquals($expected, $result);
  334. $result = App::objects('TestPlugin.component');
  335. $this->assertTrue(in_array('OtherComponent', $result));
  336. $result = App::objects('TestPlugin.Controller/Component');
  337. $this->assertTrue(in_array('OtherComponent', $result));
  338. $result = App::objects('TestPluginTwo.behavior');
  339. $this->assertEquals($result, array());
  340. $result = App::objects('TestPluginTwo.Model/Behavior');
  341. $this->assertEquals($result, array());
  342. $result = App::objects('model', null, false);
  343. $this->assertTrue(in_array('Comment', $result));
  344. $this->assertTrue(in_array('Post', $result));
  345. $result = App::objects('Model', null, false);
  346. $this->assertTrue(in_array('Comment', $result));
  347. $this->assertTrue(in_array('Post', $result));
  348. App::build();
  349. }
  350. /**
  351. * test that pluginPath can find paths for plugins.
  352. *
  353. * @return void
  354. */
  355. public function testPluginPath() {
  356. App::build(array(
  357. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  358. ));
  359. CakePlugin::loadAll();
  360. $path = App::pluginPath('TestPlugin');
  361. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  362. $this->assertEquals($path, $expected);
  363. $path = App::pluginPath('TestPluginTwo');
  364. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
  365. $this->assertEquals($path, $expected);
  366. App::build();
  367. }
  368. /**
  369. * test that pluginPath can find paths for plugins.
  370. *
  371. * @return void
  372. */
  373. public function testThemePath() {
  374. App::build(array(
  375. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  376. ));
  377. $path = App::themePath('test_theme');
  378. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  379. $this->assertEquals($path, $expected);
  380. $path = App::themePath('TestTheme');
  381. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  382. $this->assertEquals($path, $expected);
  383. App::build();
  384. }
  385. /**
  386. * testClassLoading method
  387. *
  388. * @return void
  389. */
  390. public function testClassLoading() {
  391. $file = App::import('Model', 'Model', false);
  392. $this->assertTrue($file);
  393. $this->assertTrue(class_exists('Model'));
  394. $file = App::import('Controller', 'Controller', false);
  395. $this->assertTrue($file);
  396. $this->assertTrue(class_exists('Controller'));
  397. $file = App::import('Component', 'Auth', false);
  398. $this->assertTrue($file);
  399. $this->assertTrue(class_exists('AuthComponent'));
  400. $file = App::import('Shell', 'Shell', false);
  401. $this->assertTrue($file);
  402. $this->assertTrue(class_exists('Shell'));
  403. $file = App::import('Configure', 'PhpReader');
  404. $this->assertTrue($file);
  405. $this->assertTrue(class_exists('PhpReader'));
  406. $file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
  407. $this->assertFalse($file);
  408. $file = App::import('Model', 'AppModel', false);
  409. $this->assertTrue($file);
  410. $this->assertTrue(class_exists('AppModel'));
  411. $file = App::import('WrongType', null, true, array(), '');
  412. $this->assertFalse($file);
  413. $file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
  414. $this->assertFalse($file);
  415. $file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
  416. $this->assertFalse($file);
  417. if (!class_exists('AppController', false)) {
  418. $classes = array_flip(get_declared_classes());
  419. $this->assertFalse(isset($classes['PagesController']));
  420. $this->assertFalse(isset($classes['AppController']));
  421. $file = App::import('Controller', 'Pages');
  422. $this->assertTrue($file);
  423. $this->assertTrue(class_exists('PagesController'));
  424. $classes = array_flip(get_declared_classes());
  425. $this->assertTrue(isset($classes['PagesController']));
  426. $this->assertTrue(isset($classes['AppController']));
  427. $file = App::import('Behavior', 'Containable');
  428. $this->assertTrue($file);
  429. $this->assertTrue(class_exists('ContainableBehavior'));
  430. $file = App::import('Component', 'RequestHandler');
  431. $this->assertTrue($file);
  432. $this->assertTrue(class_exists('RequestHandlerComponent'));
  433. $file = App::import('Helper', 'Form');
  434. $this->assertTrue($file);
  435. $this->assertTrue(class_exists('FormHelper'));
  436. $file = App::import('Model', 'NonExistingModel');
  437. $this->assertFalse($file);
  438. $file = App::import('Datasource', 'DboSource');
  439. $this->assertTrue($file);
  440. $this->assertTrue(class_exists('DboSource'));
  441. }
  442. App::build();
  443. }
  444. /**
  445. * test import() with plugins
  446. *
  447. * @return void
  448. */
  449. public function testPluginImporting() {
  450. App::build(array(
  451. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  452. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  453. ));
  454. CakePlugin::loadAll();
  455. $result = App::import('Controller', 'TestPlugin.Tests');
  456. $this->assertTrue($result);
  457. $this->assertTrue(class_exists('TestPluginAppController'));
  458. $this->assertTrue(class_exists('TestsController'));
  459. $result = App::import('Lib', 'TestPlugin.TestPluginLibrary');
  460. $this->assertTrue($result);
  461. $this->assertTrue(class_exists('TestPluginLibrary'));
  462. $result = App::import('Lib', 'Library');
  463. $this->assertTrue($result);
  464. $this->assertTrue(class_exists('Library'));
  465. $result = App::import('Helper', 'TestPlugin.OtherHelper');
  466. $this->assertTrue($result);
  467. $this->assertTrue(class_exists('OtherHelperHelper'));
  468. $result = App::import('Helper', 'TestPlugin.TestPluginApp');
  469. $this->assertTrue($result);
  470. $this->assertTrue(class_exists('TestPluginAppHelper'));
  471. $result = App::import('Datasource', 'TestPlugin.TestSource');
  472. $this->assertTrue($result);
  473. $this->assertTrue(class_exists('TestSource'));
  474. App::build();
  475. }
  476. /**
  477. * test that building helper paths actually works.
  478. *
  479. * @return void
  480. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/410
  481. */
  482. public function testImportingHelpersFromAlternatePaths() {
  483. $this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
  484. App::build(array(
  485. 'View/Helper' => array(
  486. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Helper' . DS
  487. )
  488. ));
  489. $this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
  490. App::import('Helper', 'Banana');
  491. $this->assertTrue(class_exists('BananaHelper', false), 'BananaHelper was not loaded.');
  492. App::build();
  493. }
  494. /**
  495. * testFileLoading method
  496. *
  497. * @return void
  498. */
  499. public function testFileLoading () {
  500. $file = App::import('File', 'RealFile', false, array(), CAKE . 'Config' . DS . 'config.php');
  501. $this->assertTrue($file);
  502. $file = App::import('File', 'NoFile', false, array(), CAKE . 'Config' . DS . 'cake' . DS . 'config.php');
  503. $this->assertFalse($file);
  504. }
  505. /**
  506. * testFileLoadingWithArray method
  507. *
  508. * @return void
  509. */
  510. public function testFileLoadingWithArray() {
  511. $type = array('type' => 'File', 'name' => 'SomeName', 'parent' => false,
  512. 'file' => CAKE . DS . 'Config' . DS . 'config.php');
  513. $file = App::import($type);
  514. $this->assertTrue($file);
  515. $type = array('type' => 'File', 'name' => 'NoFile', 'parent' => false,
  516. 'file' => CAKE . 'Config' . DS . 'cake' . DS . 'config.php');
  517. $file = App::import($type);
  518. $this->assertFalse($file);
  519. }
  520. /**
  521. * testFileLoadingReturnValue method
  522. *
  523. * @return void
  524. */
  525. public function testFileLoadingReturnValue () {
  526. $file = App::import('File', 'Name', false, array(), CAKE . 'Config' . DS . 'config.php', true);
  527. $this->assertTrue(!empty($file));
  528. $this->assertTrue(isset($file['Cake.version']));
  529. $type = array('type' => 'File', 'name' => 'OtherName', 'parent' => false,
  530. 'file' => CAKE . 'Config' . DS . 'config.php', 'return' => true);
  531. $file = App::import($type);
  532. $this->assertTrue(!empty($file));
  533. $this->assertTrue(isset($file['Cake.version']));
  534. }
  535. /**
  536. * testLoadingWithSearch method
  537. *
  538. * @return void
  539. */
  540. public function testLoadingWithSearch () {
  541. $file = App::import('File', 'NewName', false, array(CAKE . 'Config' . DS), 'config.php');
  542. $this->assertTrue($file);
  543. $file = App::import('File', 'AnotherNewName', false, array(CAKE), 'config.php');
  544. $this->assertFalse($file);
  545. }
  546. /**
  547. * testLoadingWithSearchArray method
  548. *
  549. * @return void
  550. */
  551. public function testLoadingWithSearchArray() {
  552. $type = array(
  553. 'type' => 'File',
  554. 'name' => 'RandomName',
  555. 'parent' => false,
  556. 'file' => 'config.php',
  557. 'search' => array(CAKE . 'Config' . DS)
  558. );
  559. $file = App::import($type);
  560. $this->assertTrue($file);
  561. $type = array(
  562. 'type' => 'File',
  563. 'name' => 'AnotherRandomName',
  564. 'parent' => false,
  565. 'file' => 'config.php',
  566. 'search' => array(CAKE)
  567. );
  568. $file = App::import($type);
  569. $this->assertFalse($file);
  570. }
  571. /**
  572. * testMultipleLoading method
  573. *
  574. * @return void
  575. */
  576. public function testMultipleLoading() {
  577. if (class_exists('PersisterOne', false) || class_exists('PersisterTwo', false)) {
  578. $this->markTestSkipped('Cannot test loading of classes that exist.');
  579. }
  580. App::build(array(
  581. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
  582. ));
  583. $toLoad = array('PersisterOne', 'PersisterTwo');
  584. $load = App::import('Model', $toLoad);
  585. $this->assertTrue($load);
  586. $classes = array_flip(get_declared_classes());
  587. $this->assertTrue(isset($classes['PersisterOne']));
  588. $this->assertTrue(isset($classes['PersisterTwo']));
  589. $load = App::import('Model', array('PersisterOne', 'SomeNotFoundClass', 'PersisterTwo'));
  590. $this->assertFalse($load);
  591. }
  592. public function testLoadingVendor() {
  593. App::build(array(
  594. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  595. 'vendors' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor'. DS),
  596. ), App::RESET);
  597. CakePlugin::loadAll();
  598. ob_start();
  599. $result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css'));
  600. $text = ob_get_clean();
  601. $this->assertTrue($result);
  602. $this->assertEquals($text, 'this is the test asset css file');
  603. $result = App::import('Vendor', 'TestPlugin.sample/SamplePlugin');
  604. $this->assertTrue($result);
  605. $this->assertTrue(class_exists('SamplePluginClassTestName'));
  606. $result = App::import('Vendor', 'sample/ConfigureTestVendorSample');
  607. $this->assertTrue($result);
  608. $this->assertTrue(class_exists('ConfigureTestVendorSample'));
  609. ob_start();
  610. $result = App::import('Vendor', 'SomeNameInSubfolder', array('file' => 'somename/some.name.php'));
  611. $text = ob_get_clean();
  612. $this->assertTrue($result);
  613. $this->assertEquals($text, 'This is a file with dot in file name');
  614. ob_start();
  615. $result = App::import('Vendor', 'TestHello', array('file' => 'Test' . DS . 'hello.php'));
  616. $text = ob_get_clean();
  617. $this->assertTrue($result);
  618. $this->assertEquals($text, 'This is the hello.php file in Test directory');
  619. ob_start();
  620. $result = App::import('Vendor', 'MyTest', array('file' => 'Test' . DS . 'MyTest.php'));
  621. $text = ob_get_clean();
  622. $this->assertTrue($result);
  623. $this->assertEquals($text, 'This is the MyTest.php file');
  624. ob_start();
  625. $result = App::import('Vendor', 'Welcome');
  626. $text = ob_get_clean();
  627. $this->assertTrue($result);
  628. $this->assertEquals($text, 'This is the welcome.php file in vendors directory');
  629. ob_start();
  630. $result = App::import('Vendor', 'TestPlugin.Welcome');
  631. $text = ob_get_clean();
  632. $this->assertTrue($result);
  633. $this->assertEquals($text, 'This is the welcome.php file in test_plugin/vendors directory');
  634. }
  635. /**
  636. * Tests that the automatic class loader will also find in "libs" folder for both
  637. * app and plugins if it does not find the class in other configured paths
  638. *
  639. */
  640. public function testLoadClassInLibs() {
  641. App::build(array(
  642. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  643. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  644. ), App::RESET);
  645. CakePlugin::loadAll();
  646. $this->assertFalse(class_exists('CustomLibClass', false));
  647. App::uses('CustomLibClass', 'TestPlugin.Custom/Package');
  648. $this->assertTrue(class_exists('CustomLibClass'));
  649. $this->assertFalse(class_exists('TestUtilityClass', false));
  650. App::uses('TestUtilityClass', 'Utility');
  651. $this->assertTrue(class_exists('CustomLibClass'));
  652. }
  653. /**
  654. * Tests that App::location() returns the defined path for a class
  655. *
  656. * @return void
  657. */
  658. public function testClassLocation() {
  659. App::uses('MyCustomClass', 'MyPackage/Name');
  660. $this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
  661. }
  662. /**
  663. * Test that paths() works.
  664. *
  665. * @return void
  666. */
  667. public function testPaths() {
  668. $result = App::paths();
  669. $this->assertArrayHasKey('Plugin', $result);
  670. $this->assertArrayHasKey('Controller', $result);
  671. $this->assertArrayHasKey('Controller/Component', $result);
  672. }
  673. /**
  674. * Proves that it is possible to load plugin libraries in top
  675. * level Lib dir for plugins
  676. *
  677. * @return void
  678. */
  679. public function testPluginLibClasses() {
  680. App::build(array(
  681. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  682. ), App::RESET);
  683. CakePlugin::loadAll();
  684. $this->assertFalse(class_exists('TestPluginOtherLibrary', false));
  685. App::uses('TestPluginOtherLibrary', 'TestPlugin.Lib');
  686. $this->assertTrue(class_exists('TestPluginOtherLibrary'));
  687. }
  688. }