BaseApplicationTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Cake\Test\TestCase;
  3. use Cake\Core\Configure;
  4. use Cake\Core\Plugin;
  5. use Cake\Http\BaseApplication;
  6. use Cake\Http\MiddlewareQueue;
  7. use Cake\Http\Response;
  8. use Cake\Http\ServerRequestFactory;
  9. use Cake\Routing\RouteBuilder;
  10. use Cake\Routing\RouteCollection;
  11. use Cake\TestSuite\TestCase;
  12. use InvalidArgumentException;
  13. use TestPlugin\Plugin as TestPlugin;
  14. /**
  15. * Base application test.
  16. */
  17. class BaseApplicationTest extends TestCase
  18. {
  19. /**
  20. * Setup
  21. *
  22. * @return void
  23. */
  24. public function setUp()
  25. {
  26. parent::setUp();
  27. static::setAppNamespace();
  28. $this->path = dirname(dirname(__DIR__));
  29. }
  30. public function tearDown()
  31. {
  32. parent::tearDown();
  33. Plugin::unload();
  34. }
  35. /**
  36. * Integration test for a simple controller.
  37. *
  38. * @return void
  39. */
  40. public function testInvoke()
  41. {
  42. $next = function ($req, $res) {
  43. return $res;
  44. };
  45. $response = new Response();
  46. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  47. $request = $request->withAttribute('params', [
  48. 'controller' => 'Cakes',
  49. 'action' => 'index',
  50. 'plugin' => null,
  51. 'pass' => []
  52. ]);
  53. $app = $this->getMockForAbstractClass('Cake\Http\BaseApplication', [$this->path]);
  54. $result = $app($request, $response, $next);
  55. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
  56. $this->assertEquals('Hello Jane', '' . $result->getBody());
  57. }
  58. public function testAddPluginUnknownClass()
  59. {
  60. $this->expectException(InvalidArgumentException::class);
  61. $this->expectExceptionMessage('cannot be found');
  62. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  63. $app->addPlugin('SomethingBad');
  64. }
  65. public function testAddPluginBadClass()
  66. {
  67. $this->expectException(InvalidArgumentException::class);
  68. $this->expectExceptionMessage('does not implement');
  69. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  70. $app->addPlugin(__CLASS__);
  71. }
  72. public function testAddPluginValidShortName()
  73. {
  74. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  75. $app->addPlugin('TestPlugin');
  76. $this->assertCount(1, $app->getPlugins());
  77. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  78. $app->addPlugin('Company/TestPluginThree');
  79. $this->assertCount(2, $app->getPlugins());
  80. $this->assertTrue($app->getPlugins()->has('Company/TestPluginThree'));
  81. }
  82. public function testAddPluginValid()
  83. {
  84. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  85. $app->addPlugin(TestPlugin::class);
  86. $this->assertCount(1, $app->getPlugins());
  87. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  88. }
  89. public function testPluginEvents()
  90. {
  91. $app = $this->getMockForAbstractClass(
  92. BaseApplication::class,
  93. [$this->path]
  94. );
  95. $start = $app->getEventManager();
  96. $this->assertCount(0, $start->listeners('TestPlugin.load'));
  97. $app->addPlugin(TestPlugin::class);
  98. $this->assertNull($app->pluginEvents());
  99. $after = $app->getEventManager();
  100. $this->assertSame($after, $start);
  101. $this->assertCount(1, $after->listeners('TestPlugin.load'));
  102. }
  103. public function testPluginMiddleware()
  104. {
  105. $start = new MiddlewareQueue();
  106. $app = $this->getMockForAbstractClass(
  107. BaseApplication::class,
  108. [$this->path]
  109. );
  110. $app->addPlugin(TestPlugin::class);
  111. $after = $app->pluginMiddleware($start);
  112. $this->assertSame($start, $after);
  113. $this->assertCount(1, $after);
  114. }
  115. public function testPluginRoutes()
  116. {
  117. $collection = new RouteCollection();
  118. $routes = new RouteBuilder($collection, '/');
  119. $app = $this->getMockForAbstractClass(
  120. BaseApplication::class,
  121. [$this->path]
  122. );
  123. $app->addPlugin(TestPlugin::class);
  124. $result = $app->pluginRoutes($routes);
  125. $this->assertSame($routes, $result);
  126. $url = [
  127. 'plugin' => 'TestPlugin',
  128. 'controller' => 'TestPlugin',
  129. 'action' => 'index',
  130. '_method' => 'GET'
  131. ];
  132. $this->assertNotEmpty($collection->match($url, []));
  133. }
  134. public function testPluginBootstrap()
  135. {
  136. $app = $this->getMockForAbstractClass(
  137. BaseApplication::class,
  138. [$this->path]
  139. );
  140. $app->addPlugin(TestPlugin::class);
  141. $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
  142. $this->assertNull($app->pluginBootstrap());
  143. $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
  144. }
  145. }