/tests/TestCase/Core/AppTest.php

https://github.com/LubosRemplik/cakephp · PHP · 257 lines · 141 code · 32 blank · 84 comment · 3 complexity · ae65bd82ca025d72e3e7bc97e9cd5f63 MD5 · raw file

  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 2.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core;
  15. use Cake\Core\App;
  16. use Cake\Core\Plugin;
  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()
  30. {
  31. parent::tearDown();
  32. Plugin::unload();
  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, $return);
  63. }
  64. /**
  65. * testShortName
  66. *
  67. * @param string $class Class name
  68. * @param string $type Class type
  69. * @param string $suffix Class suffix
  70. * @param mixed $expected Expected value.
  71. * @return void
  72. * @dataProvider shortNameProvider
  73. */
  74. public function testShortName($class, $type, $suffix = '', $expected = false)
  75. {
  76. static::setAppNamespace();
  77. $return = TestApp::shortName($class, $type, $suffix);
  78. $this->assertSame($expected, $return);
  79. }
  80. /**
  81. * testShortNameWithNestedAppNamespace
  82. *
  83. * @return void
  84. */
  85. public function testShortNameWithNestedAppNamespace()
  86. {
  87. static::setAppNamespace('TestApp/Nested');
  88. $return = TestApp::shortName(
  89. 'TestApp/Nested/Controller/PagesController',
  90. 'Controller',
  91. 'Controller'
  92. );
  93. $this->assertSame('Pages', $return);
  94. static::setAppNamespace();
  95. }
  96. /**
  97. * classnameProvider
  98. *
  99. * Return test permutations for testClassname method. Format:
  100. * classname
  101. * type
  102. * suffix
  103. * existsInBase (Base meaning App or plugin namespace)
  104. * expected return value
  105. *
  106. * @return void
  107. */
  108. public function classnameProvider()
  109. {
  110. return [
  111. ['Does', 'Not', 'Exist'],
  112. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  113. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  114. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  115. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  116. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  117. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  118. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  119. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  120. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  121. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  122. ['Vend/MPlugin.Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\ExistsSuffix'],
  123. ['Vend/MPlugin.Also/Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\Also\ExistsSuffix'],
  124. ['Vend/MPlugin.Also', 'Exists/In', 'Suffix', true, 'Vend\MPlugin\Exists\In\AlsoSuffix'],
  125. ['Vend/MPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix'],
  126. ['Vend/MPlugin.No', 'Suffix', '', true, 'Vend\MPlugin\Suffix\No'],
  127. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  128. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  129. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  130. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  131. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  132. // Realistic examples returning nothing
  133. ['App', 'Core', 'Suffix'],
  134. ['Auth', 'Controller/Component'],
  135. ['Unknown', 'Controller', 'Controller'],
  136. // Real examples returning classnames
  137. ['App', 'Core', '', false, 'Cake\Core\App'],
  138. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  139. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  140. ['Command', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\CommandTask'],
  141. ['Upgrade/Locations', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\Upgrade\LocationsTask'],
  142. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  143. ];
  144. }
  145. /**
  146. * pluginSplitNameProvider
  147. *
  148. * Return test permutations for testClassname method. Format:
  149. * classname
  150. * type
  151. * suffix
  152. * expected return value
  153. *
  154. * @return void
  155. */
  156. public function shortNameProvider()
  157. {
  158. return [
  159. ['TestApp\In\ExistsApp', 'In', 'App', 'Exists'],
  160. ['TestApp\In\Also\ExistsApp', 'In', 'App', 'Also/Exists'],
  161. ['TestApp\Exists\In\AlsoApp', 'Exists/In', 'App', 'Also'],
  162. ['TestApp\Exists\In\Subfolder\AlsoApp', 'Exists/In/Subfolder', 'App', 'Also'],
  163. ['TestApp\Suffix\No', 'Suffix', '', 'No'],
  164. ['MyPlugin\In\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Exists'],
  165. ['MyPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Also/Exists'],
  166. ['MyPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'MyPlugin.Also'],
  167. ['MyPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'MyPlugin.Also'],
  168. ['MyPlugin\Suffix\No', 'Suffix', '', 'MyPlugin.No'],
  169. ['Vend\MPlugin\In\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Exists'],
  170. ['Vend\MPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Also/Exists'],
  171. ['Vend\MPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'Vend/MPlugin.Also'],
  172. ['Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'Vend/MPlugin.Also'],
  173. ['Vend\MPlugin\Suffix\No', 'Suffix', '', 'Vend/MPlugin.No'],
  174. ['Cake\In\ExistsCake', 'In', 'Cake', 'Exists'],
  175. ['Cake\In\Also\ExistsCake', 'In', 'Cake', 'Also/Exists'],
  176. ['Cake\Exists\In\AlsoCake', 'Exists/In', 'Cake', 'Also'],
  177. ['Cake\Exists\In\Subfolder\AlsoCake', 'Exists/In/Subfolder', 'Cake', 'Also'],
  178. ['Cake\Suffix\No', 'Suffix', '', 'No'],
  179. ['Muffin\Webservice\Webservice\EndpointWebservice', 'Webservice', 'Webservice', 'Muffin/Webservice.Endpoint'],
  180. // Real examples returning classnames
  181. ['Cake\Core\App', 'Core', '', 'App'],
  182. ['Cake\Controller\Component\AuthComponent', 'Controller/Component', 'Component', 'Auth'],
  183. ['Cake\Cache\Engine\FileEngine', 'Cache/Engine', 'Engine', 'File'],
  184. ['Cake\Shell\Task\CommandTask', 'Shell/Task', 'Task', 'Command'],
  185. ['Cake\Shell\Task\Upgrade\LocationsTask', 'Shell/Task', 'Task', 'Upgrade/Locations'],
  186. ['TestApp\Controller\PagesController', 'Controller', 'Controller', 'Pages'],
  187. ];
  188. }
  189. /**
  190. * test path() with a plugin.
  191. *
  192. * @return void
  193. */
  194. public function testPathWithPlugins()
  195. {
  196. $basepath = TEST_APP . 'Plugin' . DS;
  197. Plugin::load('TestPlugin');
  198. $result = App::path('Controller', 'TestPlugin');
  199. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  200. Plugin::load('Company/TestPluginThree');
  201. $result = App::path('Controller', 'Company/TestPluginThree');
  202. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  203. $this->assertPathEquals($expected, $result[0]);
  204. }
  205. /**
  206. * testCore method
  207. *
  208. * @return void
  209. */
  210. public function testCore()
  211. {
  212. $model = App::core('Model');
  213. $this->assertEquals([CAKE . 'Model' . DS], $model);
  214. $view = App::core('View');
  215. $this->assertEquals([CAKE . 'View' . DS], $view);
  216. $controller = App::core('Controller');
  217. $this->assertEquals([CAKE . 'Controller' . DS], $controller);
  218. $component = App::core('Controller/Component');
  219. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS], str_replace('/', DS, $component));
  220. $auth = App::core('Controller/Component/Auth');
  221. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS], str_replace('/', DS, $auth));
  222. $datasource = App::core('Model/Datasource');
  223. $this->assertEquals([CAKE . 'Model' . DS . 'Datasource' . DS], str_replace('/', DS, $datasource));
  224. }
  225. }