aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Middleware/MiddlewareTest.php
blob: c1e5c44c4db0a82534bad3fca9dd5b303d21efdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\AppFramework\Middleware;

use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Http\Request;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\IConfig;
use OCP\IRequestId;

class ChildMiddleware extends Middleware {
};


class MiddlewareTest extends \Test\TestCase {
	/**
	 * @var Middleware
	 */
	private $middleware;
	private $controller;
	private $exception;
	private $api;
	/** @var Response */
	private $response;

	protected function setUp(): void {
		parent::setUp();

		$this->middleware = new ChildMiddleware();

		$this->api = $this->getMockBuilder(DIContainer::class)
			->disableOriginalConstructor()
			->getMock();

		$this->controller = $this->getMockBuilder(Controller::class)
			->setMethods([])
			->setConstructorArgs([
				$this->api,
				new Request(
					[],
					$this->createMock(IRequestId::class),
					$this->createMock(IConfig::class)
				)
			])->getMock();
		$this->exception = new \Exception();
		$this->response = $this->getMockBuilder(Response::class)->getMock();
	}


	public function testBeforeController(): void {
		$this->middleware->beforeController($this->controller, '');
		$this->assertNull(null);
	}


	public function testAfterExceptionRaiseAgainWhenUnhandled(): void {
		$this->expectException(\Exception::class);
		$this->middleware->afterException($this->controller, '', $this->exception);
	}


	public function testAfterControllerReturnResponseWhenUnhandled(): void {
		$response = $this->middleware->afterController($this->controller, '', $this->response);

		$this->assertEquals($this->response, $response);
	}


	public function testBeforeOutputReturnOutputhenUnhandled(): void {
		$output = $this->middleware->beforeOutput($this->controller, '', 'test');

		$this->assertEquals('test', $output);
	}
}