reflector = $this->getMockBuilder(ControllerMethodReflector::class) ->disableOriginalConstructor()->getMock(); $this->userSession = $this->createMock(IUserSession::class); $this->subAdminManager = $this->createMock(ISubAdmin::class); $this->l10n = $this->createMock(IL10N::class); $this->subadminMiddleware = new SubadminMiddleware( $this->reflector, $this->userSession, $this->subAdminManager, $this->l10n, ); $this->controller = $this->getMockBuilder(Controller::class) ->disableOriginalConstructor()->getMock(); $this->userSession ->expects(self::any()) ->method('getUser') ->willReturn($this->createMock(IUser::class)); } public function testBeforeControllerAsUserWithoutAnnotation(): void { $this->expectException(NotAdminException::class); $this->reflector ->expects($this->exactly(2)) ->method('hasAnnotation') ->withConsecutive( ['NoSubAdminRequired'], ['AuthorizedAdminSetting'], )->willReturn(false); $this->subAdminManager ->expects(self::once()) ->method('isSubAdmin') ->willReturn(false); $this->subadminMiddleware->beforeController($this->controller, 'foo'); } public function testBeforeControllerWithAnnotation(): void { $this->reflector ->expects($this->once()) ->method('hasAnnotation') ->with('NoSubAdminRequired') ->willReturn(true); $this->subAdminManager ->expects(self::never()) ->method('isSubAdmin'); $this->subadminMiddleware->beforeController($this->controller, 'foo'); } public function testBeforeControllerAsSubAdminWithoutAnnotation(): void { $this->reflector ->expects($this->exactly(2)) ->method('hasAnnotation') ->withConsecutive( ['NoSubAdminRequired'], ['AuthorizedAdminSetting'], )->willReturn(false); $this->subAdminManager ->expects(self::once()) ->method('isSubAdmin') ->willReturn(true); $this->subadminMiddleware->beforeController($this->controller, 'foo'); } public function testAfterNotAdminException(): void { $expectedResponse = new TemplateResponse('core', '403', [], 'guest'); $expectedResponse->setStatus(403); $this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException(''))); } public function testAfterRegularException(): void { $this->expectException(\Exception::class); $expectedResponse = new TemplateResponse('core', '403', [], 'guest'); $expectedResponse->setStatus(403); $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception()); } }