From a4c34ff7beb16918e52689f9677fe9db2167233e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 22 Oct 2022 12:52:11 +0200 Subject: [PATCH] Migrated GenerateKeyCommandTest to use PHPUnit mocks --- .../Command/Api/GenerateKeyCommandTest.php | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php index 6db8581b..02e704ee 100644 --- a/module/CLI/test/Command/Api/GenerateKeyCommandTest.php +++ b/module/CLI/test/Command/Api/GenerateKeyCommandTest.php @@ -5,9 +5,8 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\CLI\Command\Api; use Cake\Chronos\Chronos; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface; use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand; use Shlinkio\Shlink\Rest\Entity\ApiKey; @@ -21,22 +20,25 @@ class GenerateKeyCommandTest extends TestCase use CliTestUtilsTrait; private CommandTester $commandTester; - private ObjectProphecy $apiKeyService; + private MockObject $apiKeyService; protected function setUp(): void { - $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class); - $roleResolver = $this->prophesize(RoleResolverInterface::class); - $roleResolver->determineRoles(Argument::type(InputInterface::class))->willReturn([]); + $this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class); + $roleResolver = $this->createMock(RoleResolverInterface::class); + $roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]); - $command = new GenerateKeyCommand($this->apiKeyService->reveal(), $roleResolver->reveal()); + $command = new GenerateKeyCommand($this->apiKeyService, $roleResolver); $this->commandTester = $this->testerForCommand($command); } /** @test */ public function noExpirationDateIsDefinedIfNotProvided(): void { - $this->apiKeyService->create(null, null)->shouldBeCalledOnce()->willReturn(ApiKey::create()); + $this->apiKeyService->expects($this->once())->method('create')->with( + $this->isNull(), + $this->isNull(), + )->willReturn(ApiKey::create()); $this->commandTester->execute([]); $output = $this->commandTester->getDisplay(); @@ -47,9 +49,10 @@ class GenerateKeyCommandTest extends TestCase /** @test */ public function expirationDateIsDefinedIfProvided(): void { - $this->apiKeyService->create(Argument::type(Chronos::class), null)->shouldBeCalledOnce()->willReturn( - ApiKey::create(), - ); + $this->apiKeyService->expects($this->once())->method('create')->with( + $this->isInstanceOf(Chronos::class), + $this->isNull(), + )->willReturn(ApiKey::create()); $this->commandTester->execute([ '--expiration-date' => '2016-01-01', @@ -59,9 +62,10 @@ class GenerateKeyCommandTest extends TestCase /** @test */ public function nameIsDefinedIfProvided(): void { - $this->apiKeyService->create(null, Argument::type('string'))->shouldBeCalledOnce()->willReturn( - ApiKey::create(), - ); + $this->apiKeyService->expects($this->once())->method('create')->with( + $this->isNull(), + $this->isType('string'), + )->willReturn(ApiKey::create()); $this->commandTester->execute([ '--name' => 'Alice',