MiddlewareTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Middleware;
  23. use OC\AppFramework\Http\Request;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Http\Response;
  26. use OCP\AppFramework\Middleware;
  27. use OCP\IConfig;
  28. use OCP\IRequestId;
  29. use OC\AppFramework\DependencyInjection\DIContainer;
  30. class ChildMiddleware extends Middleware {
  31. };
  32. class MiddlewareTest extends \Test\TestCase {
  33. /**
  34. * @var Middleware
  35. */
  36. private $middleware;
  37. private $controller;
  38. private $exception;
  39. private $api;
  40. /** @var Response */
  41. private $response;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->middleware = new ChildMiddleware();
  45. $this->api = $this->getMockBuilder(DIContainer::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->controller = $this->getMockBuilder(Controller::class)
  49. ->setMethods([])
  50. ->setConstructorArgs([
  51. $this->api,
  52. new Request(
  53. [],
  54. $this->createMock(IRequestId::class),
  55. $this->createMock(IConfig::class)
  56. )
  57. ])->getMock();
  58. $this->exception = new \Exception();
  59. $this->response = $this->getMockBuilder(Response::class)->getMock();
  60. }
  61. public function testBeforeController() {
  62. $this->middleware->beforeController($this->controller, null);
  63. $this->assertNull(null);
  64. }
  65. public function testAfterExceptionRaiseAgainWhenUnhandled() {
  66. $this->expectException(\Exception::class);
  67. $this->middleware->afterException($this->controller, null, $this->exception);
  68. }
  69. public function testAfterControllerReturnResponseWhenUnhandled() {
  70. $response = $this->middleware->afterController($this->controller, null, $this->response);
  71. $this->assertEquals($this->response, $response);
  72. }
  73. public function testBeforeOutputReturnOutputhenUnhandled() {
  74. $output = $this->middleware->beforeOutput($this->controller, null, 'test');
  75. $this->assertEquals('test', $output);
  76. }
  77. }