RenameTagCommandTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 27
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A errorIsPrintedIfExceptionIsThrown() 0 14 1
A setUp() 0 9 1
A successIsPrintedIfNoErrorOccurs() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\CLI\Command\Tag;
6
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\PhpUnit\ProphecyTrait;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Shlinkio\Shlink\CLI\Command\Tag\RenameTagCommand;
11
use Shlinkio\Shlink\Core\Entity\Tag;
12
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
13
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Tester\CommandTester;
16
17
class RenameTagCommandTest extends TestCase
18
{
19
    use ProphecyTrait;
20
21
    private CommandTester $commandTester;
22
    private ObjectProphecy $tagService;
23
24
    public function setUp(): void
25
    {
26
        $this->tagService = $this->prophesize(TagServiceInterface::class);
27
28
        $command = new RenameTagCommand($this->tagService->reveal());
29
        $app = new Application();
30
        $app->add($command);
31
32
        $this->commandTester = new CommandTester($command);
33
    }
34
35
    /** @test */
36
    public function errorIsPrintedIfExceptionIsThrown(): void
37
    {
38
        $oldName = 'foo';
39
        $newName = 'bar';
40
        $renameTag = $this->tagService->renameTag($oldName, $newName)->willThrow(TagNotFoundException::fromTag('foo'));
41
42
        $this->commandTester->execute([
43
            'oldName' => $oldName,
44
            'newName' => $newName,
45
        ]);
46
        $output = $this->commandTester->getDisplay();
47
48
        self::assertStringContainsString('Tag with name "foo" could not be found', $output);
49
        $renameTag->shouldHaveBeenCalled();
50
    }
51
52
    /** @test */
53
    public function successIsPrintedIfNoErrorOccurs(): void
54
    {
55
        $oldName = 'foo';
56
        $newName = 'bar';
57
        $renameTag = $this->tagService->renameTag($oldName, $newName)->willReturn(new Tag($newName));
58
59
        $this->commandTester->execute([
60
            'oldName' => $oldName,
61
            'newName' => $newName,
62
        ]);
63
        $output = $this->commandTester->getDisplay();
64
65
        self::assertStringContainsString('Tag properly renamed', $output);
66
        $renameTag->shouldHaveBeenCalled();
67
    }
68
}
69