aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php
blob: f3d2cff1ffd1110fa6ede3e46e8cd9ca508bbc82 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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\DependencyInjection;

use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\MiddlewareRegistration;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IRequestId;

/**
 * @group DB
 */
class DIContainerTest extends \Test\TestCase {
	/** @var DIContainer|\PHPUnit\Framework\MockObject\MockObject */
	private $container;

	protected function setUp(): void {
		parent::setUp();
		$this->container = $this->getMockBuilder(DIContainer::class)
			->setMethods(['isAdminUser'])
			->setConstructorArgs(['name'])
			->getMock();
	}


	public function testProvidesRequest(): void {
		$this->assertTrue(isset($this->container['Request']));
	}

	public function testProvidesMiddlewareDispatcher(): void {
		$this->assertTrue(isset($this->container['MiddlewareDispatcher']));
	}

	public function testProvidesAppName(): void {
		$this->assertTrue(isset($this->container['AppName']));
	}


	public function testAppNameIsSetCorrectly(): void {
		$this->assertEquals('name', $this->container['AppName']);
	}

	public function testMiddlewareDispatcherIncludesSecurityMiddleware(): void {
		$this->container['Request'] = new Request(
			['method' => 'GET'],
			$this->createMock(IRequestId::class),
			$this->createMock(IConfig::class)
		);
		$dispatcher = $this->container['MiddlewareDispatcher'];
		$middlewares = $dispatcher->getMiddlewares();

		$found = false;
		foreach ($middlewares as $middleware) {
			if ($middleware instanceof SecurityMiddleware) {
				$found = true;
			}
		}

		$this->assertTrue($found);
	}

	public function testMiddlewareDispatcherIncludesBootstrapMiddlewares(): void {
		$coordinator = $this->createMock(Coordinator::class);
		$this->container[Coordinator::class] = $coordinator;
		$this->container['Request'] = $this->createMock(Request::class);
		$registrationContext = $this->createMock(RegistrationContext::class);
		$registrationContext->method('getMiddlewareRegistrations')
			->willReturn([
				new MiddlewareRegistration($this->container['appName'], 'foo', false),
				new MiddlewareRegistration('otherapp', 'bar', false),
			]);
		$this->container['foo'] = new class extends Middleware {
		};
		$this->container['bar'] = new class extends Middleware {
		};
		$coordinator->method('getRegistrationContext')->willReturn($registrationContext);

		$dispatcher = $this->container['MiddlewareDispatcher'];

		$middlewares = $dispatcher->getMiddlewares();
		self::assertNotEmpty($middlewares);
		foreach ($middlewares as $middleware) {
			if ($middleware === $this->container['bar']) {
				$this->fail('Container must not register this middleware');
			}
			if ($middleware === $this->container['foo']) {
				// It is done
				return;
			}
		}
		$this->fail('Bootstrap registered middleware not found');
	}

	public function testMiddlewareDispatcherIncludesGlobalBootstrapMiddlewares(): void {
		$coordinator = $this->createMock(Coordinator::class);
		$this->container[Coordinator::class] = $coordinator;
		$this->container['Request'] = $this->createMock(Request::class);
		$registrationContext = $this->createMock(RegistrationContext::class);
		$registrationContext->method('getMiddlewareRegistrations')
			->willReturn([
				new MiddlewareRegistration('otherapp', 'foo', true),
				new MiddlewareRegistration('otherapp', 'bar', false),
			]);
		$this->container['foo'] = new class extends Middleware {
		};
		$this->container['bar'] = new class extends Middleware {
		};
		$coordinator->method('getRegistrationContext')->willReturn($registrationContext);

		$dispatcher = $this->container['MiddlewareDispatcher'];

		$middlewares = $dispatcher->getMiddlewares();
		self::assertNotEmpty($middlewares);
		foreach ($middlewares as $middleware) {
			if ($middleware === $this->container['bar']) {
				$this->fail('Container must not register this middleware');
			}
			if ($middleware === $this->container['foo']) {
				// It is done
				return;
			}
		}
		$this->fail('Bootstrap registered middleware not found');
	}

	public function testInvalidAppClass(): void {
		$this->expectException(QueryException::class);
		$this->container->query('\OCA\Name\Foo');
	}
}