AppTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @since CakePHP(tm) v 2.0
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. namespace Cake\Test\TestCase\Core;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. use TestApp\Core\TestApp;
  22. /**
  23. * AppTest class
  24. *
  25. */
  26. class AppTest extends TestCase {
  27. /**
  28. * tearDown method
  29. *
  30. * @return void
  31. */
  32. public function tearDown() {
  33. parent::tearDown();
  34. Plugin::unload();
  35. }
  36. /**
  37. * testClassname
  38. *
  39. * $checkCake and $existsInCake are derived from the input parameters
  40. *
  41. * @dataProvider classnameProvider
  42. * @return void
  43. */
  44. public function testClassname($class, $type, $suffix = '', $existsInBase = false, $expected = false) {
  45. Configure::write('App.namespace', 'TestApp');
  46. $i = 0;
  47. TestApp::$existsInBaseCallback = function($name, $namespace) use ($existsInBase, $class, $expected, &$i) {
  48. if ($i++ === 0) {
  49. return $existsInBase;
  50. }
  51. $checkCake = (!$existsInBase || strpos('.', $class));
  52. if ($checkCake) {
  53. return (bool)$expected;
  54. }
  55. return false;
  56. };
  57. $return = TestApp::classname($class, $type, $suffix);
  58. $this->assertSame($expected, $return);
  59. }
  60. /**
  61. * classnameProvider
  62. *
  63. * Return test permutations for testClassname method. Format:
  64. * classname
  65. * type
  66. * suffix
  67. * existsInBase (Base meaning App or plugin namespace)
  68. * expected return value
  69. *
  70. * @return void
  71. */
  72. public function classnameProvider() {
  73. return [
  74. ['Does', 'Not', 'Exist'],
  75. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  76. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  77. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  78. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  79. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  80. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  81. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  82. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  83. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  84. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  85. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  86. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  87. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  88. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  89. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  90. // Realistic examples returning nothing
  91. ['App', 'Core', 'Suffix'],
  92. ['Auth', 'Controller/Component'],
  93. ['Unknown', 'Controller', 'Controller'],
  94. // Real examples returning classnames
  95. ['App', 'Core', '', false, 'Cake\Core\App'],
  96. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  97. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  98. ['Command', 'Console/Command/Task', 'Task', false, 'Cake\Console\Command\Task\CommandTask'],
  99. ['Upgrade/Locations', 'Console/Command/Task', 'Task', false, 'Cake\Console\Command\Task\Upgrade\LocationsTask'],
  100. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  101. ];
  102. }
  103. /**
  104. * test path() with a plugin.
  105. *
  106. * @return void
  107. */
  108. public function testPathWithPlugins() {
  109. $basepath = TEST_APP . 'Plugin' . DS;
  110. Plugin::load('TestPlugin');
  111. $result = App::path('Controller', 'TestPlugin');
  112. $this->assertEquals($basepath . 'TestPlugin' . DS . 'Controller' . DS, $result[0]);
  113. }
  114. /**
  115. * testCore method
  116. *
  117. * @return void
  118. */
  119. public function testCore() {
  120. $model = App::core('Model');
  121. $this->assertEquals(array(CAKE . 'Model' . DS), $model);
  122. $view = App::core('View');
  123. $this->assertEquals(array(CAKE . 'View' . DS), $view);
  124. $controller = App::core('Controller');
  125. $this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
  126. $component = App::core('Controller/Component');
  127. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
  128. $auth = App::core('Controller/Component/Auth');
  129. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
  130. $datasource = App::core('Model/Datasource');
  131. $this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
  132. }
  133. /**
  134. * testListObjects method
  135. *
  136. * @return void
  137. */
  138. public function testListObjects() {
  139. $result = App::objects('class', CAKE . 'Routing', false);
  140. $this->assertTrue(in_array('Dispatcher', $result));
  141. $this->assertTrue(in_array('Router', $result));
  142. $result = App::objects('Model/Behavior', null, false);
  143. $this->assertContains('SluggableBehavior', $result);
  144. $result = App::objects('Controller/Component', null, false);
  145. $this->assertContains('AppleComponent', $result);
  146. $result = App::objects('View', null, false);
  147. $this->assertContains('CustomJsonView', $result);
  148. $result = App::objects('View/Helper', null, false);
  149. $this->assertContains('BananaHelper', $result);
  150. $result = App::objects('Model/Table', null, false);
  151. $this->assertContains('ArticlesTable', $result);
  152. $result = App::objects('file');
  153. $this->assertFalse($result);
  154. $result = App::objects('file', 'non_existing_configure');
  155. $expected = array();
  156. $this->assertEquals($expected, $result);
  157. $result = App::objects('NonExistingType');
  158. $this->assertSame(array(), $result);
  159. $result = App::objects('Plugin', null, false);
  160. $this->assertContains('TestPlugin', $result);
  161. $this->assertContains('TestPluginTwo', $result);
  162. }
  163. /**
  164. * Make sure that .svn and friends are excluded from App::objects('Plugin')
  165. */
  166. public function testListObjectsIgnoreDotDirectories() {
  167. $path = TEST_APP . 'Plugin/';
  168. $this->skipIf(!is_writable($path), $path . ' is not writable.');
  169. mkdir($path . '.svn');
  170. $result = App::objects('Plugin', null, false);
  171. rmdir($path . '.svn');
  172. $this->assertNotContains('.svn', $result);
  173. }
  174. /**
  175. * Tests listing objects within a plugin
  176. *
  177. * @return void
  178. */
  179. public function testListObjectsInPlugin() {
  180. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  181. $result = App::objects('TestPlugin.Model/Table');
  182. $this->assertContains('TestPluginCommentsTable', $result);
  183. $result = App::objects('TestPlugin.Model/Behavior');
  184. $this->assertTrue(in_array('PersisterOneBehavior', $result));
  185. $result = App::objects('TestPlugin.View/Helper');
  186. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  187. $this->assertEquals($expected, $result);
  188. $result = App::objects('TestPlugin.Controller/Component');
  189. $this->assertTrue(in_array('OtherComponent', $result));
  190. $result = App::objects('TestPluginTwo.Model/Behavior');
  191. $this->assertSame(array(), $result);
  192. $result = App::objects('Model/Table', null, false);
  193. $this->assertContains('PostsTable', $result);
  194. $this->assertContains('ArticlesTable', $result);
  195. }
  196. /**
  197. * test that pluginPath can find paths for plugins.
  198. *
  199. * @return void
  200. */
  201. public function testPluginPath() {
  202. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  203. $path = App::pluginPath('TestPlugin');
  204. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  205. $this->assertEquals($expected, $path);
  206. $path = App::pluginPath('TestPluginTwo');
  207. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  208. $this->assertEquals($expected, $path);
  209. }
  210. /**
  211. * test that themePath can find paths for themes.
  212. *
  213. * @return void
  214. */
  215. public function testThemePath() {
  216. $path = App::themePath('test_theme');
  217. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS;
  218. $this->assertEquals($expected, $path);
  219. $path = App::themePath('TestTheme');
  220. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS;
  221. $this->assertEquals($expected, $path);
  222. }
  223. }