shlink/module/CLI/test/Command/Api/ListKeysCommandTest.php

118 lines
4.2 KiB
PHP
Raw Normal View History

2016-08-06 19:50:50 +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:50:50 +03:00
namespace ShlinkioTest\Shlink\CLI\Command\Api;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
2016-08-06 19:50:50 +03:00
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Api\ListKeysCommand;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
2016-08-06 19:50:50 +03:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2019-12-30 00:27:00 +03:00
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
2016-08-06 19:50:50 +03:00
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class ListKeysCommandTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
private ObjectProphecy $apiKeyService;
2016-08-06 19:50:50 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2016-08-06 19:50:50 +03:00
{
2019-12-30 00:27:00 +03:00
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
2018-11-18 18:02:52 +03:00
$command = new ListKeysCommand($this->apiKeyService->reveal());
2016-08-06 19:50:50 +03:00
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);
}
/**
* @test
* @dataProvider provideKeysAndOutputs
*/
public function returnsExpectedOutput(array $keys, bool $enabledOnly, string $expected): void
2016-08-06 19:50:50 +03:00
{
$listKeys = $this->apiKeyService->listKeys($enabledOnly)->willReturn($keys);
2018-11-17 19:36:22 +03:00
$this->commandTester->execute(['--enabled-only' => $enabledOnly]);
2018-11-17 19:36:22 +03:00
$output = $this->commandTester->getDisplay();
self::assertEquals($expected, $output);
$listKeys->shouldHaveBeenCalledOnce();
2016-08-06 19:50:50 +03:00
}
public function provideKeysAndOutputs(): iterable
2016-08-06 19:50:50 +03:00
{
yield 'all keys' => [
[ApiKey::withKey('foo'), ApiKey::withKey('bar'), ApiKey::withKey('baz')],
false,
<<<OUTPUT
+-----+------------+-----------------+-------+
| Key | Is enabled | Expiration date | Roles |
+-----+------------+-----------------+-------+
| foo | +++ | - | Admin |
| bar | +++ | - | Admin |
| baz | +++ | - | Admin |
+-----+------------+-----------------+-------+
2018-11-17 19:36:22 +03:00
OUTPUT,
];
yield 'enabled keys' => [
[ApiKey::withKey('foo')->disable(), ApiKey::withKey('bar')],
true,
<<<OUTPUT
+-----+-----------------+-------+
| Key | Expiration date | Roles |
+-----+-----------------+-------+
| foo | - | Admin |
| bar | - | Admin |
+-----+-----------------+-------+
OUTPUT,
];
yield 'with roles' => [
[
ApiKey::withKey('foo'),
$this->apiKeyWithRoles('bar', [RoleDefinition::forAuthoredShortUrls()]),
$this->apiKeyWithRoles('baz', [RoleDefinition::forDomain((new Domain('example.com'))->setId('1'))]),
ApiKey::withKey('foo2'),
$this->apiKeyWithRoles('baz2', [
RoleDefinition::forAuthoredShortUrls(),
RoleDefinition::forDomain((new Domain('example.com'))->setId('1')),
]),
ApiKey::withKey('foo3'),
],
true,
<<<OUTPUT
+------+-----------------+--------------------------+
| Key | Expiration date | Roles |
+------+-----------------+--------------------------+
| foo | - | Admin |
| bar | - | Author only |
| baz | - | Domain only: example.com |
| foo2 | - | Admin |
| baz2 | - | Author only |
| | | Domain only: example.com |
| foo3 | - | Admin |
+------+-----------------+--------------------------+
OUTPUT,
];
}
private function apiKeyWithRoles(string $key, array $roles): ApiKey
{
$apiKey = ApiKey::withKey($key);
foreach ($roles as $role) {
$apiKey->registerRole($role);
}
2018-11-17 19:36:22 +03:00
return $apiKey;
2016-08-06 19:50:50 +03:00
}
}