AppTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * classnameProvider
  84. *
  85. * Return test permutations for testClassname method. Format:
  86. * classname
  87. * type
  88. * suffix
  89. * existsInBase (Base meaning App or plugin namespace)
  90. * expected return value
  91. *
  92. * @return void
  93. */
  94. public function classnameProvider()
  95. {
  96. return [
  97. ['Does', 'Not', 'Exist'],
  98. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  99. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  100. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  101. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  102. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  103. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  104. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  105. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  106. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  107. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  108. ['Vend/MPlugin.Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\ExistsSuffix'],
  109. ['Vend/MPlugin.Also/Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\Also\ExistsSuffix'],
  110. ['Vend/MPlugin.Also', 'Exists/In', 'Suffix', true, 'Vend\MPlugin\Exists\In\AlsoSuffix'],
  111. ['Vend/MPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix'],
  112. ['Vend/MPlugin.No', 'Suffix', '', true, 'Vend\MPlugin\Suffix\No'],
  113. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  114. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  115. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  116. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  117. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  118. // Realistic examples returning nothing
  119. ['App', 'Core', 'Suffix'],
  120. ['Auth', 'Controller/Component'],
  121. ['Unknown', 'Controller', 'Controller'],
  122. // Real examples returning classnames
  123. ['App', 'Core', '', false, 'Cake\Core\App'],
  124. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  125. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  126. ['Command', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\CommandTask'],
  127. ['Upgrade/Locations', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\Upgrade\LocationsTask'],
  128. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  129. ];
  130. }
  131. /**
  132. * pluginSplitNameProvider
  133. *
  134. * Return test permutations for testClassname method. Format:
  135. * classname
  136. * type
  137. * suffix
  138. * expected return value
  139. *
  140. * @return void
  141. */
  142. public function shortNameProvider()
  143. {
  144. return [
  145. ['TestApp\In\ExistsApp', 'In', 'App', 'Exists'],
  146. ['TestApp\In\Also\ExistsApp', 'In', 'App', 'Also/Exists'],
  147. ['TestApp\Exists\In\AlsoApp', 'Exists/In', 'App', 'Also'],
  148. ['TestApp\Exists\In\Subfolder\AlsoApp', 'Exists/In/Subfolder', 'App', 'Also'],
  149. ['TestApp\Suffix\No', 'Suffix', '', 'No'],
  150. ['MyPlugin\In\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Exists'],
  151. ['MyPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'MyPlugin.Also/Exists'],
  152. ['MyPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'MyPlugin.Also'],
  153. ['MyPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'MyPlugin.Also'],
  154. ['MyPlugin\Suffix\No', 'Suffix', '', 'MyPlugin.No'],
  155. ['Vend\MPlugin\In\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Exists'],
  156. ['Vend\MPlugin\In\Also\ExistsSuffix', 'In', 'Suffix', 'Vend/MPlugin.Also/Exists'],
  157. ['Vend\MPlugin\Exists\In\AlsoSuffix', 'Exists/In', 'Suffix', 'Vend/MPlugin.Also'],
  158. ['Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix', 'Exists/In/Subfolder', 'Suffix', 'Vend/MPlugin.Also'],
  159. ['Vend\MPlugin\Suffix\No', 'Suffix', '', 'Vend/MPlugin.No'],
  160. ['Cake\In\ExistsCake', 'In', 'Cake', 'Exists'],
  161. ['Cake\In\Also\ExistsCake', 'In', 'Cake', 'Also/Exists'],
  162. ['Cake\Exists\In\AlsoCake', 'Exists/In', 'Cake', 'Also'],
  163. ['Cake\Exists\In\Subfolder\AlsoCake', 'Exists/In/Subfolder', 'Cake', 'Also'],
  164. ['Cake\Suffix\No', 'Suffix', '', 'No'],
  165. // Real examples returning classnames
  166. ['Cake\Core\App', 'Core', '', 'App'],
  167. ['Cake\Controller\Component\AuthComponent', 'Controller/Component', 'Component', 'Auth'],
  168. ['Cake\Cache\Engine\FileEngine', 'Cache/Engine', 'Engine', 'File'],
  169. ['Cake\Shell\Task\CommandTask', 'Shell/Task', 'Task', 'Command'],
  170. ['Cake\Shell\Task\Upgrade\LocationsTask', 'Shell/Task', 'Task', 'Upgrade/Locations'],
  171. ['TestApp\Controller\PagesController', 'Controller', 'Controller', 'Pages'],
  172. ];
  173. }
  174. /**
  175. * test path() with a plugin.
  176. *
  177. * @return void
  178. */
  179. public function testPathWithPlugins()
  180. {
  181. $basepath = TEST_APP . 'Plugin' . DS;
  182. Plugin::load('TestPlugin');
  183. $result = App::path('Controller', 'TestPlugin');
  184. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  185. Plugin::load('Company/TestPluginThree');
  186. $result = App::path('Controller', 'Company/TestPluginThree');
  187. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  188. $this->assertPathEquals($expected, $result[0]);
  189. }
  190. /**
  191. * testCore method
  192. *
  193. * @return void
  194. */
  195. public function testCore()
  196. {
  197. $model = App::core('Model');
  198. $this->assertEquals([CAKE . 'Model' . DS], $model);
  199. $view = App::core('View');
  200. $this->assertEquals([CAKE . 'View' . DS], $view);
  201. $controller = App::core('Controller');
  202. $this->assertEquals([CAKE . 'Controller' . DS], $controller);
  203. $component = App::core('Controller/Component');
  204. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS], str_replace('/', DS, $component));
  205. $auth = App::core('Controller/Component/Auth');
  206. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS], str_replace('/', DS, $auth));
  207. $datasource = App::core('Model/Datasource');
  208. $this->assertEquals([CAKE . 'Model' . DS . 'Datasource' . DS], str_replace('/', DS, $datasource));
  209. }
  210. }