BaseApplicationTest.php 8.9 KB

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