2013-08-17 11:16:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2024-05-10 15:09:14 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-08-17 11:16:48 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-18 18:40:34 +02:00
|
|
|
namespace Test\AppFramework\Middleware;
|
2013-08-17 11:16:48 +02:00
|
|
|
|
2022-02-23 10:40:58 +01:00
|
|
|
use OC\AppFramework\DependencyInjection\DIContainer;
|
2013-08-17 11:16:48 +02:00
|
|
|
use OC\AppFramework\Http\Request;
|
2022-02-23 10:40:58 +01:00
|
|
|
use OCP\AppFramework\Controller;
|
2015-02-10 13:02:48 +01:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\AppFramework\Middleware;
|
2017-10-24 15:26:53 +02:00
|
|
|
use OCP\IConfig;
|
2022-02-23 10:40:58 +01:00
|
|
|
use OCP\IRequestId;
|
2013-08-17 11:16:48 +02:00
|
|
|
|
|
|
|
class ChildMiddleware extends Middleware {
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-11-10 23:30:38 +01:00
|
|
|
class MiddlewareTest extends \Test\TestCase {
|
2013-08-21 00:41:20 +02:00
|
|
|
/**
|
|
|
|
* @var Middleware
|
|
|
|
*/
|
2013-08-17 11:16:48 +02:00
|
|
|
private $middleware;
|
|
|
|
private $controller;
|
|
|
|
private $exception;
|
|
|
|
private $api;
|
2015-02-10 13:02:48 +01:00
|
|
|
/** @var Response */
|
|
|
|
private $response;
|
2013-08-17 11:16:48 +02:00
|
|
|
|
2019-11-21 16:40:38 +01:00
|
|
|
protected function setUp(): void {
|
2014-11-10 23:30:38 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
2013-08-17 11:16:48 +02:00
|
|
|
$this->middleware = new ChildMiddleware();
|
|
|
|
|
2022-02-23 10:40:58 +01:00
|
|
|
$this->api = $this->getMockBuilder(DIContainer::class)
|
2014-05-28 02:12:01 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-08-17 11:16:48 +02:00
|
|
|
|
2022-02-23 10:40:58 +01:00
|
|
|
$this->controller = $this->getMockBuilder(Controller::class)
|
2016-07-10 14:17:26 +02:00
|
|
|
->setMethods([])
|
|
|
|
->setConstructorArgs([
|
2015-02-09 11:41:48 +01:00
|
|
|
$this->api,
|
2015-02-10 13:02:48 +01:00
|
|
|
new Request(
|
|
|
|
[],
|
2022-02-23 10:40:58 +01:00
|
|
|
$this->createMock(IRequestId::class),
|
|
|
|
$this->createMock(IConfig::class)
|
2015-02-10 13:02:48 +01:00
|
|
|
)
|
2016-07-10 14:17:26 +02:00
|
|
|
])->getMock();
|
2013-08-17 11:16:48 +02:00
|
|
|
$this->exception = new \Exception();
|
2022-02-23 10:40:58 +01:00
|
|
|
$this->response = $this->getMockBuilder(Response::class)->getMock();
|
2013-08-17 11:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-25 09:56:44 +01:00
|
|
|
public function testBeforeController(): void {
|
|
|
|
$this->middleware->beforeController($this->controller, '');
|
2013-08-21 00:41:20 +02:00
|
|
|
$this->assertNull(null);
|
2013-08-17 11:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-25 09:56:44 +01:00
|
|
|
public function testAfterExceptionRaiseAgainWhenUnhandled(): void {
|
2018-01-24 18:10:16 +01:00
|
|
|
$this->expectException(\Exception::class);
|
2023-01-25 09:56:44 +01:00
|
|
|
$this->middleware->afterException($this->controller, '', $this->exception);
|
2013-08-17 11:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-25 09:56:44 +01:00
|
|
|
public function testAfterControllerReturnResponseWhenUnhandled(): void {
|
|
|
|
$response = $this->middleware->afterController($this->controller, '', $this->response);
|
2013-08-17 11:16:48 +02:00
|
|
|
|
|
|
|
$this->assertEquals($this->response, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-25 09:56:44 +01:00
|
|
|
public function testBeforeOutputReturnOutputhenUnhandled(): void {
|
|
|
|
$output = $this->middleware->beforeOutput($this->controller, '', 'test');
|
2013-08-17 11:16:48 +02:00
|
|
|
|
|
|
|
$this->assertEquals('test', $output);
|
|
|
|
}
|
|
|
|
}
|