2016-08-06 18:26:07 +02:00
|
|
|
<?php
|
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;
|
|
|
|
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-06 18:26:07 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand;
|
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
class DisableKeyCommandTest extends TestCase
|
|
|
|
{
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var CommandTester */
|
2018-11-20 19:37:22 +01:00
|
|
|
private $commandTester;
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 19:37:22 +01:00
|
|
|
private $apiKeyService;
|
2016-08-06 18:26:07 +02:00
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
public function setUp(): void
|
2016-08-06 18:26:07 +02:00
|
|
|
{
|
|
|
|
$this->apiKeyService = $this->prophesize(ApiKeyService::class);
|
2018-11-18 16:02:52 +01:00
|
|
|
$command = new DisableKeyCommand($this->apiKeyService->reveal());
|
2016-08-06 18:26:07 +02:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2016-08-06 18:26:07 +02:00
|
|
|
public function providedApiKeyIsDisabled()
|
|
|
|
{
|
|
|
|
$apiKey = 'abcd1234';
|
2018-11-11 13:18:21 +01:00
|
|
|
$this->apiKeyService->disable($apiKey)->shouldBeCalledOnce();
|
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();
|
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
$this->assertStringContainsString('API key "abcd1234" properly disabled', $output);
|
2016-08-06 18:26:07 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2016-08-06 18:26:07 +02:00
|
|
|
public function errorIsReturnedIfServiceThrowsException()
|
|
|
|
{
|
|
|
|
$apiKey = 'abcd1234';
|
2018-11-17 17:36:22 +01:00
|
|
|
$disable = $this->apiKeyService->disable($apiKey)->willThrow(InvalidArgumentException::class);
|
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
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
$this->assertStringContainsString('API key "abcd1234" does not exist.', $output);
|
2018-11-17 17:36:22 +01:00
|
|
|
$disable->shouldHaveBeenCalledOnce();
|
2016-08-06 18:26:07 +02:00
|
|
|
}
|
|
|
|
}
|