getService(null, $this->mockResource(true), []); $this->assertInstanceOf(core_kernel_classes_Resource::class, $service->map('someValue')); } public function testMapNoResourceFound() { $this->expectException(RdsResourceNotFoundException::class); $service = $this->getService('someClass', null, []); $service->map('someValue'); } public function testMapMultipleResourceFound() { $this->expectException(RdsResourceNotFoundException::class); $service = $this->getService('someClass', null, [$this->mockResource(true), $this->mockResource(true)]); $service->map('someValue'); } public function testMapMultipleResourceNotAsClass() { $this->expectException(RdsResourceNotFoundException::class); $service = $this->getService(null, $this->mockResource(false), []); $service->map('someValue'); } /** * @param $retProperty * @param $resource * @param array $searchInstances * @return RdsValidatorValueMapper */ protected function getService($retProperty, $resource, $searchInstances = []) { $service = $this->getMockBuilder(RdsValidatorValueMapper::class)->disableOriginalConstructor() ->setMethods(['getOption','getClass'])->getMockForAbstractClass(); $classMock = $this->getMockBuilder(core_kernel_classes_Class::class)->disableOriginalConstructor()->getMock(); $classMock ->method('getResource') ->willReturn($resource); $classMock ->method('searchInstances') ->willReturn($searchInstances); $service ->method('getClass') ->willReturn($classMock); $service ->method('getOption') ->willReturnCallback(function ($param) use ($retProperty) { if ($param === RdsValidatorValueMapper::OPTION_PROPERTY) { return $retProperty; } return $param; }); return $service; } /** * @param $instanceOf * @return MockObject */ protected function mockResource($instanceOf) { $mock = $this->getMockBuilder(core_kernel_classes_Resource::class) ->setMethods(['isInstanceOf']) ->disableOriginalConstructor()->getMock(); $mock->method('isInstanceOf')->willReturn($instanceOf); return $mock; } }