From b1f814e118d7c7a8db080157c5602a5559881d67 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 23 Oct 2022 22:36:16 +0200 Subject: [PATCH] Migrated InitialApiKeyDelegatorTest to use PHPUnit mocks --- .../ApiKey/InitialApiKeyDelegatorTest.php | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/module/Rest/test/ApiKey/InitialApiKeyDelegatorTest.php b/module/Rest/test/ApiKey/InitialApiKeyDelegatorTest.php index 1db53b80..a44700a6 100644 --- a/module/Rest/test/ApiKey/InitialApiKeyDelegatorTest.php +++ b/module/Rest/test/ApiKey/InitialApiKeyDelegatorTest.php @@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\ApiKey; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Mezzio\Application; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use Psr\Container\ContainerInterface; use Shlinkio\Shlink\Rest\ApiKey\InitialApiKeyDelegator; use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface; @@ -18,15 +16,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey; class InitialApiKeyDelegatorTest extends TestCase { - use ProphecyTrait; - private InitialApiKeyDelegator $delegator; - private ObjectProphecy $container; + private MockObject $container; protected function setUp(): void { $this->delegator = new InitialApiKeyDelegator(); - $this->container = $this->prophesize(ContainerInterface::class); + $this->container = $this->createMock(ContainerInterface::class); } /** @@ -35,21 +31,21 @@ class InitialApiKeyDelegatorTest extends TestCase */ public function apiKeyIsInitializedWhenAppropriate(array $config, int $expectedCalls): void { - $app = $this->prophesize(Application::class)->reveal(); - $apiKeyRepo = $this->prophesize(ApiKeyRepositoryInterface::class); - $em = $this->prophesize(EntityManagerInterface::class); + $app = $this->createMock(Application::class); + $apiKeyRepo = $this->createMock(ApiKeyRepositoryInterface::class); + $apiKeyRepo->expects($this->exactly($expectedCalls))->method('createInitialApiKey'); + $em = $this->createMock(EntityManagerInterface::class); + $em->expects($this->exactly($expectedCalls))->method('getRepository')->with(ApiKey::class)->willReturn( + $apiKeyRepo, + ); + $this->container->expects($this->exactly($expectedCalls + 1))->method('get')->willReturnMap([ + ['config', $config], + [EntityManager::class, $em], + ]); - $getConfig = $this->container->get('config')->willReturn($config); - $getRepo = $em->getRepository(ApiKey::class)->willReturn($apiKeyRepo->reveal()); - $getEm = $this->container->get(EntityManager::class)->willReturn($em->reveal()); - - $result = ($this->delegator)($this->container->reveal(), '', fn () => $app); + $result = ($this->delegator)($this->container, '', fn () => $app); self::assertSame($result, $app); - $getConfig->shouldHaveBeenCalledOnce(); - $getRepo->shouldHaveBeenCalledTimes($expectedCalls); - $getEm->shouldHaveBeenCalledTimes($expectedCalls); - $apiKeyRepo->createInitialApiKey(Argument::any())->shouldHaveBeenCalledTimes($expectedCalls); } public function provideConfigs(): iterable