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