Migrated VisitLocatorTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya
2022-10-23 21:21:23 +02:00
parent a45d6e6b44
commit 04419a7242

View File

@@ -7,11 +7,8 @@ namespace ShlinkioTest\Shlink\Core\Visit\Geolocation;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Exception; use Exception;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException; use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Entity\Visit;
@@ -32,19 +29,17 @@ use function sprintf;
class VisitLocatorTest extends TestCase class VisitLocatorTest extends TestCase
{ {
use ProphecyTrait;
private VisitLocator $visitService; private VisitLocator $visitService;
private ObjectProphecy $em; private MockObject $em;
private ObjectProphecy $repo; private MockObject $repo;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->prophesize(EntityManager::class); $this->em = $this->createMock(EntityManager::class);
$this->repo = $this->prophesize(VisitRepositoryInterface::class); $this->repo = $this->createMock(VisitRepositoryInterface::class);
$this->em->getRepository(Visit::class)->willReturn($this->repo->reveal()); $this->em->method('getRepository')->with(Visit::class)->willReturn($this->repo);
$this->visitService = new VisitLocator($this->em->reveal()); $this->visitService = new VisitLocator($this->em);
} }
/** /**
@@ -61,14 +56,13 @@ class VisitLocatorTest extends TestCase
Visit::forValidShortUrl(ShortUrl::withLongUrl(sprintf('short_code_%s', $i)), Visitor::emptyInstance()), Visit::forValidShortUrl(ShortUrl::withLongUrl(sprintf('short_code_%s', $i)), Visitor::emptyInstance()),
); );
$findVisits = $this->mockRepoMethod($expectedRepoMethodName)->willReturn($unlocatedVisits); $this->repo->expects($this->once())->method($expectedRepoMethodName)->willReturn($unlocatedVisits);
$persist = $this->em->persist(Argument::type(Visit::class))->will(function (): void { $this->em->expects($this->exactly(count($unlocatedVisits)))->method('persist')->with(
}); $this->isInstanceOf(Visit::class),
$flush = $this->em->flush()->will(function (): void { );
}); $this->em->expects($this->exactly((int) floor(count($unlocatedVisits) / 200) + 1))->method('flush');
$clear = $this->em->clear()->will(function (): void { $this->em->expects($this->exactly((int) floor(count($unlocatedVisits) / 200) + 1))->method('clear');
});
$this->visitService->{$serviceMethodName}(new class implements VisitGeolocationHelperInterface { $this->visitService->{$serviceMethodName}(new class implements VisitGeolocationHelperInterface {
public function geolocateVisit(Visit $visit): Location public function geolocateVisit(Visit $visit): Location
@@ -84,11 +78,6 @@ class VisitLocatorTest extends TestCase
Assert::assertInstanceOf(Visit::class, array_shift($args)); Assert::assertInstanceOf(Visit::class, array_shift($args));
} }
}); });
$findVisits->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledTimes(count($unlocatedVisits));
$flush->shouldHaveBeenCalledTimes(floor(count($unlocatedVisits) / 200) + 1);
$clear->shouldHaveBeenCalledTimes(floor(count($unlocatedVisits) / 200) + 1);
} }
public function provideMethodNames(): iterable public function provideMethodNames(): iterable
@@ -111,14 +100,13 @@ class VisitLocatorTest extends TestCase
Visit::forValidShortUrl(ShortUrl::withLongUrl('foo'), Visitor::emptyInstance()), Visit::forValidShortUrl(ShortUrl::withLongUrl('foo'), Visitor::emptyInstance()),
]; ];
$findVisits = $this->mockRepoMethod($expectedRepoMethodName)->willReturn($unlocatedVisits); $this->repo->expects($this->once())->method($expectedRepoMethodName)->willReturn($unlocatedVisits);
$persist = $this->em->persist(Argument::type(Visit::class))->will(function (): void { $this->em->expects($this->exactly($isNonLocatableAddress ? 1 : 0))->method('persist')->with(
}); $this->isInstanceOf(Visit::class),
$flush = $this->em->flush()->will(function (): void { );
}); $this->em->expects($this->once())->method('flush');
$clear = $this->em->clear()->will(function (): void { $this->em->expects($this->once())->method('clear');
});
$this->visitService->{$serviceMethodName}( $this->visitService->{$serviceMethodName}(
new class ($isNonLocatableAddress) implements VisitGeolocationHelperInterface { new class ($isNonLocatableAddress) implements VisitGeolocationHelperInterface {
@@ -138,11 +126,6 @@ class VisitLocatorTest extends TestCase
} }
}, },
); );
$findVisits->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledTimes($isNonLocatableAddress ? 1 : 0);
$flush->shouldHaveBeenCalledOnce();
$clear->shouldHaveBeenCalledOnce();
} }
public function provideIsNonLocatableAddress(): iterable public function provideIsNonLocatableAddress(): iterable
@@ -162,9 +145,4 @@ class VisitLocatorTest extends TestCase
yield 'locateAllVisits - locatable address' => ['locateAllVisits', 'findAllVisits', false]; yield 'locateAllVisits - locatable address' => ['locateAllVisits', 'findAllVisits', false];
yield 'locateAllVisits - non-locatable address' => ['locateAllVisits', 'findAllVisits', true]; yield 'locateAllVisits - non-locatable address' => ['locateAllVisits', 'findAllVisits', true];
} }
private function mockRepoMethod(string $methodName): MethodProphecy
{
return (new MethodProphecy($this->repo, $methodName, new Argument\ArgumentsWildcard([])));
}
} }