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