BaseApplicationTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Http\BaseApplication;
  19. use Cake\Http\MiddlewareQueue;
  20. use Cake\Http\Response;
  21. use Cake\Http\ServerRequestFactory;
  22. use Cake\Routing\RouteBuilder;
  23. use Cake\Routing\RouteCollection;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use InvalidArgumentException;
  27. use TestPlugin\Plugin as TestPlugin;
  28. /**
  29. * Base application test.
  30. */
  31. class BaseApplicationTest extends TestCase
  32. {
  33. /**
  34. * Setup
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. static::setAppNamespace();
  42. $this->path = dirname(dirname(__DIR__));
  43. }
  44. public function tearDown()
  45. {
  46. parent::tearDown();
  47. Plugin::unload();
  48. }
  49. /**
  50. * Integration test for a simple controller.
  51. *
  52. * @return void
  53. */
  54. public function testInvoke()
  55. {
  56. $next = function ($req, $res) {
  57. return $res;
  58. };
  59. $response = new Response();
  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('Cake\Http\BaseApplication', [$this->path]);
  68. $result = $app($request, $response, $next);
  69. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
  70. $this->assertEquals('Hello Jane', '' . $result->getBody());
  71. }
  72. public function testAddPluginUnknownClass()
  73. {
  74. $this->expectException(InvalidArgumentException::class);
  75. $this->expectExceptionMessage('cannot be found');
  76. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  77. $app->addPlugin('SomethingBad');
  78. }
  79. public function testAddPluginBadClass()
  80. {
  81. $this->expectException(InvalidArgumentException::class);
  82. $this->expectExceptionMessage('does not implement');
  83. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  84. $app->addPlugin(__CLASS__);
  85. }
  86. public function testAddPluginValidShortName()
  87. {
  88. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  89. $app->addPlugin('TestPlugin');
  90. $this->assertCount(1, $app->getPlugins());
  91. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  92. $app->addPlugin('Company/TestPluginThree');
  93. $this->assertCount(2, $app->getPlugins());
  94. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  95. }
  96. public function testAddPluginValid()
  97. {
  98. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  99. $app->addPlugin(TestPlugin::class);
  100. $this->assertCount(1, $app->getPlugins());
  101. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  102. }
  103. public function testPluginMiddleware()
  104. {
  105. $start = new MiddlewareQueue();
  106. $app = $this->getMockForAbstractClass(
  107. BaseApplication::class,
  108. [$this->path]
  109. );
  110. $app->addPlugin(TestPlugin::class);
  111. $after = $app->pluginMiddleware($start);
  112. $this->assertSame($start, $after);
  113. $this->assertCount(1, $after);
  114. }
  115. public function testPluginRoutes()
  116. {
  117. $collection = new RouteCollection();
  118. $routes = new RouteBuilder($collection, '/');
  119. $app = $this->getMockForAbstractClass(
  120. BaseApplication::class,
  121. [$this->path]
  122. );
  123. $app->addPlugin(TestPlugin::class);
  124. $result = $app->pluginRoutes($routes);
  125. $this->assertSame($routes, $result);
  126. $url = [
  127. 'plugin' => 'TestPlugin',
  128. 'controller' => 'TestPlugin',
  129. 'action' => 'index',
  130. '_method' => 'GET'
  131. ];
  132. $this->assertNotEmpty($collection->match($url, []));
  133. }
  134. public function testPluginBootstrap()
  135. {
  136. $app = $this->getMockForAbstractClass(
  137. BaseApplication::class,
  138. [$this->path]
  139. );
  140. $app->addPlugin(TestPlugin::class);
  141. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  142. $this->assertNull($app->pluginBootstrap());
  143. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  144. }
  145. /**
  146. * Ensure that plugins loaded via Plugin::load()
  147. * don't have their bootstrapping run twice.
  148. *
  149. * @return void
  150. */
  151. public function testPluginBootstrapInteractWithPluginLoad()
  152. {
  153. Plugin::load('TestPlugin', ['bootstrap' => true]);
  154. $app = $this->getMockForAbstractClass(
  155. BaseApplication::class,
  156. [$this->path]
  157. );
  158. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  159. Configure::delete('PluginTest.test_plugin.bootstrap');
  160. $this->assertNull($app->pluginBootstrap());
  161. $this->assertFalse(
  162. Configure::check('PluginTest.test_plugin.bootstrap'),
  163. 'Key should not be set, as plugin has already had bootstrap run'
  164. );
  165. }
  166. /**
  167. * Test that plugins loaded with addPlugin() can load additional
  168. * plugins.
  169. *
  170. * @return void
  171. */
  172. public function testPluginBootstrapRecursivePlugins()
  173. {
  174. $app = $this->getMockForAbstractClass(
  175. BaseApplication::class,
  176. [$this->path]
  177. );
  178. $app->addPlugin('ParentPlugin');
  179. $app->pluginBootstrap();
  180. $this->assertTrue(
  181. Configure::check('ParentPlugin.bootstrap'),
  182. 'Plugin bootstrap should be run'
  183. );
  184. $this->assertTrue(
  185. Configure::check('PluginTest.test_plugin.bootstrap'),
  186. 'Nested plugin should have bootstrap run'
  187. );
  188. $this->assertTrue(
  189. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  190. 'Nested plugin should have bootstrap run'
  191. );
  192. }
  193. /**
  194. * Ensure that Router::$initialized is toggled even if the routes
  195. * file fails. This prevents the routes file from being re-parsed
  196. * during the error handling process.
  197. *
  198. * @return void
  199. */
  200. public function testRouteHookInitializesRouterOnError()
  201. {
  202. $app = $this->getMockForAbstractClass(
  203. 'Cake\Http\BaseApplication',
  204. [TEST_APP . 'invalid_routes' . DS]
  205. );
  206. $builder = Router::createRouteBuilder('/');
  207. try {
  208. $app->routes($builder);
  209. $this->fail('invalid_routes/routes.php file should raise an error.');
  210. } catch (\InvalidArgumentException $e) {
  211. $this->assertTrue(Router::$initialized, 'Should be toggled to prevent duplicate route errors');
  212. $this->assertContains('route class', $e->getMessage());
  213. }
  214. }
  215. }