AppTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 method
  37. *
  38. * @return void
  39. */
  40. public function testClassname() {
  41. Configure::write('App.namespace', 'TestApp');
  42. // Test core
  43. $this->assertEquals('Cake\Core\App', App::classname('App', 'Core'));
  44. $this->assertFalse(App::classname('App', 'Core', 'Suffix'));
  45. // Assert prefix
  46. $this->assertFalse(App::classname('Auth', 'Controller/Component'));
  47. $this->assertEquals('Cake\Controller\Component\AuthComponent', App::classname('Auth', 'Controller/Component', 'Component'));
  48. // Test app
  49. $this->assertEquals('TestApp\Controller\PagesController', App::classname('Pages', 'Controller', 'Controller'));
  50. $this->assertFalse(App::classname('Unknown', 'Controller', 'Controller'));
  51. // Test plugin
  52. Plugin::load('TestPlugin');
  53. $this->assertEquals('TestPlugin\Utility\TestPluginEngine', App::classname('TestPlugin.TestPlugin', 'Utility', 'Engine'));
  54. $this->assertFalse(App::classname('TestPlugin.Unknown', 'Utility'));
  55. $this->assertFalse(Plugin::loaded('TestPluginTwo'), 'TestPluginTwo should not be loaded.');
  56. // Test unknown plugin
  57. $this->assertEquals(
  58. 'TestPluginTwo\Console\Command\ExampleShell',
  59. App::classname('TestPluginTwo.Example', 'Console/Command', 'Shell')
  60. );
  61. }
  62. /**
  63. * test path() with a plugin.
  64. *
  65. * @return void
  66. */
  67. public function testPathWithPlugins() {
  68. $basepath = CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS;
  69. Plugin::load('TestPlugin');
  70. $result = App::path('Controller', 'TestPlugin');
  71. $this->assertEquals($basepath . 'TestPlugin' . DS . 'Controller' . DS, $result[0]);
  72. }
  73. /**
  74. * testCore method
  75. *
  76. * @return void
  77. */
  78. public function testCore() {
  79. $model = App::core('Model');
  80. $this->assertEquals(array(CAKE . 'Model' . DS), $model);
  81. $view = App::core('View');
  82. $this->assertEquals(array(CAKE . 'View' . DS), $view);
  83. $controller = App::core('Controller');
  84. $this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
  85. $component = App::core('Controller/Component');
  86. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
  87. $auth = App::core('Controller/Component/Auth');
  88. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
  89. $datasource = App::core('Model/Datasource');
  90. $this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
  91. }
  92. /**
  93. * testListObjects method
  94. *
  95. * @return void
  96. */
  97. public function testListObjects() {
  98. $result = App::objects('class', CAKE . 'Routing', false);
  99. $this->assertTrue(in_array('Dispatcher', $result));
  100. $this->assertTrue(in_array('Router', $result));
  101. $result = App::objects('Model/Behavior', null, false);
  102. $this->assertContains('PersisterOneBehaviorBehavior', $result);
  103. $result = App::objects('Controller/Component', null, false);
  104. $this->assertContains('AppleComponent', $result);
  105. $result = App::objects('View', null, false);
  106. $this->assertContains('CustomJsonView', $result);
  107. $result = App::objects('View/Helper', null, false);
  108. $this->assertContains('BananaHelper', $result);
  109. $result = App::objects('Model', null, false);
  110. $this->assertContains('Article', $result);
  111. $result = App::objects('file');
  112. $this->assertFalse($result);
  113. $result = App::objects('file', 'non_existing_configure');
  114. $expected = array();
  115. $this->assertEquals($expected, $result);
  116. $result = App::objects('NonExistingType');
  117. $this->assertSame(array(), $result);
  118. $result = App::objects('Plugin', null, false);
  119. $this->assertContains('TestPlugin', $result);
  120. $this->assertContains('TestPluginTwo', $result);
  121. }
  122. /**
  123. * Make sure that .svn and friends are excluded from App::objects('Plugin')
  124. */
  125. public function testListObjectsIgnoreDotDirectories() {
  126. $path = CAKE . 'Test/TestApp/Plugin/';
  127. $this->skipIf(!is_writable($path), $path . ' is not writable.');
  128. mkdir($path . '.svn');
  129. $result = App::objects('Plugin', null, false);
  130. rmdir($path . '.svn');
  131. $this->assertNotContains('.svn', $result);
  132. }
  133. /**
  134. * Tests listing objects within a plugin
  135. *
  136. * @return void
  137. */
  138. public function testListObjectsInPlugin() {
  139. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  140. $result = App::objects('TestPlugin.Model');
  141. $this->assertTrue(in_array('TestPluginPost', $result));
  142. $result = App::objects('TestPlugin.Model/Behavior');
  143. $this->assertTrue(in_array('TestPluginPersisterOneBehavior', $result));
  144. $result = App::objects('TestPlugin.View/Helper');
  145. $expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
  146. $this->assertEquals($expected, $result);
  147. $result = App::objects('TestPlugin.Controller/Component');
  148. $this->assertTrue(in_array('OtherComponent', $result));
  149. $result = App::objects('TestPluginTwo.Model/Behavior');
  150. $this->assertSame(array(), $result);
  151. $result = App::objects('Model', null, false);
  152. $this->assertTrue(in_array('Comment', $result));
  153. $this->assertTrue(in_array('Post', $result));
  154. }
  155. /**
  156. * test that pluginPath can find paths for plugins.
  157. *
  158. * @return void
  159. */
  160. public function testPluginPath() {
  161. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  162. $path = App::pluginPath('TestPlugin');
  163. $expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  164. $this->assertEquals($expected, $path);
  165. $path = App::pluginPath('TestPluginTwo');
  166. $expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
  167. $this->assertEquals($expected, $path);
  168. }
  169. /**
  170. * test that themePath can find paths for themes.
  171. *
  172. * @return void
  173. */
  174. public function testThemePath() {
  175. $path = App::themePath('test_theme');
  176. $expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  177. $this->assertEquals($expected, $path);
  178. $path = App::themePath('TestTheme');
  179. $expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  180. $this->assertEquals($expected, $path);
  181. }
  182. }