Migrated UpdateGeoLiteDbTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 09:42:21 +02:00
parent e01e370d16
commit 51fcbfb3c2

View File

@ -4,10 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher; namespace ShlinkioTest\Shlink\Core\EventDispatcher;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
@ -20,24 +18,18 @@ use function Functional\map;
class UpdateGeoLiteDbTest extends TestCase class UpdateGeoLiteDbTest extends TestCase
{ {
use ProphecyTrait;
private UpdateGeoLiteDb $listener; private UpdateGeoLiteDb $listener;
private ObjectProphecy $dbUpdater; private MockObject $dbUpdater;
private ObjectProphecy $logger; private MockObject $logger;
private ObjectProphecy $eventDispatcher; private MockObject $eventDispatcher;
protected function setUp(): void protected function setUp(): void
{ {
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class); $this->dbUpdater = $this->createMock(GeolocationDbUpdaterInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->listener = new UpdateGeoLiteDb( $this->listener = new UpdateGeoLiteDb($this->dbUpdater, $this->logger, $this->eventDispatcher);
$this->dbUpdater->reveal(),
$this->logger->reveal(),
$this->eventDispatcher->reveal(),
);
} }
/** @test */ /** @test */
@ -45,15 +37,15 @@ class UpdateGeoLiteDbTest extends TestCase
{ {
$e = new RuntimeException(); $e = new RuntimeException();
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willThrow($e); $this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willThrowException($e);
$logError = $this->logger->error('GeoLite2 database download failed. {e}', ['e' => $e]); $this->logger->expects($this->once())->method('error')->with(
$this->equalTo('GeoLite2 database download failed. {e}'),
$this->equalTo(['e' => $e]),
);
$this->logger->expects($this->never())->method('notice');
$this->eventDispatcher->expects($this->never())->method('dispatch');
($this->listener)(); ($this->listener)();
$checkDbUpdate->shouldHaveBeenCalledOnce();
$logError->shouldHaveBeenCalledOnce();
$this->logger->notice(Argument::cetera())->shouldNotHaveBeenCalled();
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
} }
/** /**
@ -62,22 +54,17 @@ class UpdateGeoLiteDbTest extends TestCase
*/ */
public function noticeMessageIsPrintedWhenFirstCallbackIsInvoked(bool $oldDbExists, string $expectedMessage): void public function noticeMessageIsPrintedWhenFirstCallbackIsInvoked(bool $oldDbExists, string $expectedMessage): void
{ {
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will( $this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturnCallback(
function (array $args) use ($oldDbExists): GeolocationResult { function (callable $firstCallback) use ($oldDbExists): GeolocationResult {
[$firstCallback] = $args;
$firstCallback($oldDbExists); $firstCallback($oldDbExists);
return GeolocationResult::DB_IS_UP_TO_DATE; return GeolocationResult::DB_IS_UP_TO_DATE;
}, },
); );
$logNotice = $this->logger->notice($expectedMessage); $this->logger->expects($this->once())->method('notice')->with($this->equalTo($expectedMessage));
$this->logger->expects($this->never())->method('error');
$this->eventDispatcher->expects($this->never())->method('dispatch');
($this->listener)(); ($this->listener)();
$checkDbUpdate->shouldHaveBeenCalledOnce();
$logNotice->shouldHaveBeenCalledOnce();
$this->logger->error(Argument::cetera())->shouldNotHaveBeenCalled();
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
} }
public function provideFlags(): iterable public function provideFlags(): iterable
@ -96,10 +83,8 @@ class UpdateGeoLiteDbTest extends TestCase
bool $oldDbExists, bool $oldDbExists,
?string $expectedMessage, ?string $expectedMessage,
): void { ): void {
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will( $this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturnCallback(
function (array $args) use ($total, $downloaded, $oldDbExists): GeolocationResult { function ($_, callable $secondCallback) use ($total, $downloaded, $oldDbExists): GeolocationResult {
[, $secondCallback] = $args;
// Invoke several times to ensure the log is printed only once // Invoke several times to ensure the log is printed only once
$secondCallback($total, $downloaded, $oldDbExists); $secondCallback($total, $downloaded, $oldDbExists);
$secondCallback($total, $downloaded, $oldDbExists); $secondCallback($total, $downloaded, $oldDbExists);
@ -108,18 +93,12 @@ class UpdateGeoLiteDbTest extends TestCase
return GeolocationResult::DB_UPDATED; return GeolocationResult::DB_UPDATED;
}, },
); );
$logNotice = $this->logger->notice($expectedMessage ?? Argument::cetera()); $logNoticeExpectation = $expectedMessage !== null ? $this->once() : $this->never();
$this->logger->expects($logNoticeExpectation)->method('notice')->with($this->equalTo($expectedMessage));
$this->logger->expects($this->never())->method('error');
$this->eventDispatcher->expects($this->never())->method('dispatch');
($this->listener)(); ($this->listener)();
if ($expectedMessage !== null) {
$logNotice->shouldHaveBeenCalledOnce();
} else {
$logNotice->shouldNotHaveBeenCalled();
}
$checkDbUpdate->shouldHaveBeenCalledOnce();
$this->logger->error(Argument::cetera())->shouldNotHaveBeenCalled();
$this->eventDispatcher->dispatch(Argument::cetera())->shouldNotHaveBeenCalled();
} }
public function provideDownloaded(): iterable public function provideDownloaded(): iterable
@ -142,12 +121,12 @@ class UpdateGeoLiteDbTest extends TestCase
GeolocationResult $result, GeolocationResult $result,
int $expectedDispatches, int $expectedDispatches,
): void { ): void {
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->willReturn($result); $this->dbUpdater->expects($this->once())->method('checkDbUpdate')->withAnyParameters()->willReturn($result);
$this->eventDispatcher->expects($this->exactly($expectedDispatches))->method('dispatch')->with(
$this->equalTo(new GeoLiteDbCreated())
);
($this->listener)(); ($this->listener)();
$checkDbUpdate->shouldHaveBeenCalledOnce();
$this->eventDispatcher->dispatch(new GeoLiteDbCreated())->shouldHaveBeenCalledTimes($expectedDispatches);
} }
public function provideGeolocationResults(): iterable public function provideGeolocationResults(): iterable