AppTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 2.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core;
  15. use Cake\Core\App;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. use TestApp\Core\TestApp;
  20. /**
  21. * AppTest class
  22. *
  23. */
  24. class AppTest extends TestCase {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown() {
  31. parent::tearDown();
  32. Plugin::unload();
  33. }
  34. /**
  35. * testClassname
  36. *
  37. * $checkCake and $existsInCake are derived from the input parameters
  38. *
  39. * @dataProvider classnameProvider
  40. * @return void
  41. */
  42. public function testClassname($class, $type, $suffix = '', $existsInBase = false, $expected = false) {
  43. Configure::write('App.namespace', 'TestApp');
  44. $i = 0;
  45. TestApp::$existsInBaseCallback = function($name, $namespace) use ($existsInBase, $class, $expected, &$i) {
  46. if ($i++ === 0) {
  47. return $existsInBase;
  48. }
  49. $checkCake = (!$existsInBase || strpos('.', $class));
  50. if ($checkCake) {
  51. return (bool)$expected;
  52. }
  53. return false;
  54. };
  55. $return = TestApp::classname($class, $type, $suffix);
  56. $this->assertSame($expected, $return);
  57. }
  58. /**
  59. * classnameProvider
  60. *
  61. * Return test permutations for testClassname method. Format:
  62. * classname
  63. * type
  64. * suffix
  65. * existsInBase (Base meaning App or plugin namespace)
  66. * expected return value
  67. *
  68. * @return void
  69. */
  70. public function classnameProvider() {
  71. return [
  72. ['Does', 'Not', 'Exist'],
  73. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  74. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  75. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  76. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  77. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  78. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  79. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  80. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  81. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  82. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  83. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  84. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  85. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  86. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  87. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  88. // Realistic examples returning nothing
  89. ['App', 'Core', 'Suffix'],
  90. ['Auth', 'Controller/Component'],
  91. ['Unknown', 'Controller', 'Controller'],
  92. // Real examples returning classnames
  93. ['App', 'Core', '', false, 'Cake\Core\App'],
  94. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  95. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  96. ['Command', 'Console/Command/Task', 'Task', false, 'Cake\Console\Command\Task\CommandTask'],
  97. ['Upgrade/Locations', 'Console/Command/Task', 'Task', false, 'Cake\Console\Command\Task\Upgrade\LocationsTask'],
  98. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  99. ];
  100. }
  101. /**
  102. * test path() with a plugin.
  103. *
  104. * @return void
  105. */
  106. public function testPathWithPlugins() {
  107. $basepath = TEST_APP . 'Plugin' . DS;
  108. Plugin::load('TestPlugin');
  109. $result = App::path('Controller', 'TestPlugin');
  110. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'Controller' . DS, $result[0]);
  111. }
  112. /**
  113. * testCore method
  114. *
  115. * @return void
  116. */
  117. public function testCore() {
  118. $model = App::core('Model');
  119. $this->assertEquals(array(CAKE . 'Model' . DS), $model);
  120. $view = App::core('View');
  121. $this->assertEquals(array(CAKE . 'View' . DS), $view);
  122. $controller = App::core('Controller');
  123. $this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
  124. $component = App::core('Controller/Component');
  125. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
  126. $auth = App::core('Controller/Component/Auth');
  127. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
  128. $datasource = App::core('Model/Datasource');
  129. $this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
  130. }
  131. /**
  132. * testListObjects method
  133. *
  134. * @return void
  135. */
  136. public function testListObjects() {
  137. $result = App::objects('class', CAKE . 'Routing', false);
  138. $this->assertTrue(in_array('Dispatcher', $result));
  139. $this->assertTrue(in_array('Router', $result));
  140. $result = App::objects('Model/Behavior', null, false);
  141. $this->assertContains('SluggableBehavior', $result);
  142. $result = App::objects('Controller/Component', null, false);
  143. $this->assertContains('AppleComponent', $result);
  144. $result = App::objects('View', null, false);
  145. $this->assertContains('CustomJsonView', $result);
  146. $result = App::objects('View/Helper', null, false);
  147. $this->assertContains('BananaHelper', $result);
  148. $result = App::objects('Model/Table', null, false);
  149. $this->assertContains('ArticlesTable', $result);
  150. $result = App::objects('file');
  151. $this->assertFalse($result);
  152. $result = App::objects('file', 'non_existing_configure');
  153. $expected = array();
  154. $this->assertEquals($expected, $result);
  155. $result = App::objects('NonExistingType');
  156. $this->assertSame(array(), $result);
  157. $result = App::objects('Plugin', null, false);
  158. $this->assertContains('TestPlugin', $result);
  159. $this->assertContains('TestPluginTwo', $result);
  160. }
  161. /**
  162. * Make sure that .svn and friends are excluded from App::objects('Plugin')
  163. */
  164. public function testListObjectsIgnoreDotDirectories() {
  165. $path = TEST_APP . 'Plugin/';
  166. $this->skipIf(!is_writable($path), $path . ' is not writable.');
  167. mkdir($path . '.svn');
  168. $result = App::objects('Plugin', null, false);
  169. rmdir($path . '.svn');
  170. $this->assertNotContains('.svn', $result);
  171. }
  172. /**
  173. * Tests listing objects within a plugin
  174. *
  175. * @return void
  176. */
  177. public function testListObjectsInPlugin() {
  178. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  179. $result = App::objects('TestPlugin.Model/Table');
  180. $this->assertContains('TestPluginCommentsTable', $result);
  181. $result = App::objects('TestPlugin.Model/Behavior');
  182. $this->assertTrue(in_array('PersisterOneBehavior', $result));
  183. $result = App::objects('TestPlugin.View/Helper');
  184. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  185. $this->assertEquals($expected, $result);
  186. $result = App::objects('TestPlugin.Controller/Component');
  187. $this->assertTrue(in_array('OtherComponent', $result));
  188. $result = App::objects('TestPluginTwo.Model/Behavior');
  189. $this->assertSame(array(), $result);
  190. $result = App::objects('Model/Table', null, false);
  191. $this->assertContains('PostsTable', $result);
  192. $this->assertContains('ArticlesTable', $result);
  193. }
  194. /**
  195. * test that pluginPath can find paths for plugins.
  196. *
  197. * @return void
  198. */
  199. public function testPluginPath() {
  200. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  201. $path = App::pluginPath('TestPlugin');
  202. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  203. $this->assertPathEquals($expected, $path);
  204. $path = App::pluginPath('TestPluginTwo');
  205. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  206. $this->assertPathEquals($expected, $path);
  207. }
  208. /**
  209. * test that themePath can find paths for themes.
  210. *
  211. * @return void
  212. */
  213. public function testThemePath() {
  214. $path = App::themePath('test_theme');
  215. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS;
  216. $this->assertPathEquals($expected, $path);
  217. $path = App::themePath('TestTheme');
  218. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS;
  219. $this->assertPathEquals($expected, $path);
  220. }
  221. }