container = $this->getMockBuilder(DIContainer::class) ->setMethods(['isAdminUser']) ->setConstructorArgs(['name']) ->getMock(); } public function testProvidesRequest() { $this->assertTrue(isset($this->container['Request'])); } public function testProvidesMiddlewareDispatcher() { $this->assertTrue(isset($this->container['MiddlewareDispatcher'])); } public function testProvidesAppName() { $this->assertTrue(isset($this->container['AppName'])); } public function testAppNameIsSetCorrectly() { $this->assertEquals('name', $this->container['AppName']); } public function testMiddlewareDispatcherIncludesSecurityMiddleware() { $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() { $this->expectException(QueryException::class); $this->container->query('\OCA\Name\Foo'); } }