2016-08-06 19:26:07 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-06 19:26:07 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Api;
|
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-06 19:26:07 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand;
|
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2019-12-30 00:27:00 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
2021-04-08 14:42:56 +03:00
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
2016-08-06 19:26:07 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
class DisableKeyCommandTest extends TestCase
|
|
|
|
{
|
2021-04-08 14:42:56 +03:00
|
|
|
use CliTestUtilsTrait;
|
2020-11-02 13:50:19 +03:00
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
|
|
|
private ObjectProphecy $apiKeyService;
|
2016-08-06 19:26:07 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-08-06 19:26:07 +03:00
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
|
2021-04-08 14:42:56 +03:00
|
|
|
$this->commandTester = $this->testerForCommand(new DisableKeyCommand($this->apiKeyService->reveal()));
|
2016-08-06 19:26:07 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-28 20:42:27 +03:00
|
|
|
public function providedApiKeyIsDisabled(): void
|
2016-08-06 19:26:07 +03:00
|
|
|
{
|
|
|
|
$apiKey = 'abcd1234';
|
2018-11-11 15:18:21 +03:00
|
|
|
$this->apiKeyService->disable($apiKey)->shouldBeCalledOnce();
|
2018-11-17 19:36:22 +03:00
|
|
|
|
2016-08-06 19:26:07 +03:00
|
|
|
$this->commandTester->execute([
|
|
|
|
'apiKey' => $apiKey,
|
|
|
|
]);
|
2018-11-17 19:36:22 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString('API key "abcd1234" properly disabled', $output);
|
2016-08-06 19:26:07 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-11-28 20:42:27 +03:00
|
|
|
public function errorIsReturnedIfServiceThrowsException(): void
|
2016-08-06 19:26:07 +03:00
|
|
|
{
|
|
|
|
$apiKey = 'abcd1234';
|
2019-11-28 20:42:27 +03:00
|
|
|
$expectedMessage = 'API key "abcd1234" does not exist.';
|
|
|
|
$disable = $this->apiKeyService->disable($apiKey)->willThrow(new InvalidArgumentException($expectedMessage));
|
2016-08-06 19:26:07 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'apiKey' => $apiKey,
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 19:36:22 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString($expectedMessage, $output);
|
2018-11-17 19:36:22 +03:00
|
|
|
$disable->shouldHaveBeenCalledOnce();
|
2016-08-06 19:26:07 +03:00
|
|
|
}
|
|
|
|
}
|