BaseApplicationTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\TestSuite\TestCase;
  25. use InvalidArgumentException;
  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()
  38. {
  39. parent::setUp();
  40. static::setAppNamespace();
  41. $this->path = dirname(dirname(__DIR__));
  42. }
  43. public function tearDown()
  44. {
  45. parent::tearDown();
  46. Plugin::unload();
  47. }
  48. /**
  49. * Integration test for a simple controller.
  50. *
  51. * @return void
  52. */
  53. public function testInvoke()
  54. {
  55. $next = function ($req, $res) {
  56. return $res;
  57. };
  58. $response = new Response();
  59. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  60. $request = $request->withAttribute('params', [
  61. 'controller' => 'Cakes',
  62. 'action' => 'index',
  63. 'plugin' => null,
  64. 'pass' => []
  65. ]);
  66. $app = $this->getMockForAbstractClass('Cake\Http\BaseApplication', [$this->path]);
  67. $result = $app($request, $response, $next);
  68. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
  69. $this->assertEquals('Hello Jane', '' . $result->getBody());
  70. }
  71. public function testAddPluginUnknownClass()
  72. {
  73. $this->expectException(InvalidArgumentException::class);
  74. $this->expectExceptionMessage('cannot be found');
  75. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  76. $app->addPlugin('SomethingBad');
  77. }
  78. public function testAddPluginBadClass()
  79. {
  80. $this->expectException(InvalidArgumentException::class);
  81. $this->expectExceptionMessage('does not implement');
  82. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  83. $app->addPlugin(__CLASS__);
  84. }
  85. public function testAddPluginValidShortName()
  86. {
  87. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  88. $app->addPlugin('TestPlugin');
  89. $this->assertCount(1, $app->getPlugins());
  90. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  91. $app->addPlugin('Company/TestPluginThree');
  92. $this->assertCount(2, $app->getPlugins());
  93. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  94. }
  95. public function testAddPluginValid()
  96. {
  97. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  98. $app->addPlugin(TestPlugin::class);
  99. $this->assertCount(1, $app->getPlugins());
  100. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  101. }
  102. public function testPluginMiddleware()
  103. {
  104. $start = new MiddlewareQueue();
  105. $app = $this->getMockForAbstractClass(
  106. BaseApplication::class,
  107. [$this->path]
  108. );
  109. $app->addPlugin(TestPlugin::class);
  110. $after = $app->pluginMiddleware($start);
  111. $this->assertSame($start, $after);
  112. $this->assertCount(1, $after);
  113. }
  114. public function testPluginRoutes()
  115. {
  116. $collection = new RouteCollection();
  117. $routes = new RouteBuilder($collection, '/');
  118. $app = $this->getMockForAbstractClass(
  119. BaseApplication::class,
  120. [$this->path]
  121. );
  122. $app->addPlugin(TestPlugin::class);
  123. $result = $app->pluginRoutes($routes);
  124. $this->assertSame($routes, $result);
  125. $url = [
  126. 'plugin' => 'TestPlugin',
  127. 'controller' => 'TestPlugin',
  128. 'action' => 'index',
  129. '_method' => 'GET'
  130. ];
  131. $this->assertNotEmpty($collection->match($url, []));
  132. }
  133. public function testPluginBootstrap()
  134. {
  135. $app = $this->getMockForAbstractClass(
  136. BaseApplication::class,
  137. [$this->path]
  138. );
  139. $app->addPlugin(TestPlugin::class);
  140. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  141. $this->assertNull($app->pluginBootstrap());
  142. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  143. }
  144. /**
  145. * Ensure that plugins loaded via Plugin::load()
  146. * don't have their bootstrapping run twice.
  147. *
  148. * @return void
  149. */
  150. public function testPluginBootstrapInteractWithPluginLoad()
  151. {
  152. Plugin::load('TestPlugin', ['bootstrap' => true]);
  153. $app = $this->getMockForAbstractClass(
  154. BaseApplication::class,
  155. [$this->path]
  156. );
  157. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  158. Configure::delete('PluginTest.test_plugin.bootstrap');
  159. $this->assertNull($app->pluginBootstrap());
  160. $this->assertFalse(
  161. Configure::check('PluginTest.test_plugin.bootstrap'),
  162. 'Key should not be set, as plugin has already had bootstrap run'
  163. );
  164. }
  165. }