From ef82158368c15bab15e7241e5d868d1e47a779cb Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 23 Oct 2022 23:07:17 +0200 Subject: [PATCH] Migrated ApiKeyServiceTest to use PHPUnit mocks --- composer.json | 1 - .../Rest/test/Service/ApiKeyServiceTest.php | 57 +++++++------------ 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/composer.json b/composer.json index 5f72b1db..cdf2c42e 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,6 @@ "dms/phpunit-arraysubset-asserts": "^0.4.0", "infection/infection": "^0.26.15", "openswoole/ide-helper": "~4.11.5", - "phpspec/prophecy-phpunit": "^2.0", "phpstan/phpstan": "^1.8", "phpstan/phpstan-doctrine": "^1.3", "phpstan/phpstan-symfony": "^1.2", diff --git a/module/Rest/test/Service/ApiKeyServiceTest.php b/module/Rest/test/Service/ApiKeyServiceTest.php index 66bd4c28..9336da9e 100644 --- a/module/Rest/test/Service/ApiKeyServiceTest.php +++ b/module/Rest/test/Service/ApiKeyServiceTest.php @@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Service; use Cake\Chronos\Chronos; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Domain\Entity\Domain; use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta; @@ -20,15 +18,15 @@ use Shlinkio\Shlink\Rest\Service\ApiKeyService; class ApiKeyServiceTest extends TestCase { - use ProphecyTrait; - private ApiKeyService $service; - private ObjectProphecy $em; + private MockObject $em; + private MockObject $repo; protected function setUp(): void { - $this->em = $this->prophesize(EntityManager::class); - $this->service = new ApiKeyService($this->em->reveal()); + $this->em = $this->createMock(EntityManager::class); + $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 { - $this->em->flush()->shouldBeCalledOnce(); - $this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledOnce(); + $this->em->expects($this->once())->method('flush'); + $this->em->expects($this->once())->method('persist')->with($this->isInstanceOf(ApiKey::class)); $key = $this->service->create($date, $name, ...$roles); @@ -69,10 +67,8 @@ class ApiKeyServiceTest extends TestCase */ public function checkReturnsFalseForInvalidApiKeys(?ApiKey $invalidKey): void { - $repo = $this->prophesize(EntityRepository::class); - $repo->findOneBy(['key' => '12345'])->willReturn($invalidKey) - ->shouldBeCalledOnce(); - $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($invalidKey); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); $result = $this->service->check('12345'); @@ -92,10 +88,8 @@ class ApiKeyServiceTest extends TestCase { $apiKey = ApiKey::create(); - $repo = $this->prophesize(EntityRepository::class); - $repo->findOneBy(['key' => '12345'])->willReturn($apiKey) - ->shouldBeCalledOnce(); - $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($apiKey); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); $result = $this->service->check('12345'); @@ -106,10 +100,8 @@ class ApiKeyServiceTest extends TestCase /** @test */ public function disableThrowsExceptionWhenNoApiKeyIsFound(): void { - $repo = $this->prophesize(EntityRepository::class); - $repo->findOneBy(['key' => '12345'])->willReturn(null) - ->shouldBeCalledOnce(); - $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn(null); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); $this->expectException(InvalidArgumentException::class); @@ -120,12 +112,9 @@ class ApiKeyServiceTest extends TestCase public function disableReturnsDisabledApiKeyWhenFound(): void { $key = ApiKey::create(); - $repo = $this->prophesize(EntityRepository::class); - $repo->findOneBy(['key' => '12345'])->willReturn($key) - ->shouldBeCalledOnce(); - $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); - - $this->em->flush()->shouldBeCalledOnce(); + $this->repo->expects($this->once())->method('findOneBy')->with(['key' => '12345'])->willReturn($key); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); + $this->em->expects($this->once())->method('flush'); self::assertTrue($key->isEnabled()); $returnedKey = $this->service->disable('12345'); @@ -138,10 +127,8 @@ class ApiKeyServiceTest extends TestCase { $expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()]; - $repo = $this->prophesize(EntityRepository::class); - $repo->findBy([])->willReturn($expectedApiKeys) - ->shouldBeCalledOnce(); - $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + $this->repo->expects($this->once())->method('findBy')->with([])->willReturn($expectedApiKeys); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); $result = $this->service->listKeys(); @@ -153,10 +140,8 @@ class ApiKeyServiceTest extends TestCase { $expectedApiKeys = [ApiKey::create(), ApiKey::create(), ApiKey::create()]; - $repo = $this->prophesize(EntityRepository::class); - $repo->findBy(['enabled' => true])->willReturn($expectedApiKeys) - ->shouldBeCalledOnce(); - $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal()); + $this->repo->expects($this->once())->method('findBy')->with(['enabled' => true])->willReturn($expectedApiKeys); + $this->em->method('getRepository')->with(ApiKey::class)->willReturn($this->repo); $result = $this->service->listKeys(true);