AppTest.php 8.3 KB

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