mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Migrated VisitLocatorTest to use PHPUnit mocks
This commit is contained in:
parent
a45d6e6b44
commit
04419a7242
@ -7,11 +7,8 @@ namespace ShlinkioTest\Shlink\Core\Visit\Geolocation;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Exception;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
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\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||
@ -32,19 +29,17 @@ use function sprintf;
|
||||
|
||||
class VisitLocatorTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private VisitLocator $visitService;
|
||||
private ObjectProphecy $em;
|
||||
private ObjectProphecy $repo;
|
||||
private MockObject $em;
|
||||
private MockObject $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->em = $this->prophesize(EntityManager::class);
|
||||
$this->repo = $this->prophesize(VisitRepositoryInterface::class);
|
||||
$this->em->getRepository(Visit::class)->willReturn($this->repo->reveal());
|
||||
$this->em = $this->createMock(EntityManager::class);
|
||||
$this->repo = $this->createMock(VisitRepositoryInterface::class);
|
||||
$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()),
|
||||
);
|
||||
|
||||
$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 {
|
||||
});
|
||||
$flush = $this->em->flush()->will(function (): void {
|
||||
});
|
||||
$clear = $this->em->clear()->will(function (): void {
|
||||
});
|
||||
$this->em->expects($this->exactly(count($unlocatedVisits)))->method('persist')->with(
|
||||
$this->isInstanceOf(Visit::class),
|
||||
);
|
||||
$this->em->expects($this->exactly((int) floor(count($unlocatedVisits) / 200) + 1))->method('flush');
|
||||
$this->em->expects($this->exactly((int) floor(count($unlocatedVisits) / 200) + 1))->method('clear');
|
||||
|
||||
$this->visitService->{$serviceMethodName}(new class implements VisitGeolocationHelperInterface {
|
||||
public function geolocateVisit(Visit $visit): Location
|
||||
@ -84,11 +78,6 @@ class VisitLocatorTest extends TestCase
|
||||
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
|
||||
@ -111,14 +100,13 @@ class VisitLocatorTest extends TestCase
|
||||
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 {
|
||||
});
|
||||
$flush = $this->em->flush()->will(function (): void {
|
||||
});
|
||||
$clear = $this->em->clear()->will(function (): void {
|
||||
});
|
||||
$this->em->expects($this->exactly($isNonLocatableAddress ? 1 : 0))->method('persist')->with(
|
||||
$this->isInstanceOf(Visit::class),
|
||||
);
|
||||
$this->em->expects($this->once())->method('flush');
|
||||
$this->em->expects($this->once())->method('clear');
|
||||
|
||||
$this->visitService->{$serviceMethodName}(
|
||||
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
|
||||
@ -162,9 +145,4 @@ class VisitLocatorTest extends TestCase
|
||||
yield 'locateAllVisits - locatable address' => ['locateAllVisits', 'findAllVisits', false];
|
||||
yield 'locateAllVisits - non-locatable address' => ['locateAllVisits', 'findAllVisits', true];
|
||||
}
|
||||
|
||||
private function mockRepoMethod(string $methodName): MethodProphecy
|
||||
{
|
||||
return (new MethodProphecy($this->repo, $methodName, new Argument\ArgumentsWildcard([])));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user