2016-08-06 18:26:07 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-06 18:26:07 +02:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Api;
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 12:48:17 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-06 18:26:07 +02:00
|
|
|
use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand;
|
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2019-12-29 22:27:00 +01:00
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
2023-06-18 10:51:59 +02:00
|
|
|
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
|
2016-08-06 18:26:07 +02:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
class DisableKeyCommandTest extends TestCase
|
|
|
|
{
|
2019-12-29 22:27:00 +01:00
|
|
|
private CommandTester $commandTester;
|
2022-10-24 19:53:13 +02:00
|
|
|
private MockObject & ApiKeyServiceInterface $apiKeyService;
|
2016-08-06 18:26:07 +02:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2016-08-06 18:26:07 +02:00
|
|
|
{
|
2022-10-22 12:48:17 +02:00
|
|
|
$this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
|
2023-06-18 10:51:59 +02:00
|
|
|
$this->commandTester = CliTestUtils::testerForCommand(new DisableKeyCommand($this->apiKeyService));
|
2016-08-06 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-11-28 18:42:27 +01:00
|
|
|
public function providedApiKeyIsDisabled(): void
|
2016-08-06 18:26:07 +02:00
|
|
|
{
|
|
|
|
$apiKey = 'abcd1234';
|
2022-10-23 18:15:57 +02:00
|
|
|
$this->apiKeyService->expects($this->once())->method('disable')->with($apiKey);
|
2018-11-17 17:36:22 +01:00
|
|
|
|
2016-08-06 18:26:07 +02:00
|
|
|
$this->commandTester->execute([
|
|
|
|
'apiKey' => $apiKey,
|
|
|
|
]);
|
2018-11-17 17:36:22 +01:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertStringContainsString('API key "abcd1234" properly disabled', $output);
|
2016-08-06 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-11-28 18:42:27 +01:00
|
|
|
public function errorIsReturnedIfServiceThrowsException(): void
|
2016-08-06 18:26:07 +02:00
|
|
|
{
|
|
|
|
$apiKey = 'abcd1234';
|
2019-11-28 18:42:27 +01:00
|
|
|
$expectedMessage = 'API key "abcd1234" does not exist.';
|
2022-10-23 18:15:57 +02:00
|
|
|
$this->apiKeyService->expects($this->once())->method('disable')->with($apiKey)->willThrowException(
|
|
|
|
new InvalidArgumentException($expectedMessage),
|
|
|
|
);
|
2016-08-06 18:26:07 +02:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'apiKey' => $apiKey,
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 17:36:22 +01:00
|
|
|
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertStringContainsString($expectedMessage, $output);
|
2016-08-06 18:26:07 +02:00
|
|
|
}
|
|
|
|
}
|