1
0

MiddlewareTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Http\Response;
  25. use OCP\AppFramework\Middleware;
  26. use OCP\IConfig;
  27. class ChildMiddleware extends Middleware {
  28. };
  29. class MiddlewareTest extends \Test\TestCase {
  30. /**
  31. * @var Middleware
  32. */
  33. private $middleware;
  34. private $controller;
  35. private $exception;
  36. private $api;
  37. /** @var Response */
  38. private $response;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->middleware = new ChildMiddleware();
  42. $this->api = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer')
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->controller = $this->getMockBuilder('OCP\AppFramework\Controller')
  46. ->setMethods([])
  47. ->setConstructorArgs([
  48. $this->api,
  49. new Request(
  50. [],
  51. $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
  52. $this->getMockBuilder(IConfig::class)->getMock()
  53. )
  54. ])->getMock();
  55. $this->exception = new \Exception();
  56. $this->response = $this->getMockBuilder('OCP\AppFramework\Http\Response')->getMock();
  57. }
  58. public function testBeforeController() {
  59. $this->middleware->beforeController($this->controller, null);
  60. $this->assertNull(null);
  61. }
  62. public function testAfterExceptionRaiseAgainWhenUnhandled() {
  63. $this->expectException(\Exception::class);
  64. $this->middleware->afterException($this->controller, null, $this->exception);
  65. }
  66. public function testAfterControllerReturnResponseWhenUnhandled() {
  67. $response = $this->middleware->afterController($this->controller, null, $this->response);
  68. $this->assertEquals($this->response, $response);
  69. }
  70. public function testBeforeOutputReturnOutputhenUnhandled() {
  71. $output = $this->middleware->beforeOutput($this->controller, null, 'test');
  72. $this->assertEquals('test', $output);
  73. }
  74. }