BaseApplicationTest.php 7.4 KB

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