From ec839183e82e6a1d4b175069a607b56efeb0c854 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 23 Sep 2023 08:01:10 +0200 Subject: [PATCH] Add unit test for ApiKeyService::createInitial --- .../Rest/test/Service/ApiKeyServiceTest.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/module/Rest/test/Service/ApiKeyServiceTest.php b/module/Rest/test/Service/ApiKeyServiceTest.php index 952887a5..5c9d28db 100644 --- a/module/Rest/test/Service/ApiKeyServiceTest.php +++ b/module/Rest/test/Service/ApiKeyServiceTest.php @@ -15,6 +15,7 @@ use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition; +use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface; use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Service\ApiKeyService; @@ -22,12 +23,12 @@ class ApiKeyServiceTest extends TestCase { private ApiKeyService $service; private MockObject & EntityManager $em; - private MockObject & EntityRepository $repo; + private MockObject & ApiKeyRepositoryInterface $repo; protected function setUp(): void { $this->em = $this->createMock(EntityManager::class); - $this->repo = $this->createMock(EntityRepository::class); + $this->repo = $this->createMock(ApiKeyRepositoryInterface::class); $this->service = new ApiKeyService($this->em); } @@ -150,4 +151,21 @@ class ApiKeyServiceTest extends TestCase self::assertEquals($expectedApiKeys, $result); } + + #[Test, DataProvider('provideInitialApiKeys')] + public function createInitialDelegatesToRepository(?ApiKey $apiKey): void + { + $this->repo->expects($this->once())->method('createInitialApiKey')->with('the_key')->willReturn($apiKey); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); + + $result = $this->service->createInitial('the_key'); + + self::assertSame($result, $apiKey); + } + + public static function provideInitialApiKeys(): iterable + { + yield 'first api key' => [ApiKey::create()]; + yield 'existing api keys' => [null]; + } }