MiddlewareTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Middleware;
  8. use OC\AppFramework\DependencyInjection\DIContainer;
  9. use OC\AppFramework\Http\Request;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\Response;
  12. use OCP\AppFramework\Middleware;
  13. use OCP\IConfig;
  14. use OCP\IRequestId;
  15. class ChildMiddleware extends Middleware {
  16. };
  17. class MiddlewareTest extends \Test\TestCase {
  18. /**
  19. * @var Middleware
  20. */
  21. private $middleware;
  22. private $controller;
  23. private $exception;
  24. private $api;
  25. /** @var Response */
  26. private $response;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->middleware = new ChildMiddleware();
  30. $this->api = $this->getMockBuilder(DIContainer::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->controller = $this->getMockBuilder(Controller::class)
  34. ->setMethods([])
  35. ->setConstructorArgs([
  36. $this->api,
  37. new Request(
  38. [],
  39. $this->createMock(IRequestId::class),
  40. $this->createMock(IConfig::class)
  41. )
  42. ])->getMock();
  43. $this->exception = new \Exception();
  44. $this->response = $this->getMockBuilder(Response::class)->getMock();
  45. }
  46. public function testBeforeController(): void {
  47. $this->middleware->beforeController($this->controller, '');
  48. $this->assertNull(null);
  49. }
  50. public function testAfterExceptionRaiseAgainWhenUnhandled(): void {
  51. $this->expectException(\Exception::class);
  52. $this->middleware->afterException($this->controller, '', $this->exception);
  53. }
  54. public function testAfterControllerReturnResponseWhenUnhandled(): void {
  55. $response = $this->middleware->afterController($this->controller, '', $this->response);
  56. $this->assertEquals($this->response, $response);
  57. }
  58. public function testBeforeOutputReturnOutputhenUnhandled(): void {
  59. $output = $this->middleware->beforeOutput($this->controller, '', 'test');
  60. $this->assertEquals('test', $output);
  61. }
  62. }