BaseApplicationTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.5.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Core\BasePlugin;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Container;
  20. use Cake\Core\ContainerInterface;
  21. use Cake\Event\EventInterface;
  22. use Cake\Http\BaseApplication;
  23. use Cake\Http\MiddlewareQueue;
  24. use Cake\Http\ServerRequest;
  25. use Cake\Http\ServerRequestFactory;
  26. use Cake\Routing\RouteBuilder;
  27. use Cake\Routing\RouteCollection;
  28. use Cake\TestSuite\TestCase;
  29. use Psr\Http\Message\ResponseInterface;
  30. use TestPlugin\Plugin as TestPlugin;
  31. /**
  32. * Base application test.
  33. *
  34. * @coversDefaultClass \Cake\Http\BaseApplication
  35. */
  36. class BaseApplicationTest extends TestCase
  37. {
  38. /**
  39. * @var \Cake\Http\BaseApplication
  40. */
  41. protected BaseApplication $app;
  42. /**
  43. * Setup
  44. */
  45. public function setUp(): void
  46. {
  47. parent::setUp();
  48. static::setAppNamespace();
  49. $this->app = new class (dirname(__DIR__, 2)) extends BaseApplication
  50. {
  51. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  52. {
  53. return $middlewareQueue;
  54. }
  55. };
  56. }
  57. /**
  58. * @return void
  59. */
  60. public function tearDown(): void
  61. {
  62. parent::tearDown();
  63. $this->clearPlugins();
  64. unset($this->app);
  65. }
  66. /**
  67. * Integration test for a simple controller.
  68. */
  69. public function testHandle(): void
  70. {
  71. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  72. $request = $request->withAttribute('params', [
  73. 'controller' => 'Cakes',
  74. 'action' => 'index',
  75. 'plugin' => null,
  76. 'pass' => [],
  77. ]);
  78. $app = $this->app;
  79. $result = $app->handle($request);
  80. $this->assertInstanceOf(ResponseInterface::class, $result);
  81. $this->assertSame('Hello Jane', '' . $result->getBody());
  82. $container = $app->getContainer();
  83. $this->assertSame($request, $container->get(ServerRequest::class));
  84. $this->assertSame($container, $container->get(ContainerInterface::class));
  85. }
  86. /**
  87. * Ensure that plugins with no plugin class can be loaded.
  88. * This makes adopting the new API easier
  89. */
  90. public function testAddPluginUnknownClass(): void
  91. {
  92. $app = $this->app;
  93. $app->addPlugin('PluginJs');
  94. $plugin = $app->getPlugins()->get('PluginJs');
  95. $this->assertInstanceOf(BasePlugin::class, $plugin);
  96. $this->assertSame(
  97. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
  98. $plugin->getPath()
  99. );
  100. $this->assertSame(
  101. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
  102. $plugin->getConfigPath()
  103. );
  104. $this->assertSame(
  105. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
  106. $plugin->getClassPath()
  107. );
  108. }
  109. public function testAddPluginValidShortName(): void
  110. {
  111. $app = $this->app;
  112. $app->addPlugin('TestPlugin');
  113. $this->assertCount(1, $app->getPlugins());
  114. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  115. $app->addPlugin('Company/TestPluginThree');
  116. $this->assertCount(2, $app->getPlugins());
  117. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  118. }
  119. public function testAddPluginValid(): void
  120. {
  121. $app = $this->app;
  122. $app->addPlugin(TestPlugin::class);
  123. $this->assertCount(1, $app->getPlugins());
  124. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  125. }
  126. public function testPluginMiddleware(): void
  127. {
  128. $start = new MiddlewareQueue();
  129. $app = $this->app;
  130. $app->addPlugin(TestPlugin::class);
  131. $after = $app->pluginMiddleware($start);
  132. $this->assertSame($start, $after);
  133. $this->assertCount(1, $after);
  134. }
  135. public function testPluginRoutes(): void
  136. {
  137. $collection = new RouteCollection();
  138. $routes = new RouteBuilder($collection, '/');
  139. $app = $this->app;
  140. $app->addPlugin(TestPlugin::class);
  141. $result = $app->pluginRoutes($routes);
  142. $this->assertSame($routes, $result);
  143. $url = [
  144. 'plugin' => 'TestPlugin',
  145. 'controller' => 'TestPlugin',
  146. 'action' => 'index',
  147. '_method' => 'GET',
  148. ];
  149. $this->assertNotEmpty($collection->match($url, []));
  150. }
  151. public function testAppBootstrapPlugins(): void
  152. {
  153. $app = new class (dirname(__DIR__, 2) . DS . 'test_app' . DS . 'config_plugins') extends BaseApplication
  154. {
  155. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  156. {
  157. return $middlewareQueue;
  158. }
  159. };
  160. $app->bootstrap();
  161. $this->assertTrue($app->getPlugins()->has('TestPlugin'), 'TestPlugin was not loaded via plugins.php');
  162. }
  163. public function testPluginBootstrap(): void
  164. {
  165. $app = $this->app;
  166. $app->addPlugin(TestPlugin::class);
  167. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  168. $app->pluginBootstrap();
  169. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  170. }
  171. /**
  172. * Test that plugins loaded with addPlugin() can load additional
  173. * plugins.
  174. */
  175. public function testPluginBootstrapRecursivePlugins(): void
  176. {
  177. $app = $this->app;
  178. $app->addPlugin('Named');
  179. $app->pluginBootstrap();
  180. $this->assertTrue(
  181. Configure::check('Named.bootstrap'),
  182. 'Plugin bootstrap should be run'
  183. );
  184. $this->assertTrue(
  185. Configure::check('PluginTest.test_plugin.bootstrap'),
  186. 'Nested plugin should have bootstrap run'
  187. );
  188. $this->assertTrue(
  189. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  190. 'Nested plugin should have bootstrap run'
  191. );
  192. }
  193. /**
  194. * Tests that loading a nonexistent plugin through addOptionalPlugin() does not throw an exception
  195. *
  196. * @covers ::addOptionalPlugin
  197. */
  198. public function testAddOptionalPluginLoadingNonExistentPlugin(): void
  199. {
  200. $app = $this->app;
  201. $pluginCountBefore = count($app->getPlugins());
  202. $nonExistingPlugin = 'NonExistentPlugin';
  203. $app->addOptionalPlugin($nonExistingPlugin);
  204. $pluginCountAfter = count($app->getPlugins());
  205. $this->assertSame($pluginCountBefore, $pluginCountAfter);
  206. }
  207. /**
  208. * Tests that loading an existing plugin through addOptionalPlugin() works
  209. *
  210. * @covers ::addOptionalPlugin
  211. */
  212. public function testAddOptionalPluginLoadingNonExistentPluginValid(): void
  213. {
  214. $app = $this->app;
  215. $app->addOptionalPlugin(TestPlugin::class);
  216. $this->assertCount(1, $app->getPlugins());
  217. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  218. }
  219. public function testGetContainer(): void
  220. {
  221. $app = $this->app;
  222. $container = $app->getContainer();
  223. $this->assertInstanceOf(ContainerInterface::class, $container);
  224. $this->assertSame($container, $app->getContainer(), 'Should return a reference');
  225. }
  226. public function testBuildContainerEvent(): void
  227. {
  228. $app = $this->app;
  229. $called = false;
  230. $app->getEventManager()->on('Application.buildContainer', function ($event, $container) use (&$called): void {
  231. $this->assertInstanceOf(BaseApplication::class, $event->getSubject());
  232. $this->assertInstanceOf(ContainerInterface::class, $container);
  233. $called = true;
  234. });
  235. $container = $app->getContainer();
  236. $this->assertInstanceOf(ContainerInterface::class, $container);
  237. $this->assertTrue($called, 'Listener should be called');
  238. }
  239. public function testBuildContainerEventReplaceContainer(): void
  240. {
  241. $app = $this->app;
  242. $app->getEventManager()->on('Application.buildContainer', function (EventInterface $event) {
  243. $new = new Container();
  244. $new->add('testing', 'yes');
  245. $event->setResult($new);
  246. });
  247. $container = $app->getContainer();
  248. $this->assertInstanceOf(ContainerInterface::class, $container);
  249. $this->assertTrue($container->has('testing'));
  250. }
  251. }