subject = $this->getMockBuilder(Admin::class) ->setMethods(['getRequest', 'defaultData', 'prodLocker', 'returnJson']) ->getMock(); $this->applicationServiceMock = $this->createMock(ApplicationService::class); $this->extensionAccessServiceMock = $this->createMock(ExtensionAccessService::class); $this->moduleAccessServiceMock = $this->createMock(ModuleAccessService::class); $this->actionAccessServiceMock = $this->createMock(ActionAccessService::class); $this->funcAclMock = $this->createMock(FuncAcl::class); $this->applicationServiceMock ->expects($this->once()) ->method('isDebugMode') ->willReturn(true); $this->subject->setServiceLocator($this->getServiceLocatorMock([ ApplicationService::SERVICE_ID => $this->applicationServiceMock, AclProxy::SERVICE_ID => $this->funcAclMock ])); // AccessServices are not registered in the service manager, need to mock them in the global Service class $instancesRef = new ReflectionProperty(tao_models_classes_Service::class, 'instances'); $instancesRef->setAccessible(true); $instancesRef->setValue(null, [ ExtensionAccessService::class => $this->extensionAccessServiceMock, ModuleAccessService::class => $this->moduleAccessServiceMock, ActionAccessService::class => $this->actionAccessServiceMock ]); } protected function tearDown(): void { $_SERVER['HTTP_X_REQUESTED_WITH'] = null; } public function testRemoveExtensionAccess(): void { $this->extensionAccessServiceMock ->expects($this->once()) ->method('remove') ->with('foo', 'bar'); $this->subject ->method('getRequest') ->willReturn($this->getRequestMock('foo', 'bar')); $this->subject ->expects($this->once()) ->method('returnJson') ->with([ 'uri' => 'bar', ]); $this->subject->removeExtensionAccess(); } public function testAddExtensionAccess(): void { $this->extensionAccessServiceMock ->expects($this->once()) ->method('add') ->with('foo', 'bar'); $this->subject ->method('getRequest') ->willReturn($this->getRequestMock('foo', 'bar')); $this->subject ->expects($this->once()) ->method('returnJson') ->with([ 'uri' => 'bar', ]); $this->subject->addExtensionAccess(); } public function testRemoveModuleAccess(): void { $this->moduleAccessServiceMock ->expects($this->once()) ->method('remove') ->with('foo', 'bar'); $this->subject ->method('getRequest') ->willReturn($this->getRequestMock('foo', 'bar')); $this->subject ->expects($this->once()) ->method('returnJson') ->with([ 'uri' => 'bar', ]); $this->subject->removeModuleAccess(); } public function testAddModuleAccess(): void { $this->moduleAccessServiceMock ->expects($this->once()) ->method('add') ->with('foo', 'bar'); $this->subject ->method('getRequest') ->willReturn($this->getRequestMock('foo', 'bar')); $this->subject ->expects($this->once()) ->method('returnJson') ->with([ 'uri' => 'bar', ]); $this->subject->addModuleAccess(); } public function testRemoveActionAccess(): void { $this->actionAccessServiceMock ->expects($this->once()) ->method('remove') ->with('foo', 'bar'); $this->subject ->method('getRequest') ->willReturn($this->getRequestMock('foo', 'bar')); $this->subject ->expects($this->once()) ->method('returnJson') ->with([ 'uri' => 'bar', ]); $this->subject->removeActionAccess(); } public function testAddActionAccess(): void { $this->actionAccessServiceMock ->expects($this->once()) ->method('add') ->with('foo', 'bar'); $this->subject ->method('getRequest') ->willReturn($this->getRequestMock('foo', 'bar')); $this->subject ->expects($this->once()) ->method('returnJson') ->with([ 'uri' => 'bar', ]); $this->subject->addActionAccess(); } /** * @return Request|MockObject */ private function getRequestMock(string $role, string $uri): Request { $requestMock = $this->createMock(Request::class); $requestMock ->method('getParameter') ->withConsecutive(['role'], ['uri']) ->willReturnOnConsecutiveCalls($role, $uri); return $requestMock; } }