BaseApplicationTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\Controller\ComponentRegistry;
  18. use Cake\Core\BasePlugin;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Container;
  21. use Cake\Core\ContainerInterface;
  22. use Cake\Event\EventInterface;
  23. use Cake\Http\BaseApplication;
  24. use Cake\Http\MiddlewareQueue;
  25. use Cake\Http\ServerRequest;
  26. use Cake\Http\ServerRequestFactory;
  27. use Cake\Routing\RouteBuilder;
  28. use Cake\Routing\RouteCollection;
  29. use Cake\TestSuite\TestCase;
  30. use Psr\Http\Message\ResponseInterface;
  31. use TestPlugin\Plugin as TestPlugin;
  32. /**
  33. * Base application test.
  34. *
  35. * @coversDefaultClass \Cake\Http\BaseApplication
  36. */
  37. class BaseApplicationTest extends TestCase
  38. {
  39. /**
  40. * @var string
  41. */
  42. protected $path;
  43. /**
  44. * Setup
  45. */
  46. public function setUp(): void
  47. {
  48. parent::setUp();
  49. static::setAppNamespace();
  50. $this->path = dirname(dirname(__DIR__));
  51. }
  52. public function tearDown(): void
  53. {
  54. parent::tearDown();
  55. $this->clearPlugins();
  56. }
  57. /**
  58. * Integration test for a simple controller.
  59. */
  60. public function testHandle(): void
  61. {
  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->handle($request);
  71. $this->assertInstanceOf(ResponseInterface::class, $result);
  72. $this->assertSame('Hello Jane', '' . $result->getBody());
  73. $container = $app->getContainer();
  74. $this->assertSame($request, $container->get(ServerRequest::class));
  75. $this->assertSame($container, $container->get(ContainerInterface::class));
  76. $this->assertInstanceOf(ComponentRegistry::class, $container->get(ComponentRegistry::class));
  77. }
  78. /**
  79. * Ensure that plugins with no plugin class can be loaded.
  80. * This makes adopting the new API easier
  81. */
  82. public function testAddPluginUnknownClass(): void
  83. {
  84. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  85. $app->addPlugin('PluginJs');
  86. $plugin = $app->getPlugins()->get('PluginJs');
  87. $this->assertInstanceOf(BasePlugin::class, $plugin);
  88. $this->assertSame(
  89. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
  90. $plugin->getPath()
  91. );
  92. $this->assertSame(
  93. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
  94. $plugin->getConfigPath()
  95. );
  96. $this->assertSame(
  97. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
  98. $plugin->getClassPath()
  99. );
  100. }
  101. public function testAddPluginValidShortName(): void
  102. {
  103. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  104. $app->addPlugin('TestPlugin');
  105. $this->assertCount(1, $app->getPlugins());
  106. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  107. $app->addPlugin('Company/TestPluginThree');
  108. $this->assertCount(2, $app->getPlugins());
  109. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  110. }
  111. public function testAddPluginValid(): void
  112. {
  113. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  114. $app->addPlugin(TestPlugin::class);
  115. $this->assertCount(1, $app->getPlugins());
  116. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  117. }
  118. public function testPluginMiddleware(): void
  119. {
  120. $start = new MiddlewareQueue();
  121. $app = $this->getMockForAbstractClass(
  122. BaseApplication::class,
  123. [$this->path]
  124. );
  125. $app->addPlugin(TestPlugin::class);
  126. $after = $app->pluginMiddleware($start);
  127. $this->assertSame($start, $after);
  128. $this->assertCount(1, $after);
  129. }
  130. public function testPluginRoutes(): void
  131. {
  132. $collection = new RouteCollection();
  133. $routes = new RouteBuilder($collection, '/');
  134. $app = $this->getMockForAbstractClass(
  135. BaseApplication::class,
  136. [$this->path]
  137. );
  138. $app->addPlugin(TestPlugin::class);
  139. $result = $app->pluginRoutes($routes);
  140. $this->assertSame($routes, $result);
  141. $url = [
  142. 'plugin' => 'TestPlugin',
  143. 'controller' => 'TestPlugin',
  144. 'action' => 'index',
  145. '_method' => 'GET',
  146. ];
  147. $this->assertNotEmpty($collection->match($url, []));
  148. }
  149. public function testPluginBootstrap(): void
  150. {
  151. $app = $this->getMockForAbstractClass(
  152. BaseApplication::class,
  153. [$this->path]
  154. );
  155. $app->addPlugin(TestPlugin::class);
  156. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  157. $app->pluginBootstrap();
  158. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  159. }
  160. /**
  161. * Test that plugins loaded with addPlugin() can load additional
  162. * plugins.
  163. */
  164. public function testPluginBootstrapRecursivePlugins(): void
  165. {
  166. $app = $this->getMockForAbstractClass(
  167. BaseApplication::class,
  168. [$this->path]
  169. );
  170. $app->addPlugin('Named');
  171. $app->pluginBootstrap();
  172. $this->assertTrue(
  173. Configure::check('Named.bootstrap'),
  174. 'Plugin bootstrap should be run'
  175. );
  176. $this->assertTrue(
  177. Configure::check('PluginTest.test_plugin.bootstrap'),
  178. 'Nested plugin should have bootstrap run'
  179. );
  180. $this->assertTrue(
  181. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  182. 'Nested plugin should have bootstrap run'
  183. );
  184. }
  185. /**
  186. * Tests that loading a nonexistent plugin through addOptionalPlugin() does not throw an exception
  187. *
  188. * @covers ::addOptionalPlugin
  189. */
  190. public function testAddOptionalPluginLoadingNonExistentPlugin(): void
  191. {
  192. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  193. $pluginCountBefore = count($app->getPlugins());
  194. $nonExistingPlugin = 'NonExistentPlugin';
  195. $app->addOptionalPlugin($nonExistingPlugin);
  196. $pluginCountAfter = count($app->getPlugins());
  197. $this->assertSame($pluginCountBefore, $pluginCountAfter);
  198. }
  199. /**
  200. * Tests that loading an existing plugin through addOptionalPlugin() works
  201. *
  202. * @covers ::addOptionalPlugin
  203. */
  204. public function testAddOptionalPluginLoadingNonExistentPluginValid(): void
  205. {
  206. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  207. $app->addOptionalPlugin(TestPlugin::class);
  208. $this->assertCount(1, $app->getPlugins());
  209. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  210. }
  211. public function testGetContainer(): void
  212. {
  213. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  214. $container = $app->getContainer();
  215. $this->assertInstanceOf(ContainerInterface::class, $container);
  216. $this->assertSame($container, $app->getContainer(), 'Should return a reference');
  217. }
  218. public function testBuildContainerEvent(): void
  219. {
  220. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  221. $called = false;
  222. $app->getEventManager()->on('Application.buildContainer', function ($event, $container) use (&$called): void {
  223. $this->assertInstanceOf(BaseApplication::class, $event->getSubject());
  224. $this->assertInstanceOf(ContainerInterface::class, $container);
  225. $called = true;
  226. });
  227. $container = $app->getContainer();
  228. $this->assertInstanceOf(ContainerInterface::class, $container);
  229. $this->assertTrue($called, 'Listener should be called');
  230. }
  231. public function testBuildContainerEventReplaceContainer(): void
  232. {
  233. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  234. $app->getEventManager()->on('Application.buildContainer', function (EventInterface $event) {
  235. $new = new Container();
  236. $new->add('testing', 'yes');
  237. $event->setResult($new);
  238. });
  239. $container = $app->getContainer();
  240. $this->assertInstanceOf(ContainerInterface::class, $container);
  241. $this->assertTrue($container->has('testing'));
  242. }
  243. }