BaseApplicationTest.php 7.0 KB

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