Migrated ApiKeyServiceTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 23:07:17 +02:00
parent 083ccd36b7
commit ef82158368
2 changed files with 21 additions and 37 deletions

View File

@ -65,7 +65,6 @@
"dms/phpunit-arraysubset-asserts": "^0.4.0", "dms/phpunit-arraysubset-asserts": "^0.4.0",
"infection/infection": "^0.26.15", "infection/infection": "^0.26.15",
"openswoole/ide-helper": "~4.11.5", "openswoole/ide-helper": "~4.11.5",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.8", "phpstan/phpstan": "^1.8",
"phpstan/phpstan-doctrine": "^1.3", "phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-symfony": "^1.2", "phpstan/phpstan-symfony": "^1.2",

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Service;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
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 Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
@ -20,15 +18,15 @@ use Shlinkio\Shlink\Rest\Service\ApiKeyService;
class ApiKeyServiceTest extends TestCase class ApiKeyServiceTest extends TestCase
{ {
use ProphecyTrait;
private ApiKeyService $service; private ApiKeyService $service;
private ObjectProphecy $em; private MockObject $em;
private MockObject $repo;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->prophesize(EntityManager::class); $this->em = $this->createMock(EntityManager::class);
$this->service = new ApiKeyService($this->em->reveal()); $this->repo = $this->createMock(EntityRepository::class);
$this->service = new ApiKeyService($this->em);
} }
/** /**
@ -38,8 +36,8 @@ class ApiKeyServiceTest extends TestCase
*/ */
public function apiKeyIsProperlyCreated(?Chronos $date, ?string $name, array $roles): void public function apiKeyIsProperlyCreated(?Chronos $date, ?string $name, array $roles): void
{ {
$this->em->flush()->shouldBeCalledOnce(); $this->em->expects($this->once())->method('flush');
$this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledOnce(); $this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class));
$key = $this->service->create($date, $name, ...$roles); $key = $this->service->create($date, $name, ...$roles);
@ -69,10 +67,8 @@ class ApiKeyServiceTest extends TestCase
*/ */
public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void
{ {
$repo = $this->prophesize(EntityRepository::class); $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($invalidKey);
$repo->findOneBy(['key' => '12345'])->willReturn($invalidKey) $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->check('12345'); $result = $this->service->check('12345');
@ -92,10 +88,8 @@ class ApiKeyServiceTest extends TestCase
{ {
$apiKey = ApiKey::create(); $apiKey = ApiKey::create();
$repo = $this->prophesize(EntityRepository::class); $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($apiKey);
$repo->findOneBy(['key' => '12345'])->willReturn($apiKey) $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->check('12345'); $result = $this->service->check('12345');
@ -106,10 +100,8 @@ class ApiKeyServiceTest extends TestCase
/** @test */ /** @test */
public function disableThrowsExceptionWhenNoApiKeyIsFound(): void public function disableThrowsExceptionWhenNoApiKeyIsFound(): void
{ {
$repo = $this->prophesize(EntityRepository::class); $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn(null);
$repo->findOneBy(['key' => '12345'])->willReturn(null) $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
@ -120,12 +112,9 @@ class ApiKeyServiceTest extends TestCase
public function disableReturnsDisabledApiKeyWhenFound(): void public function disableReturnsDisabledApiKeyWhenFound(): void
{ {
$key = ApiKey::create(); $key = ApiKey::create();
$repo = $this->prophesize(EntityRepository::class); $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($key);
$repo->findOneBy(['key' => '12345'])->willReturn($key) $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
->shouldBeCalledOnce(); $this->em->expects($this->once())->method('flush');
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$this->em->flush()->shouldBeCalledOnce();
self::assertTrue($key->isEnabled()); self::assertTrue($key->isEnabled());
$returnedKey = $this->service->disable('12345'); $returnedKey = $this->service->disable('12345');
@ -138,10 +127,8 @@ class ApiKeyServiceTest extends TestCase
{ {
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()]; $expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$repo = $this->prophesize(EntityRepository::class); $this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys);
$repo->findBy([])->willReturn($expectedApiKeys) $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->listKeys(); $result = $this->service->listKeys();
@ -153,10 +140,8 @@ class ApiKeyServiceTest extends TestCase
{ {
$expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()]; $expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()];
$repo = $this->prophesize(EntityRepository::class); $this->repo->expects($this->once())->method('findBy')->with(['enabled' => true])->willReturn($expectedApiKeys);
$repo->findBy(['enabled' => true])->willReturn($expectedApiKeys) $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo);
->shouldBeCalledOnce();
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->service->listKeys(true); $result = $this->service->listKeys(true);