AppTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Core\App;
  17. use Cake\TestSuite\TestCase;
  18. use TestApp\Core\TestApp;
  19. /**
  20. * AppTest class
  21. */
  22. class AppTest extends TestCase
  23. {
  24. /**
  25. * tearDown method
  26. *
  27. * @return void
  28. */
  29. public function tearDown(): void
  30. {
  31. parent::tearDown();
  32. $this->clearPlugins();
  33. }
  34. /**
  35. * testClassName
  36. *
  37. * $checkCake and $existsInCake are derived from the input parameters
  38. *
  39. * @param string $class Class name
  40. * @param string $type Class type
  41. * @param string $suffix Class suffix
  42. * @param bool $existsInBase Whether class exists in base.
  43. * @param mixed $expected Expected value.
  44. * @return void
  45. * @dataProvider classNameProvider
  46. */
  47. public function testClassName($class, $type, $suffix = '', $existsInBase = false, $expected = false)
  48. {
  49. static::setAppNamespace();
  50. $i = 0;
  51. TestApp::$existsInBaseCallback = function ($name, $namespace) use ($existsInBase, $class, $expected, &$i) {
  52. if ($i++ === 0) {
  53. return $existsInBase;
  54. }
  55. $checkCake = (!$existsInBase || strpos('.', $class));
  56. if ($checkCake) {
  57. return (bool)$expected;
  58. }
  59. return false;
  60. };
  61. $return = TestApp::className($class, $type, $suffix);
  62. $this->assertSame($expected === false ? null : $expected, $return);
  63. }
  64. public function testClassNameWithFqcn()
  65. {
  66. $this->assertSame(TestCase::class, App::className(TestCase::class));
  67. $this->assertNull(App::className('\Foo'));
  68. }
  69. /**
  70. * testShortName
  71. *
  72. * @param string $class Class name
  73. * @param string $type Class type
  74. * @param string $suffix Class suffix
  75. * @param mixed $expected Expected value.
  76. * @return void
  77. * @dataProvider shortNameProvider
  78. */
  79. public function testShortName($class, $type, $suffix = '', $expected = false)
  80. {
  81. static::setAppNamespace();
  82. $return = TestApp::shortName($class, $type, $suffix);
  83. $this->assertSame($expected, $return);
  84. }
  85. /**
  86. * testShortNameWithNestedAppNamespace
  87. *
  88. * @return void
  89. */
  90. public function testShortNameWithNestedAppNamespace()
  91. {
  92. static::setAppNamespace('TestApp/Nested');
  93. $return = TestApp::shortName(
  94. 'TestApp/Nested/Controller/PagesController',
  95. 'Controller',
  96. 'Controller'
  97. );
  98. $this->assertSame('Pages', $return);
  99. static::setAppNamespace();
  100. }
  101. /**
  102. * classNameProvider
  103. *
  104. * Return test permutations for testClassName method. Format:
  105. * className
  106. * type
  107. * suffix
  108. * existsInBase (Base meaning App or plugin namespace)
  109. * expected return value
  110. *
  111. * @return array
  112. */
  113. public function classNameProvider()
  114. {
  115. return [
  116. ['Does', 'Not', 'Exist'],
  117. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  118. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  119. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  120. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  121. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  122. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  123. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  124. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  125. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  126. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  127. ['Vend/MPlugin.Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\ExistsSuffix'],
  128. ['Vend/MPlugin.Also/Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\Also\ExistsSuffix'],
  129. ['Vend/MPlugin.Also', 'Exists/In', 'Suffix', true, 'Vend\MPlugin\Exists\In\AlsoSuffix'],
  130. ['Vend/MPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix'],
  131. ['Vend/MPlugin.No', 'Suffix', '', true, 'Vend\MPlugin\Suffix\No'],
  132. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  133. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  134. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  135. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  136. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  137. // Realistic examples returning nothing
  138. ['App', 'Core', 'Suffix'],
  139. ['Auth', 'Controller/Component'],
  140. ['Unknown', 'Controller', 'Controller'],
  141. // Real examples returning class names
  142. ['App', 'Core', '', false, 'Cake\Core\App'],
  143. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  144. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  145. ['Command', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\CommandTask'],
  146. ['Upgrade/Locations', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\Upgrade\LocationsTask'],
  147. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  148. ];
  149. }
  150. /**
  151. * pluginSplitNameProvider
  152. *
  153. * Return test permutations for testClassName method. Format:
  154. * className
  155. * type
  156. * suffix
  157. * expected return value
  158. *
  159. * @return array
  160. */
  161. public function shortNameProvider()
  162. {
  163. return [
  164. ['TestApp\In\ExistsApp', 'In', 'App', 'Exists'],
  165. ['TestApp\In\Also\ExistsApp', 'In', 'App', 'Also/Exists'],
  166. ['TestApp\Exists\In\AlsoApp', 'Exists/In', 'App', 'Also'],
  167. ['TestApp\Exists\In\Subfolder\AlsoApp', 'Exists/In/Subfolder', 'App', 'Also'],
  168. ['TestApp\Suffix\No', 'Suffix', '', 'No'],
  169. ['MyPlugin\In\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Exists'],
  170. ['MyPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Also/Exists'],
  171. ['MyPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'MyPlugin.Also'],
  172. ['MyPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'MyPlugin.Also'],
  173. ['MyPlugin\Suffix\No', 'Suffix', '', 'MyPlugin.No'],
  174. ['Vend\MPlugin\In\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Exists'],
  175. ['Vend\MPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Also/Exists'],
  176. ['Vend\MPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'Vend/MPlugin.Also'],
  177. ['Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'Vend/MPlugin.Also'],
  178. ['Vend\MPlugin\Suffix\No', 'Suffix', '', 'Vend/MPlugin.No'],
  179. ['Cake\In\ExistsCake', 'In', 'Cake', 'Exists'],
  180. ['Cake\In\Also\ExistsCake', 'In', 'Cake', 'Also/Exists'],
  181. ['Cake\Exists\In\AlsoCake', 'Exists/In', 'Cake', 'Also'],
  182. ['Cake\Exists\In\Subfolder\AlsoCake', 'Exists/In/Subfolder', 'Cake', 'Also'],
  183. ['Cake\Suffix\No', 'Suffix', '', 'No'],
  184. ['Muffin\Webservice\Webservice\EndpointWebservice', 'Webservice', 'Webservice', 'Muffin/Webservice.Endpoint'],
  185. // Real examples returning class names
  186. ['Cake\Core\App', 'Core', '', 'App'],
  187. ['Cake\Controller\Component\AuthComponent', 'Controller/Component', 'Component', 'Auth'],
  188. ['Cake\Cache\Engine\FileEngine', 'Cache/Engine', 'Engine', 'File'],
  189. ['Cake\Shell\Task\CommandTask', 'Shell/Task', 'Task', 'Command'],
  190. ['Cake\Shell\Task\Upgrade\LocationsTask', 'Shell/Task', 'Task', 'Upgrade/Locations'],
  191. ['TestApp\Controller\PagesController', 'Controller', 'Controller', 'Pages'],
  192. ];
  193. }
  194. /**
  195. * test classPath() with a plugin.
  196. *
  197. * @return void
  198. */
  199. public function testClassPathWithPlugins()
  200. {
  201. $basepath = TEST_APP . 'Plugin' . DS;
  202. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  203. $result = App::classPath('Controller', 'TestPlugin');
  204. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  205. $result = App::classPath('Controller', 'Company/TestPluginThree');
  206. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  207. $this->assertPathEquals($expected, $result[0]);
  208. }
  209. /**
  210. * test path() with a plugin.
  211. *
  212. * @return void
  213. * @deprecated
  214. */
  215. public function testPathWithPlugins()
  216. {
  217. $basepath = TEST_APP . 'Plugin' . DS;
  218. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  219. $result = App::path('locales', 'TestPlugin');
  220. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'resources' . DS . 'locales' . DS, $result[0]);
  221. $result = App::path('locales', 'Company/TestPluginThree');
  222. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'resources' . DS . 'locales' . DS;
  223. $this->assertPathEquals($expected, $result[0]);
  224. $this->deprecated(function () use ($basepath) {
  225. $result = App::path('Controller', 'TestPlugin');
  226. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  227. $result = App::path('Controller', 'Company/TestPluginThree');
  228. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  229. $this->assertPathEquals($expected, $result[0]);
  230. });
  231. }
  232. /**
  233. * testCore method
  234. *
  235. * @return void
  236. */
  237. public function testCore()
  238. {
  239. $model = App::core('Model');
  240. $this->assertEquals([CAKE . 'Model' . DS], $model);
  241. $view = App::core('View');
  242. $this->assertEquals([CAKE . 'View' . DS], $view);
  243. $controller = App::core('Controller');
  244. $this->assertEquals([CAKE . 'Controller' . DS], $controller);
  245. $component = App::core('Controller/Component');
  246. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS], str_replace('/', DS, $component));
  247. $auth = App::core('Controller/Component/Auth');
  248. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS], str_replace('/', DS, $auth));
  249. $datasource = App::core('Model/Datasource');
  250. $this->assertEquals([CAKE . 'Model' . DS . 'Datasource' . DS], str_replace('/', DS, $datasource));
  251. }
  252. }