2013-08-17 05:16:48 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-10 09:09:14 -04: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 05:16:48 -04:00
|
|
|
*/
|
|
|
|
|
|
2016-05-18 12:40:34 -04:00
|
|
|
namespace Test\AppFramework\Middleware;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2023-11-23 04:22:34 -05:00
|
|
|
use OC\AppFramework\DependencyInjection\DIContainer;
|
2013-08-17 05:16:48 -04:00
|
|
|
use OC\AppFramework\Http\Request;
|
2022-02-23 04:40:58 -05:00
|
|
|
use OCP\AppFramework\Controller;
|
2015-02-10 07:02:48 -05:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Middleware;
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCP\IConfig;
|
2022-02-23 04:40:58 -05:00
|
|
|
use OCP\IRequestId;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2020-04-10 08:19:56 -04:00
|
|
|
class ChildMiddleware extends Middleware {
|
|
|
|
|
};
|
2013-08-17 05:16:48 -04:00
|
|
|
|
|
|
|
|
|
2014-11-10 17:30:38 -05:00
|
|
|
class MiddlewareTest extends \Test\TestCase {
|
2013-08-20 18:41:20 -04:00
|
|
|
/**
|
|
|
|
|
* @var Middleware
|
|
|
|
|
*/
|
2013-08-17 05:16:48 -04:00
|
|
|
private $middleware;
|
|
|
|
|
private $controller;
|
|
|
|
|
private $exception;
|
|
|
|
|
private $api;
|
2015-02-10 07:02:48 -05:00
|
|
|
/** @var Response */
|
|
|
|
|
private $response;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->middleware = new ChildMiddleware();
|
|
|
|
|
|
2025-05-12 11:18:04 -04:00
|
|
|
$this->api = $this->createMock(DIContainer::class);
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2022-02-23 04:40:58 -05:00
|
|
|
$this->controller = $this->getMockBuilder(Controller::class)
|
2016-07-10 08:17:26 -04:00
|
|
|
->setConstructorArgs([
|
2015-02-09 05:41:48 -05:00
|
|
|
$this->api,
|
2015-02-10 07:02:48 -05:00
|
|
|
new Request(
|
|
|
|
|
[],
|
2022-02-23 04:40:58 -05:00
|
|
|
$this->createMock(IRequestId::class),
|
|
|
|
|
$this->createMock(IConfig::class)
|
2015-02-10 07:02:48 -05:00
|
|
|
)
|
2016-07-10 08:17:26 -04:00
|
|
|
])->getMock();
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->exception = new \Exception();
|
2025-05-12 11:18:04 -04:00
|
|
|
$this->response = $this->createMock(Response::class);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-01-25 03:56:44 -05:00
|
|
|
public function testBeforeController(): void {
|
|
|
|
|
$this->middleware->beforeController($this->controller, '');
|
2013-08-20 18:41:20 -04:00
|
|
|
$this->assertNull(null);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-01-25 03:56:44 -05:00
|
|
|
public function testAfterExceptionRaiseAgainWhenUnhandled(): void {
|
2018-01-24 12:10:16 -05:00
|
|
|
$this->expectException(\Exception::class);
|
2023-01-25 03:56:44 -05:00
|
|
|
$this->middleware->afterException($this->controller, '', $this->exception);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-01-25 03:56:44 -05:00
|
|
|
public function testAfterControllerReturnResponseWhenUnhandled(): void {
|
|
|
|
|
$response = $this->middleware->afterController($this->controller, '', $this->response);
|
2013-08-17 05:16:48 -04:00
|
|
|
|
|
|
|
|
$this->assertEquals($this->response, $response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-01-25 03:56:44 -05:00
|
|
|
public function testBeforeOutputReturnOutputhenUnhandled(): void {
|
|
|
|
|
$output = $this->middleware->beforeOutput($this->controller, '', 'test');
|
2013-08-17 05:16:48 -04:00
|
|
|
|
|
|
|
|
$this->assertEquals('test', $output);
|
|
|
|
|
}
|
|
|
|
|
}
|