BaseApplicationTest.php 8.7 KB

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