From 110e8cb78d067c50a9ce547eea8ab14fe287773b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 4 Aug 2018 15:50:02 +0200 Subject: [PATCH] Added test to cover new IP resolution API limits --- .../Visit/ProcessVisitsCommandTest.php | 37 +++++++++++++++++++ .../Service/IpApiLocationResolverTest.php | 16 ++++++++ 2 files changed, 53 insertions(+) diff --git a/module/CLI/test/Command/Visit/ProcessVisitsCommandTest.php b/module/CLI/test/Command/Visit/ProcessVisitsCommandTest.php index ae36de22..5f843fa4 100644 --- a/module/CLI/test/Command/Visit/ProcessVisitsCommandTest.php +++ b/module/CLI/test/Command/Visit/ProcessVisitsCommandTest.php @@ -97,4 +97,41 @@ class ProcessVisitsCommandTest extends TestCase $output = $this->commandTester->getDisplay(); $this->assertTrue(strpos($output, 'Ignored localhost address') > 0); } + + /** + * @test + */ + public function sleepsEveryTimeTheApiLimitIsReached() + { + $visits = [ + (new Visit())->setRemoteAddr('1.2.3.4'), + (new Visit())->setRemoteAddr('4.3.2.1'), + (new Visit())->setRemoteAddr('12.34.56.78'), + (new Visit())->setRemoteAddr('1.2.3.4'), + (new Visit())->setRemoteAddr('4.3.2.1'), + (new Visit())->setRemoteAddr('12.34.56.78'), + (new Visit())->setRemoteAddr('1.2.3.4'), + (new Visit())->setRemoteAddr('4.3.2.1'), + (new Visit())->setRemoteAddr('12.34.56.78'), + (new Visit())->setRemoteAddr('4.3.2.1'), + ]; + $apiLimit = 3; + + $this->visitService->getUnlocatedVisits()->willReturn($visits); + $this->visitService->saveVisit(Argument::any())->will(function () { + }); + + $getApiLimit = $this->ipResolver->getApiLimit()->willReturn($apiLimit); + $getApiInterval = $this->ipResolver->getApiInterval()->willReturn(0); + $resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]) + ->shouldBeCalledTimes(count($visits)); + + $this->commandTester->execute([ + 'command' => 'visit:process', + ]); + + $getApiLimit->shouldHaveBeenCalledTimes(\count($visits)); + $getApiInterval->shouldHaveBeenCalledTimes(\round(\count($visits) / $apiLimit)); + $resolveIpLocation->shouldHaveBeenCalledTimes(\count($visits)); + } } diff --git a/module/Common/test/Service/IpApiLocationResolverTest.php b/module/Common/test/Service/IpApiLocationResolverTest.php index 1d36ce9a..84012e73 100644 --- a/module/Common/test/Service/IpApiLocationResolverTest.php +++ b/module/Common/test/Service/IpApiLocationResolverTest.php @@ -65,4 +65,20 @@ class IpApiLocationResolverTest extends TestCase ->shouldBeCalledTimes(1); $this->ipResolver->resolveIpLocation('1.2.3.4'); } + + /** + * @test + */ + public function getApiIntervalReturnsExpectedValue() + { + $this->assertEquals(65, $this->ipResolver->getApiInterval()); + } + + /** + * @test + */ + public function getApiLimitReturnsExpectedValue() + { + $this->assertEquals(145, $this->ipResolver->getApiLimit()); + } }