BaseApplicationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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\Console\CommandRunner;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\TestSuite\StubConsoleOutput;
  20. use Cake\Core\BasePlugin;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Container;
  23. use Cake\Core\ContainerInterface;
  24. use Cake\Event\EventInterface;
  25. use Cake\Event\EventManagerInterface;
  26. use Cake\Http\BaseApplication;
  27. use Cake\Http\MiddlewareQueue;
  28. use Cake\Http\ServerRequest;
  29. use Cake\Http\ServerRequestFactory;
  30. use Cake\Routing\RouteBuilder;
  31. use Cake\Routing\RouteCollection;
  32. use Cake\TestSuite\TestCase;
  33. use Mockery;
  34. use Psr\Http\Message\ResponseInterface;
  35. use TestPlugin\Plugin as TestPlugin;
  36. /**
  37. * Base application test.
  38. */
  39. class BaseApplicationTest extends TestCase
  40. {
  41. /**
  42. * @var \Cake\Http\BaseApplication
  43. */
  44. protected BaseApplication $app;
  45. /**
  46. * Setup
  47. */
  48. public function setUp(): void
  49. {
  50. parent::setUp();
  51. static::setAppNamespace();
  52. $this->app = new class (dirname(__DIR__, 2)) extends BaseApplication
  53. {
  54. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  55. {
  56. return $middlewareQueue;
  57. }
  58. public function events(EventManagerInterface $eventManager): EventManagerInterface
  59. {
  60. $eventManager->on('testTrue', function ($event) {
  61. return true;
  62. });
  63. return $eventManager;
  64. }
  65. };
  66. }
  67. /**
  68. * @return void
  69. */
  70. public function tearDown(): void
  71. {
  72. parent::tearDown();
  73. $this->clearPlugins();
  74. unset($this->app);
  75. }
  76. /**
  77. * Integration test for a simple controller.
  78. */
  79. public function testHandle(): void
  80. {
  81. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  82. $request = $request->withAttribute('params', [
  83. 'controller' => 'Cakes',
  84. 'action' => 'index',
  85. 'plugin' => null,
  86. 'pass' => [],
  87. ]);
  88. $app = $this->app;
  89. $result = $app->handle($request);
  90. $this->assertInstanceOf(ResponseInterface::class, $result);
  91. $this->assertSame('Hello Jane', '' . $result->getBody());
  92. $container = $app->getContainer();
  93. $this->assertSame($request, $container->get(ServerRequest::class));
  94. $this->assertSame($container, $container->get(ContainerInterface::class));
  95. }
  96. /**
  97. * Ensure that plugins with no plugin class can be loaded.
  98. * This makes adopting the new API easier
  99. */
  100. public function testAddPluginUnknownClass(): void
  101. {
  102. $app = $this->app;
  103. $app->addPlugin('PluginJs');
  104. $plugin = $app->getPlugins()->get('PluginJs');
  105. $this->assertInstanceOf(BasePlugin::class, $plugin);
  106. $this->assertSame(
  107. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
  108. $plugin->getPath()
  109. );
  110. $this->assertSame(
  111. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
  112. $plugin->getConfigPath()
  113. );
  114. $this->assertSame(
  115. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
  116. $plugin->getClassPath()
  117. );
  118. }
  119. public function testAddPluginValidShortName(): void
  120. {
  121. $app = $this->app;
  122. $app->addPlugin('TestPlugin');
  123. $this->assertCount(1, $app->getPlugins());
  124. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  125. $app->addPlugin('Company/TestPluginThree');
  126. $this->assertCount(2, $app->getPlugins());
  127. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  128. }
  129. public function testAddPluginValid(): void
  130. {
  131. $app = $this->app;
  132. $app->addPlugin(TestPlugin::class);
  133. $this->assertCount(1, $app->getPlugins());
  134. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  135. }
  136. public function testPluginMiddleware(): void
  137. {
  138. $start = new MiddlewareQueue();
  139. $app = $this->app;
  140. $app->addPlugin(TestPlugin::class);
  141. $after = $app->pluginMiddleware($start);
  142. $this->assertSame($start, $after);
  143. $this->assertCount(1, $after);
  144. }
  145. public function testPluginRoutes(): void
  146. {
  147. $collection = new RouteCollection();
  148. $routes = new RouteBuilder($collection, '/');
  149. $app = $this->app;
  150. $app->addPlugin(TestPlugin::class);
  151. $result = $app->pluginRoutes($routes);
  152. $this->assertSame($routes, $result);
  153. $url = [
  154. 'plugin' => 'TestPlugin',
  155. 'controller' => 'TestPlugin',
  156. 'action' => 'index',
  157. '_method' => 'GET',
  158. ];
  159. $this->assertNotEmpty($collection->match($url, []));
  160. }
  161. public function testAppBootstrapPlugins(): void
  162. {
  163. $app = new class (dirname(__DIR__, 2) . DS . 'test_app' . DS . 'config_plugins') extends BaseApplication
  164. {
  165. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  166. {
  167. return $middlewareQueue;
  168. }
  169. };
  170. $app->bootstrap();
  171. $this->assertTrue($app->getPlugins()->has('TestPlugin'), 'TestPlugin was not loaded via plugins.php');
  172. }
  173. public function testPluginBootstrap(): void
  174. {
  175. $app = $this->app;
  176. $app->addPlugin(TestPlugin::class);
  177. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  178. $app->pluginBootstrap();
  179. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  180. }
  181. /**
  182. * Test that plugins loaded with addPlugin() can load additional
  183. * plugins.
  184. */
  185. public function testPluginBootstrapRecursivePlugins(): void
  186. {
  187. $app = $this->app;
  188. $app->addPlugin('Named');
  189. $app->pluginBootstrap();
  190. $this->assertTrue(
  191. Configure::check('Named.bootstrap'),
  192. 'Plugin bootstrap should be run'
  193. );
  194. $this->assertTrue(
  195. Configure::check('PluginTest.test_plugin.bootstrap'),
  196. 'Nested plugin should have bootstrap run'
  197. );
  198. $this->assertTrue(
  199. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  200. 'Nested plugin should have bootstrap run'
  201. );
  202. }
  203. /**
  204. * Tests that loading a nonexistent plugin through addOptionalPlugin() does not throw an exception
  205. */
  206. public function testAddOptionalPluginLoadingNonExistentPlugin(): void
  207. {
  208. $app = $this->app;
  209. $pluginCountBefore = count($app->getPlugins());
  210. $nonExistingPlugin = 'NonExistentPlugin';
  211. $app->addOptionalPlugin($nonExistingPlugin);
  212. $pluginCountAfter = count($app->getPlugins());
  213. $this->assertSame($pluginCountBefore, $pluginCountAfter);
  214. }
  215. /**
  216. * Tests that loading an existing plugin through addOptionalPlugin() works
  217. */
  218. public function testAddOptionalPluginLoadingNonExistentPluginValid(): void
  219. {
  220. $app = $this->app;
  221. $app->addOptionalPlugin(TestPlugin::class);
  222. $this->assertCount(1, $app->getPlugins());
  223. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  224. }
  225. public function testGetContainer(): void
  226. {
  227. $app = $this->app;
  228. $container = $app->getContainer();
  229. $this->assertInstanceOf(ContainerInterface::class, $container);
  230. $this->assertSame($container, $app->getContainer(), 'Should return a reference');
  231. }
  232. public function testBuildContainerEvent(): void
  233. {
  234. $app = $this->app;
  235. $called = false;
  236. $app->getEventManager()->on('Application.buildContainer', function ($event, $container) use (&$called): void {
  237. $this->assertInstanceOf(BaseApplication::class, $event->getSubject());
  238. $this->assertInstanceOf(ContainerInterface::class, $container);
  239. $called = true;
  240. });
  241. $container = $app->getContainer();
  242. $this->assertInstanceOf(ContainerInterface::class, $container);
  243. $this->assertTrue($called, 'Listener should be called');
  244. }
  245. public function testBuildContainerEventReplaceContainer(): void
  246. {
  247. $app = $this->app;
  248. $app->getEventManager()->on('Application.buildContainer', function (EventInterface $event): void {
  249. $new = new Container();
  250. $new->add('testing', 'yes');
  251. $event->setResult($new);
  252. });
  253. $container = $app->getContainer();
  254. $this->assertInstanceOf(ContainerInterface::class, $container);
  255. $this->assertTrue($container->has('testing'));
  256. }
  257. public function testEventsAreRegistered(): void
  258. {
  259. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  260. $request = $request->withAttribute('params', [
  261. 'controller' => 'Cakes',
  262. 'action' => 'index',
  263. 'plugin' => null,
  264. 'pass' => [],
  265. ]);
  266. $app = $this->app;
  267. $app->handle($request);
  268. $this->assertNotEmpty($app->getEventManager()->listeners('testTrue'));
  269. }
  270. public function testConsoleEventsAreRegistered(): void
  271. {
  272. static::setAppNamespace();
  273. $app = new class (dirname(__DIR__, 2)) extends BaseApplication
  274. {
  275. public function routes(RouteBuilder $routes): void
  276. {
  277. }
  278. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  279. {
  280. return $middlewareQueue;
  281. }
  282. public function events(EventManagerInterface $eventManager): EventManagerInterface
  283. {
  284. $eventManager->on('testTrue', function ($event) {
  285. return true;
  286. });
  287. return $eventManager;
  288. }
  289. };
  290. $output = new StubConsoleOutput();
  291. $consoleIo = Mockery::mock(ConsoleIo::class, [$output, $output, null, null])
  292. ->shouldAllowMockingMethod('in')
  293. ->makePartial();
  294. $runner = new CommandRunner($app);
  295. $runner->run(['cake', 'version'], $consoleIo);
  296. $this->assertNotEmpty($app->getEventManager()->listeners('testTrue'));
  297. }
  298. }