BaseApplicationTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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('SomethingBad');
  82. $plugin = $app->getPlugins()->get('SomethingBad');
  83. $this->assertInstanceOf(BasePlugin::class, $plugin);
  84. }
  85. /**
  86. * Ensure that plugin interfaces are implemented.
  87. */
  88. public function testAddPluginBadClass()
  89. {
  90. $this->expectException(InvalidArgumentException::class);
  91. $this->expectExceptionMessage('does not implement');
  92. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  93. $app->addPlugin(__CLASS__);
  94. }
  95. public function testAddPluginValidShortName()
  96. {
  97. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  98. $app->addPlugin('TestPlugin');
  99. $this->assertCount(1, $app->getPlugins());
  100. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  101. $app->addPlugin('Company/TestPluginThree');
  102. $this->assertCount(2, $app->getPlugins());
  103. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  104. }
  105. public function testAddPluginValid()
  106. {
  107. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  108. $app->addPlugin(TestPlugin::class);
  109. $this->assertCount(1, $app->getPlugins());
  110. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  111. }
  112. public function testPluginMiddleware()
  113. {
  114. $start = new MiddlewareQueue();
  115. $app = $this->getMockForAbstractClass(
  116. BaseApplication::class,
  117. [$this->path]
  118. );
  119. $app->addPlugin(TestPlugin::class);
  120. $after = $app->pluginMiddleware($start);
  121. $this->assertSame($start, $after);
  122. $this->assertCount(1, $after);
  123. }
  124. public function testPluginRoutes()
  125. {
  126. $collection = new RouteCollection();
  127. $routes = new RouteBuilder($collection, '/');
  128. $app = $this->getMockForAbstractClass(
  129. BaseApplication::class,
  130. [$this->path]
  131. );
  132. $app->addPlugin(TestPlugin::class);
  133. $result = $app->pluginRoutes($routes);
  134. $this->assertSame($routes, $result);
  135. $url = [
  136. 'plugin' => 'TestPlugin',
  137. 'controller' => 'TestPlugin',
  138. 'action' => 'index',
  139. '_method' => 'GET'
  140. ];
  141. $this->assertNotEmpty($collection->match($url, []));
  142. }
  143. public function testPluginBootstrap()
  144. {
  145. $app = $this->getMockForAbstractClass(
  146. BaseApplication::class,
  147. [$this->path]
  148. );
  149. $app->addPlugin(TestPlugin::class);
  150. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  151. $this->assertNull($app->pluginBootstrap());
  152. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  153. }
  154. /**
  155. * Ensure that plugins loaded via Plugin::load()
  156. * don't have their bootstrapping run twice.
  157. *
  158. * @return void
  159. */
  160. public function testPluginBootstrapInteractWithPluginLoad()
  161. {
  162. Plugin::load('TestPlugin', ['bootstrap' => true]);
  163. $app = $this->getMockForAbstractClass(
  164. BaseApplication::class,
  165. [$this->path]
  166. );
  167. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  168. Configure::delete('PluginTest.test_plugin.bootstrap');
  169. $this->assertNull($app->pluginBootstrap());
  170. $this->assertFalse(
  171. Configure::check('PluginTest.test_plugin.bootstrap'),
  172. 'Key should not be set, as plugin has already had bootstrap run'
  173. );
  174. }
  175. /**
  176. * Test that plugins loaded with addPlugin() can load additional
  177. * plugins.
  178. *
  179. * @return void
  180. */
  181. public function testPluginBootstrapRecursivePlugins()
  182. {
  183. $app = $this->getMockForAbstractClass(
  184. BaseApplication::class,
  185. [$this->path]
  186. );
  187. $app->addPlugin('ParentPlugin');
  188. $app->pluginBootstrap();
  189. $this->assertTrue(
  190. Configure::check('ParentPlugin.bootstrap'),
  191. 'Plugin bootstrap should be run'
  192. );
  193. $this->assertTrue(
  194. Configure::check('PluginTest.test_plugin.bootstrap'),
  195. 'Nested plugin should have bootstrap run'
  196. );
  197. $this->assertTrue(
  198. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  199. 'Nested plugin should have bootstrap run'
  200. );
  201. }
  202. /**
  203. * Ensure that Router::$initialized is toggled even if the routes
  204. * file fails. This prevents the routes file from being re-parsed
  205. * during the error handling process.
  206. *
  207. * @return void
  208. */
  209. public function testRouteHookInitializesRouterOnError()
  210. {
  211. $app = $this->getMockForAbstractClass(
  212. 'Cake\Http\BaseApplication',
  213. [TEST_APP . 'invalid_routes' . DS]
  214. );
  215. $builder = Router::createRouteBuilder('/');
  216. try {
  217. $app->routes($builder);
  218. $this->fail('invalid_routes/routes.php file should raise an error.');
  219. } catch (\InvalidArgumentException $e) {
  220. $this->assertTrue(Router::$initialized, 'Should be toggled to prevent duplicate route errors');
  221. $this->assertContains('route class', $e->getMessage());
  222. }
  223. }
  224. }