AppTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. class AppTest extends TestCase
  24. {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown()
  31. {
  32. parent::tearDown();
  33. Plugin::unload();
  34. }
  35. /**
  36. * testClassname
  37. *
  38. * $checkCake and $existsInCake are derived from the input parameters
  39. *
  40. * @param string $class Class name
  41. * @param string $type Class type
  42. * @param string $suffix Class suffix
  43. * @param bool $existsInBase Whether class exists in base.
  44. * @param mixed $expected Expected value.
  45. * @return void
  46. * @dataProvider classnameProvider
  47. */
  48. public function testClassname($class, $type, $suffix = '', $existsInBase = false, $expected = false)
  49. {
  50. Configure::write('App.namespace', 'TestApp');
  51. $i = 0;
  52. TestApp::$existsInBaseCallback = function ($name, $namespace) use ($existsInBase, $class, $expected, &$i) {
  53. if ($i++ === 0) {
  54. return $existsInBase;
  55. }
  56. $checkCake = (!$existsInBase || strpos('.', $class));
  57. if ($checkCake) {
  58. return (bool)$expected;
  59. }
  60. return false;
  61. };
  62. $return = TestApp::classname($class, $type, $suffix);
  63. $this->assertSame($expected, $return);
  64. }
  65. /**
  66. * testShortName
  67. *
  68. * @param string $class Class name
  69. * @param string $type Class type
  70. * @param string $suffix Class suffix
  71. * @param mixed $expected Expected value.
  72. * @return void
  73. * @dataProvider shortNameProvider
  74. */
  75. public function testShortName($class, $type, $suffix = '', $expected = false)
  76. {
  77. Configure::write('App.namespace', 'TestApp');
  78. $return = TestApp::shortName($class, $type, $suffix);
  79. $this->assertSame($expected, $return);
  80. }
  81. /**
  82. * testShortNameWithNestedAppNamespace
  83. *
  84. * @return void
  85. */
  86. public function testShortNameWithNestedAppNamespace()
  87. {
  88. Configure::write('App.namespace', 'TestApp/Nested');
  89. $return = TestApp::shortName(
  90. 'TestApp/Nested/Controller/PagesController',
  91. 'Controller',
  92. 'Controller'
  93. );
  94. $this->assertSame('Pages', $return);
  95. Configure::write('App.namespace', 'TestApp');
  96. }
  97. /**
  98. * classnameProvider
  99. *
  100. * Return test permutations for testClassname method. Format:
  101. * classname
  102. * type
  103. * suffix
  104. * existsInBase (Base meaning App or plugin namespace)
  105. * expected return value
  106. *
  107. * @return void
  108. */
  109. public function classnameProvider()
  110. {
  111. return [
  112. ['Does', 'Not', 'Exist'],
  113. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  114. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  115. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  116. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  117. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  118. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  119. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  120. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  121. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  122. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  123. ['Vend/MPlugin.Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\ExistsSuffix'],
  124. ['Vend/MPlugin.Also/Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\Also\ExistsSuffix'],
  125. ['Vend/MPlugin.Also', 'Exists/In', 'Suffix', true, 'Vend\MPlugin\Exists\In\AlsoSuffix'],
  126. ['Vend/MPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix'],
  127. ['Vend/MPlugin.No', 'Suffix', '', true, 'Vend\MPlugin\Suffix\No'],
  128. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  129. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  130. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  131. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  132. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  133. // Realistic examples returning nothing
  134. ['App', 'Core', 'Suffix'],
  135. ['Auth', 'Controller/Component'],
  136. ['Unknown', 'Controller', 'Controller'],
  137. // Real examples returning classnames
  138. ['App', 'Core', '', false, 'Cake\Core\App'],
  139. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  140. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  141. ['Command', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\CommandTask'],
  142. ['Upgrade/Locations', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\Upgrade\LocationsTask'],
  143. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  144. ];
  145. }
  146. /**
  147. * pluginSplitNameProvider
  148. *
  149. * Return test permutations for testClassname method. Format:
  150. * classname
  151. * type
  152. * suffix
  153. * expected return value
  154. *
  155. * @return void
  156. */
  157. public function shortNameProvider()
  158. {
  159. return [
  160. ['TestApp\In\ExistsApp', 'In', 'App', 'Exists'],
  161. ['TestApp\In\Also\ExistsApp', 'In', 'App', 'Also/Exists'],
  162. ['TestApp\Exists\In\AlsoApp', 'Exists/In', 'App', 'Also'],
  163. ['TestApp\Exists\In\Subfolder\AlsoApp', 'Exists/In/Subfolder', 'App', 'Also'],
  164. ['TestApp\Suffix\No', 'Suffix', '', 'No'],
  165. ['MyPlugin\In\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Exists'],
  166. ['MyPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Also/Exists'],
  167. ['MyPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'MyPlugin.Also'],
  168. ['MyPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'MyPlugin.Also'],
  169. ['MyPlugin\Suffix\No', 'Suffix', '', 'MyPlugin.No'],
  170. ['Vend\MPlugin\In\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Exists'],
  171. ['Vend\MPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Also/Exists'],
  172. ['Vend\MPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'Vend/MPlugin.Also'],
  173. ['Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'Vend/MPlugin.Also'],
  174. ['Vend\MPlugin\Suffix\No', 'Suffix', '', 'Vend/MPlugin.No'],
  175. ['Cake\In\ExistsCake', 'In', 'Cake', 'Exists'],
  176. ['Cake\In\Also\ExistsCake', 'In', 'Cake', 'Also/Exists'],
  177. ['Cake\Exists\In\AlsoCake', 'Exists/In', 'Cake', 'Also'],
  178. ['Cake\Exists\In\Subfolder\AlsoCake', 'Exists/In/Subfolder', 'Cake', 'Also'],
  179. ['Cake\Suffix\No', 'Suffix', '', 'No'],
  180. ['Muffin\Webservice\Webservice\EndpointWebservice', 'Webservice', 'Webservice', 'Muffin/Webservice.Endpoint'],
  181. // Real examples returning classnames
  182. ['Cake\Core\App', 'Core', '', 'App'],
  183. ['Cake\Controller\Component\AuthComponent', 'Controller/Component', 'Component', 'Auth'],
  184. ['Cake\Cache\Engine\FileEngine', 'Cache/Engine', 'Engine', 'File'],
  185. ['Cake\Shell\Task\CommandTask', 'Shell/Task', 'Task', 'Command'],
  186. ['Cake\Shell\Task\Upgrade\LocationsTask', 'Shell/Task', 'Task', 'Upgrade/Locations'],
  187. ['TestApp\Controller\PagesController', 'Controller', 'Controller', 'Pages'],
  188. ];
  189. }
  190. /**
  191. * test path() with a plugin.
  192. *
  193. * @return void
  194. */
  195. public function testPathWithPlugins()
  196. {
  197. $basepath = TEST_APP . 'Plugin' . DS;
  198. Plugin::load('TestPlugin');
  199. $result = App::path('Controller', 'TestPlugin');
  200. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  201. Plugin::load('Company/TestPluginThree');
  202. $result = App::path('Controller', 'Company/TestPluginThree');
  203. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  204. $this->assertPathEquals($expected, $result[0]);
  205. }
  206. /**
  207. * testCore method
  208. *
  209. * @return void
  210. */
  211. public function testCore()
  212. {
  213. $model = App::core('Model');
  214. $this->assertEquals([CAKE . 'Model' . DS], $model);
  215. $view = App::core('View');
  216. $this->assertEquals([CAKE . 'View' . DS], $view);
  217. $controller = App::core('Controller');
  218. $this->assertEquals([CAKE . 'Controller' . DS], $controller);
  219. $component = App::core('Controller/Component');
  220. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS], str_replace('/', DS, $component));
  221. $auth = App::core('Controller/Component/Auth');
  222. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS], str_replace('/', DS, $auth));
  223. $datasource = App::core('Model/Datasource');
  224. $this->assertEquals([CAKE . 'Model' . DS . 'Datasource' . DS], str_replace('/', DS, $datasource));
  225. }
  226. }