diff --git a/module/Common/test/IpGeolocation/ChainIpLocationResolverTest.php b/module/Common/test/IpGeolocation/ChainIpLocationResolverTest.php new file mode 100644 index 00000000..b4933f21 --- /dev/null +++ b/module/Common/test/IpGeolocation/ChainIpLocationResolverTest.php @@ -0,0 +1,86 @@ +firstInnerResolver = $this->prophesize(IpLocationResolverInterface::class); + $this->secondInnerResolver = $this->prophesize(IpLocationResolverInterface::class); + + $this->resolver = new ChainIpLocationResolver( + $this->firstInnerResolver->reveal(), + $this->secondInnerResolver->reveal() + ); + } + + /** + * @test + */ + public function throwsExceptionWhenNoInnerResolverCanHandleTheResolution() + { + $ipAddress = '1.2.3.4'; + + $firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); + $secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); + + $this->expectException(WrongIpException::class); + $firstResolve->shouldBeCalledOnce(); + $secondResolve->shouldBeCalledOnce(); + + $this->resolver->resolveIpLocation($ipAddress); + } + + /** + * @test + */ + public function returnsResultOfFirstInnerResolver() + { + $ipAddress = '1.2.3.4'; + + $firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willReturn([]); + $secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); + + $this->resolver->resolveIpLocation($ipAddress); + + $firstResolve->shouldHaveBeenCalledOnce(); + $secondResolve->shouldNotHaveBeenCalled(); + } + + /** + * @test + */ + public function returnsResultOfSecondInnerResolver() + { + $ipAddress = '1.2.3.4'; + + $firstResolve = $this->firstInnerResolver->resolveIpLocation($ipAddress)->willThrow(WrongIpException::class); + $secondResolve = $this->secondInnerResolver->resolveIpLocation($ipAddress)->willReturn([]); + + $this->resolver->resolveIpLocation($ipAddress); + + $firstResolve->shouldHaveBeenCalledOnce(); + $secondResolve->shouldHaveBeenCalledOnce(); + } +}