BaseApplicationTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * @var string
  36. */
  37. protected $path;
  38. /**
  39. * Setup
  40. *
  41. * @return void
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. static::setAppNamespace();
  47. $this->path = dirname(dirname(__DIR__));
  48. }
  49. public function tearDown(): void
  50. {
  51. parent::tearDown();
  52. $this->clearPlugins();
  53. }
  54. /**
  55. * Integration test for a simple controller.
  56. *
  57. * @return void
  58. */
  59. public function testHandle()
  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. }
  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()
  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()
  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()
  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()
  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()
  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()
  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. * @return void
  160. */
  161. public function testPluginBootstrapRecursivePlugins()
  162. {
  163. $app = $this->getMockForAbstractClass(
  164. BaseApplication::class,
  165. [$this->path]
  166. );
  167. $app->addPlugin('ParentPlugin');
  168. $app->pluginBootstrap();
  169. $this->assertTrue(
  170. Configure::check('ParentPlugin.bootstrap'),
  171. 'Plugin bootstrap should be run'
  172. );
  173. $this->assertTrue(
  174. Configure::check('PluginTest.test_plugin.bootstrap'),
  175. 'Nested plugin should have bootstrap run'
  176. );
  177. $this->assertTrue(
  178. Configure::check('PluginTest.test_plugin_two.bootstrap'),
  179. 'Nested plugin should have bootstrap run'
  180. );
  181. }
  182. /**
  183. * Tests that loading a non existing plugin through addOptionalPlugin() does not throw an exception
  184. *
  185. * @return void
  186. * @covers ::addOptionalPlugin
  187. */
  188. public function testAddOptionalPluginLoadingNonExistingPlugin()
  189. {
  190. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  191. $pluginCountBefore = count($app->getPlugins());
  192. $nonExistingPlugin = 'NonExistingPlugin';
  193. $app->addOptionalPlugin($nonExistingPlugin);
  194. $pluginCountAfter = count($app->getPlugins());
  195. $this->assertSame($pluginCountBefore, $pluginCountAfter);
  196. }
  197. /**
  198. * Tests that loading an existing plugin through addOptionalPlugin() works
  199. *
  200. * @return void
  201. * @covers ::addOptionalPlugin
  202. */
  203. public function testAddOptionalPluginLoadingNonExistingPluginValid()
  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. }