ListKeysCommandTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 33
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A onlyEnabledKeysAreListedIfEnabledOnlyIsProvided() 0 17 1
A everythingIsListedIfEnabledOnlyIsNotProvided() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\CLI\Command\Api;
6
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\PhpUnit\ProphecyTrait;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Shlinkio\Shlink\CLI\Command\Api\ListKeysCommand;
11
use Shlinkio\Shlink\Rest\Entity\ApiKey;
12
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Tester\CommandTester;
15
16
class ListKeysCommandTest extends TestCase
17
{
18
    use ProphecyTrait;
19
20
    private CommandTester $commandTester;
21
    private ObjectProphecy $apiKeyService;
22
23
    public function setUp(): void
24
    {
25
        $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
26
        $command = new ListKeysCommand($this->apiKeyService->reveal());
27
        $app = new Application();
28
        $app->add($command);
29
        $this->commandTester = new CommandTester($command);
30
    }
31
32
    /** @test */
33
    public function everythingIsListedIfEnabledOnlyIsNotProvided(): void
34
    {
35
        $this->apiKeyService->listKeys(false)->willReturn([
36
            new ApiKey(),
37
            new ApiKey(),
38
            new ApiKey(),
39
        ])->shouldBeCalledOnce();
40
41
        $this->commandTester->execute([]);
42
        $output = $this->commandTester->getDisplay();
43
44
        self::assertStringContainsString('Key', $output);
45
        self::assertStringContainsString('Is enabled', $output);
46
        self::assertStringContainsString(' +++ ', $output);
47
        self::assertStringNotContainsString(' --- ', $output);
48
        self::assertStringContainsString('Expiration date', $output);
49
    }
50
51
    /** @test */
52
    public function onlyEnabledKeysAreListedIfEnabledOnlyIsProvided(): void
53
    {
54
        $this->apiKeyService->listKeys(true)->willReturn([
55
            (new ApiKey())->disable(),
56
            new ApiKey(),
57
        ])->shouldBeCalledOnce();
58
59
        $this->commandTester->execute([
60
            '--enabledOnly' => true,
61
        ]);
62
        $output = $this->commandTester->getDisplay();
63
64
        self::assertStringContainsString('Key', $output);
65
        self::assertStringNotContainsString('Is enabled', $output);
66
        self::assertStringNotContainsString(' +++ ', $output);
67
        self::assertStringNotContainsString(' --- ', $output);
68
        self::assertStringContainsString('Expiration date', $output);
69
    }
70
}
71