BaseApplicationTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Core\BasePlugin;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Http\BaseApplication;
  20. use Cake\Http\MiddlewareQueue;
  21. use Cake\Http\Response;
  22. use Cake\Http\ServerRequestFactory;
  23. use Cake\Routing\RouteBuilder;
  24. use Cake\Routing\RouteCollection;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use InvalidArgumentException;
  28. use Psr\Http\Message\ResponseInterface;
  29. use TestPlugin\Plugin as TestPlugin;
  30. /**
  31. * Base application test.
  32. */
  33. class BaseApplicationTest extends TestCase
  34. {
  35. /**
  36. * Setup
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. parent::setUp();
  43. static::setAppNamespace();
  44. $this->path = dirname(dirname(__DIR__));
  45. }
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. Plugin::unload();
  50. }
  51. /**
  52. * Integration test for a simple controller.
  53. *
  54. * @return void
  55. */
  56. public function testInvoke()
  57. {
  58. $next = function ($req, $res) {
  59. return $res;
  60. };
  61. $response = new Response();
  62. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  63. $request = $request->withAttribute('params', [
  64. 'controller' => 'Cakes',
  65. 'action' => 'index',
  66. 'plugin' => null,
  67. 'pass' => []
  68. ]);
  69. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  70. $result = $app($request, $response, $next);
  71. $this->assertInstanceOf(ResponseInterface::class, $result);
  72. $this->assertEquals('Hello Jane', '' . $result->getBody());
  73. }
  74. /**
  75. * Ensure that plugins with no plugin class can be loaded.
  76. * This makes adopting the new API easier
  77. */
  78. public function testAddPluginUnknownClass()
  79. {
  80. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  81. $app->addPlugin('PluginJs');
  82. $plugin = $app->getPlugins()->get('PluginJs');
  83. $this->assertInstanceOf(BasePlugin::class, $plugin);
  84. $this->assertEquals(
  85. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
  86. $plugin->getPath()
  87. );
  88. $this->assertEquals(
  89. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
  90. $plugin->getConfigPath()
  91. );
  92. $this->assertEquals(
  93. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
  94. $plugin->getClassPath()
  95. );
  96. }
  97. /**
  98. * Ensure that plugin interfaces are implemented.
  99. */
  100. public function testAddPluginBadClass()
  101. {
  102. $this->expectException(InvalidArgumentException::class);
  103. $this->expectExceptionMessage('does not implement');
  104. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  105. $app->addPlugin(__CLASS__);
  106. }
  107. public function testAddPluginValidShortName()
  108. {
  109. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  110. $app->addPlugin('TestPlugin');
  111. $this->assertCount(1, $app->getPlugins());
  112. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  113. $app->addPlugin('Company/TestPluginThree');
  114. $this->assertCount(2, $app->getPlugins());
  115. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  116. }
  117. public function testAddPluginValid()
  118. {
  119. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  120. $app->addPlugin(TestPlugin::class);
  121. $this->assertCount(1, $app->getPlugins());
  122. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  123. }
  124. public function testPluginMiddleware()
  125. {
  126. $start = new MiddlewareQueue();
  127. $app = $this->getMockForAbstractClass(
  128. BaseApplication::class,
  129. [$this->path]
  130. );
  131. $app->addPlugin(TestPlugin::class);
  132. $after = $app->pluginMiddleware($start);
  133. $this->assertSame($start, $after);
  134. $this->assertCount(1, $after);
  135. }
  136. public function testPluginRoutes()
  137. {
  138. $collection = new RouteCollection();
  139. $routes = new RouteBuilder($collection, '/');
  140. $app = $this->getMockForAbstractClass(
  141. BaseApplication::class,
  142. [$this->path]
  143. );
  144. $app->addPlugin(TestPlugin::class);
  145. $result = $app->pluginRoutes($routes);
  146. $this->assertSame($routes, $result);
  147. $url = [
  148. 'plugin' => 'TestPlugin',
  149. 'controller' => 'TestPlugin',
  150. 'action' => 'index',
  151. '_method' => 'GET'
  152. ];
  153. $this->assertNotEmpty($collection->match($url, []));
  154. }
  155. public function testPluginBootstrap()
  156. {
  157. $app = $this->getMockForAbstractClass(
  158. BaseApplication::class,
  159. [$this->path]
  160. );
  161. $app->addPlugin(TestPlugin::class);
  162. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  163. $this->assertNull($app->pluginBootstrap());
  164. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  165. }
  166. /**
  167. * Ensure that plugins loaded via Plugin::load()
  168. * don't have their bootstrapping run twice.
  169. *
  170. * @return void
  171. */
  172. public function testPluginBootstrapInteractWithPluginLoad()
  173. {
  174. Plugin::load('TestPlugin', ['bootstrap' => true]);
  175. $app = $this->getMockForAbstractClass(
  176. BaseApplication::class,
  177. [$this->path]
  178. );
  179. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  180. Configure::delete('PluginTest.test_plugin.bootstrap');
  181. $this->assertNull($app->pluginBootstrap());
  182. $this->assertFalse(
  183. Configure::check('PluginTest.test_plugin.bootstrap'),
  184. 'Key should not be set, as plugin has already had bootstrap run'
  185. );
  186. }
  187. /**
  188. * Test that plugins loaded with addPlugin() can load additional
  189. * plugins.
  190. *
  191. * @return void
  192. */
  193. public function testPluginBootstrapRecursivePlugins()
  194. {
  195. $app = $this->getMockForAbstractClass(
  196. BaseApplication::class,
  197. [$this->path]
  198. );
  199. $app->addPlugin('ParentPlugin');
  200. $app->pluginBootstrap();
  201. $this->assertTrue(
  202. Configure::check('ParentPlugin.bootstrap'),
  203. 'Plugin bootstrap should be run'
  204. );
  205. $this->assertTrue(
  206. Configure::check('PluginTest.test_plugin.bootstrap'),
  207. 'Nested plugin should have bootstrap run'
  208. );
  209. $this->assertTrue(
  210. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  211. 'Nested plugin should have bootstrap run'
  212. );
  213. }
  214. /**
  215. * Ensure that Router::$initialized is toggled even if the routes
  216. * file fails. This prevents the routes file from being re-parsed
  217. * during the error handling process.
  218. *
  219. * @return void
  220. */
  221. public function testRouteHookInitializesRouterOnError()
  222. {
  223. $app = $this->getMockForAbstractClass(
  224. 'Cake\Http\BaseApplication',
  225. [TEST_APP . 'invalid_routes' . DS]
  226. );
  227. $builder = Router::createRouteBuilder('/');
  228. try {
  229. $app->routes($builder);
  230. $this->fail('invalid_routes/routes.php file should raise an error.');
  231. } catch (\InvalidArgumentException $e) {
  232. $this->assertTrue(Router::$initialized, 'Should be toggled to prevent duplicate route errors');
  233. $this->assertContains('route class', $e->getMessage());
  234. }
  235. }
  236. }