BaseApplicationTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Cake\Test\TestCase;
  3. use Cake\Http\BaseApplication;
  4. use Cake\Http\Response;
  5. use Cake\Http\ServerRequestFactory;
  6. use Cake\TestSuite\TestCase;
  7. use InvalidArgumentException;
  8. use TestPlugin\Plugin as TestPlugin;
  9. /**
  10. * Base application test.
  11. */
  12. class BaseApplicationTest extends TestCase
  13. {
  14. /**
  15. * Setup
  16. *
  17. * @return void
  18. */
  19. public function setUp()
  20. {
  21. parent::setUp();
  22. static::setAppNamespace();
  23. $this->path = dirname(dirname(__DIR__));
  24. }
  25. /**
  26. * Integration test for a simple controller.
  27. *
  28. * @return void
  29. */
  30. public function testInvoke()
  31. {
  32. $next = function ($req, $res) {
  33. return $res;
  34. };
  35. $response = new Response();
  36. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  37. $request = $request->withAttribute('params', [
  38. 'controller' => 'Cakes',
  39. 'action' => 'index',
  40. 'plugin' => null,
  41. 'pass' => []
  42. ]);
  43. $app = $this->getMockForAbstractClass('Cake\Http\BaseApplication', [$this->path]);
  44. $result = $app($request, $response, $next);
  45. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
  46. $this->assertEquals('Hello Jane', '' . $result->getBody());
  47. }
  48. public function testAddPluginUnknownClass()
  49. {
  50. $this->expectException(InvalidArgumentException::class);
  51. $this->expectExceptionMessage('cannot be found');
  52. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  53. $app->addPlugin('SomethingBad');
  54. }
  55. public function testAddPluginBadClass()
  56. {
  57. $this->expectException(InvalidArgumentException::class);
  58. $this->expectExceptionMessage('does not implement');
  59. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  60. $app->addPlugin(__CLASS__);
  61. }
  62. public function testAddPluginValid()
  63. {
  64. $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
  65. $app->addPlugin(TestPlugin::class);
  66. $this->assertCount(1, $app->getPlugins());
  67. $this->assertTrue($app->getPlugins()->has('TestPlugin'));
  68. }
  69. }