BaseApplicationTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 testPluginEvents()
  103. {
  104. $app = $this->getMockForAbstractClass(
  105. BaseApplication::class,
  106. [$this->path]
  107. );
  108. $start = $app->getEventManager();
  109. $this->assertCount(0, $start->listeners('TestPlugin.load'));
  110. $app->addPlugin(TestPlugin::class);
  111. $this->assertNull($app->pluginEvents());
  112. $after = $app->getEventManager();
  113. $this->assertSame($after, $start);
  114. $this->assertCount(1, $after->listeners('TestPlugin.load'));
  115. }
  116. public function testPluginMiddleware()
  117. {
  118. $start = new MiddlewareQueue();
  119. $app = $this->getMockForAbstractClass(
  120. BaseApplication::class,
  121. [$this->path]
  122. );
  123. $app->addPlugin(TestPlugin::class);
  124. $after = $app->pluginMiddleware($start);
  125. $this->assertSame($start, $after);
  126. $this->assertCount(1, $after);
  127. }
  128. public function testPluginRoutes()
  129. {
  130. $collection = new RouteCollection();
  131. $routes = new RouteBuilder($collection, '/');
  132. $app = $this->getMockForAbstractClass(
  133. BaseApplication::class,
  134. [$this->path]
  135. );
  136. $app->addPlugin(TestPlugin::class);
  137. $result = $app->pluginRoutes($routes);
  138. $this->assertSame($routes, $result);
  139. $url = [
  140. 'plugin' => 'TestPlugin',
  141. 'controller' => 'TestPlugin',
  142. 'action' => 'index',
  143. '_method' => 'GET'
  144. ];
  145. $this->assertNotEmpty($collection->match($url, []));
  146. }
  147. public function testPluginBootstrap()
  148. {
  149. $app = $this->getMockForAbstractClass(
  150. BaseApplication::class,
  151. [$this->path]
  152. );
  153. $app->addPlugin(TestPlugin::class);
  154. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  155. $this->assertNull($app->pluginBootstrap());
  156. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  157. }
  158. }