BaseApplicationTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Core\BasePlugin;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\Http\BaseApplication;
  20. use Cake\Http\MiddlewareQueue;
  21. use Cake\Http\Response;
  22. use Cake\Http\ServerRequestFactory;
  23. use Cake\Routing\RouteBuilder;
  24. use Cake\Routing\RouteCollection;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use InvalidArgumentException;
  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. * Setup
  39. *
  40. * @return void
  41. */
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. static::setAppNamespace();
  46. $this->path = dirname(dirname(__DIR__));
  47. }
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. $this->clearPlugins();
  52. }
  53. /**
  54. * Integration test for a simple controller.
  55. *
  56. * @return void
  57. */
  58. public function testInvoke()
  59. {
  60. $next = function ($req, $res) {
  61. return $res;
  62. };
  63. $response = new Response();
  64. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  65. $request = $request->withAttribute('params', [
  66. 'controller' => 'Cakes',
  67. 'action' => 'index',
  68. 'plugin' => null,
  69. 'pass' => [],
  70. ]);
  71. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  72. $result = $app($request, $response, $next);
  73. $this->assertInstanceOf(ResponseInterface::class, $result);
  74. $this->assertEquals('Hello Jane', '' . $result->getBody());
  75. }
  76. /**
  77. * Ensure that plugins with no plugin class can be loaded.
  78. * This makes adopting the new API easier
  79. */
  80. public function testAddPluginUnknownClass()
  81. {
  82. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  83. $app->addPlugin('PluginJs');
  84. $plugin = $app->getPlugins()->get('PluginJs');
  85. $this->assertInstanceOf(BasePlugin::class, $plugin);
  86. $this->assertEquals(
  87. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
  88. $plugin->getPath()
  89. );
  90. $this->assertEquals(
  91. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
  92. $plugin->getConfigPath()
  93. );
  94. $this->assertEquals(
  95. TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
  96. $plugin->getClassPath()
  97. );
  98. }
  99. /**
  100. * Ensure that plugin interfaces are implemented.
  101. */
  102. public function testAddPluginBadClass()
  103. {
  104. $this->expectException(InvalidArgumentException::class);
  105. $this->expectExceptionMessage('does not implement');
  106. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  107. $app->addPlugin(__CLASS__);
  108. }
  109. public function testAddPluginValidShortName()
  110. {
  111. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  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()
  120. {
  121. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  122. $app->addPlugin(TestPlugin::class);
  123. $this->assertCount(1, $app->getPlugins());
  124. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  125. }
  126. public function testPluginMiddleware()
  127. {
  128. $start = new MiddlewareQueue();
  129. $app = $this->getMockForAbstractClass(
  130. BaseApplication::class,
  131. [$this->path]
  132. );
  133. $app->addPlugin(TestPlugin::class);
  134. $after = $app->pluginMiddleware($start);
  135. $this->assertSame($start, $after);
  136. $this->assertCount(1, $after);
  137. }
  138. public function testPluginRoutes()
  139. {
  140. $collection = new RouteCollection();
  141. $routes = new RouteBuilder($collection, '/');
  142. $app = $this->getMockForAbstractClass(
  143. BaseApplication::class,
  144. [$this->path]
  145. );
  146. $app->addPlugin(TestPlugin::class);
  147. $result = $app->pluginRoutes($routes);
  148. $this->assertSame($routes, $result);
  149. $url = [
  150. 'plugin' => 'TestPlugin',
  151. 'controller' => 'TestPlugin',
  152. 'action' => 'index',
  153. '_method' => 'GET',
  154. ];
  155. $this->assertNotEmpty($collection->match($url, []));
  156. }
  157. public function testPluginBootstrap()
  158. {
  159. $app = $this->getMockForAbstractClass(
  160. BaseApplication::class,
  161. [$this->path]
  162. );
  163. $app->addPlugin(TestPlugin::class);
  164. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  165. $this->assertNull($app->pluginBootstrap());
  166. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  167. }
  168. /**
  169. * Ensure that plugins loaded via Plugin::load()
  170. * don't have their bootstrapping run twice.
  171. *
  172. * @return void
  173. */
  174. public function testPluginBootstrapInteractWithPluginLoad()
  175. {
  176. $this->deprecated(function () {
  177. Plugin::load('TestPlugin', ['bootstrap' => true]);
  178. $app = $this->getMockForAbstractClass(
  179. BaseApplication::class,
  180. [$this->path]
  181. );
  182. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  183. Configure::delete('PluginTest.test_plugin.bootstrap');
  184. $this->assertNull($app->pluginBootstrap());
  185. $this->assertFalse(
  186. Configure::check('PluginTest.test_plugin.bootstrap'),
  187. 'Key should not be set, as plugin has already had bootstrap run'
  188. );
  189. });
  190. }
  191. /**
  192. * Test that plugins loaded with addPlugin() can load additional
  193. * plugins.
  194. *
  195. * @return void
  196. */
  197. public function testPluginBootstrapRecursivePlugins()
  198. {
  199. $app = $this->getMockForAbstractClass(
  200. BaseApplication::class,
  201. [$this->path]
  202. );
  203. $app->addPlugin('ParentPlugin');
  204. $this->deprecated(function () use ($app) {
  205. $app->pluginBootstrap();
  206. });
  207. $this->assertTrue(
  208. Configure::check('ParentPlugin.bootstrap'),
  209. 'Plugin bootstrap should be run'
  210. );
  211. $this->assertTrue(
  212. Configure::check('PluginTest.test_plugin.bootstrap'),
  213. 'Nested plugin should have bootstrap run'
  214. );
  215. $this->assertTrue(
  216. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  217. 'Nested plugin should have bootstrap run'
  218. );
  219. }
  220. /**
  221. * Ensure that Router::$initialized is toggled even if the routes
  222. * file fails. This prevents the routes file from being re-parsed
  223. * during the error handling process.
  224. *
  225. * @return void
  226. */
  227. public function testRouteHookInitializesRouterOnError()
  228. {
  229. $app = $this->getMockForAbstractClass(
  230. 'Cake\Http\BaseApplication',
  231. [TEST_APP . 'invalid_routes' . DS]
  232. );
  233. $builder = Router::createRouteBuilder('/');
  234. try {
  235. $app->routes($builder);
  236. $this->fail('invalid_routes/routes.php file should raise an error.');
  237. } catch (\InvalidArgumentException $e) {
  238. $this->assertTrue(Router::$initialized, 'Should be toggled to prevent duplicate route errors');
  239. $this->assertContains('route class', $e->getMessage());
  240. }
  241. }
  242. /**
  243. * Tests that loading a non existing plugin through addOptionalPlugin() does not throw an exception
  244. *
  245. * @return void
  246. * @covers ::addOptionalPlugin
  247. */
  248. public function testAddOptionalPluginLoadingNonExistingPlugin()
  249. {
  250. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  251. $pluginCountBefore = count($app->getPlugins());
  252. $nonExistingPlugin = 'NonExistingPlugin';
  253. $app->addOptionalPlugin($nonExistingPlugin);
  254. $pluginCountAfter = count($app->getPlugins());
  255. $this->assertSame($pluginCountBefore, $pluginCountAfter);
  256. }
  257. /**
  258. * Tests that loading an existing plugin through addOptionalPlugin() works
  259. *
  260. * @return void
  261. * @covers ::addOptionalPlugin
  262. */
  263. public function testAddOptionalPluginLoadingNonExistingPluginValid()
  264. {
  265. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  266. $app->addOptionalPlugin(TestPlugin::class);
  267. $this->assertCount(1, $app->getPlugins());
  268. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  269. }
  270. }