Migrated DisableKeyCommandTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 12:48:17 +02:00
parent 58db902084
commit 2b7b5e9a8f

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Api; namespace ShlinkioTest\Shlink\CLI\Command\Api;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand; use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
@ -17,19 +17,19 @@ class DisableKeyCommandTest extends TestCase
use CliTestUtilsTrait; use CliTestUtilsTrait;
private CommandTester $commandTester; private CommandTester $commandTester;
private ObjectProphecy $apiKeyService; private MockObject $apiKeyService;
protected function setUp(): void protected function setUp(): void
{ {
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class); $this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
$this->commandTester = $this->testerForCommand(new DisableKeyCommand($this->apiKeyService->reveal())); $this->commandTester = $this->testerForCommand(new DisableKeyCommand($this->apiKeyService));
} }
/** @test */ /** @test */
public function providedApiKeyIsDisabled(): void public function providedApiKeyIsDisabled(): void
{ {
$apiKey = 'abcd1234'; $apiKey = 'abcd1234';
$this->apiKeyService->disable($apiKey)->shouldBeCalledOnce(); $this->apiKeyService->expects($this->once())->method('disable')->with($this->equalTo($apiKey));
$this->commandTester->execute([ $this->commandTester->execute([
'apiKey' => $apiKey, 'apiKey' => $apiKey,
@ -44,7 +44,9 @@ class DisableKeyCommandTest extends TestCase
{ {
$apiKey = 'abcd1234'; $apiKey = 'abcd1234';
$expectedMessage = 'API key "abcd1234" does not exist.'; $expectedMessage = 'API key "abcd1234" does not exist.';
$disable = $this->apiKeyService->disable($apiKey)->willThrow(new InvalidArgumentException($expectedMessage)); $this->apiKeyService->expects($this->once())->method('disable')->with(
$this->equalTo($apiKey),
)->willThrowException(new InvalidArgumentException($expectedMessage));
$this->commandTester->execute([ $this->commandTester->execute([
'apiKey' => $apiKey, 'apiKey' => $apiKey,
@ -52,6 +54,5 @@ class DisableKeyCommandTest extends TestCase
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
self::assertStringContainsString($expectedMessage, $output); self::assertStringContainsString($expectedMessage, $output);
$disable->shouldHaveBeenCalledOnce();
} }
} }