BaseApplicationTest.php 5.9 KB

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