Completed
Pull Request — master (#98)
by Alejandro
06:29
created

RenameTagCommandTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 48.48 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 32
loc 66
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A errorIsPrintedIfExceptionIsThrown() 16 16 1
A successIsPrintedIfNoErrorOccurs() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace ShlinkioTest\Shlink\CLI\Command\Tag;
3
4
use PHPUnit\Framework\TestCase;
5
use Prophecy\Prophecy\MethodProphecy;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Shlinkio\Shlink\CLI\Command\Tag\RenameTagCommand;
8
use Shlinkio\Shlink\Core\Entity\Tag;
9
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
10
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
11
use Symfony\Component\Console\Application;
12
use Symfony\Component\Console\Tester\CommandTester;
13
use Zend\I18n\Translator\Translator;
14
15
class RenameTagCommandTest extends TestCase
16
{
17
    /**
18
     * @var RenameTagCommand
19
     */
20
    private $command;
0 ignored issues
show
Unused Code introduced by
The property $command is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
    /**
22
     * @var CommandTester
23
     */
24
    private $commandTester;
25
    /**
26
     * @var ObjectProphecy
27
     */
28
    private $tagService;
29
30
    public function setUp()
31
    {
32
        $this->tagService = $this->prophesize(TagServiceInterface::class);
33
34
        $command = new RenameTagCommand($this->tagService->reveal(), Translator::factory([]));
35
        $app = new Application();
36
        $app->add($command);
37
38
        $this->commandTester = new CommandTester($command);
39
    }
40
41
    /**
42
     * @test
43
     */
44 View Code Duplication
    public function errorIsPrintedIfExceptionIsThrown()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $oldName = 'foo';
47
        $newName = 'bar';
48
        /** @var MethodProphecy $renameTag */
49
        $renameTag = $this->tagService->renameTag($oldName, $newName)->willThrow(EntityDoesNotExistException::class);
50
51
        $this->commandTester->execute([
52
            'oldName' => $oldName,
53
            'newName' => $newName,
54
        ]);
55
        $output = $this->commandTester->getDisplay();
56
57
        $this->assertContains('A tag with name "foo" was not found', $output);
58
        $renameTag->shouldHaveBeenCalled();
59
    }
60
61
    /**
62
     * @test
63
     */
64 View Code Duplication
    public function successIsPrintedIfNoErrorOccurs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $oldName = 'foo';
67
        $newName = 'bar';
68
        /** @var MethodProphecy $renameTag */
69
        $renameTag = $this->tagService->renameTag($oldName, $newName)->willReturn(new Tag());
70
71
        $this->commandTester->execute([
72
            'oldName' => $oldName,
73
            'newName' => $newName,
74
        ]);
75
        $output = $this->commandTester->getDisplay();
76
77
        $this->assertContains('Tag properly renamed', $output);
78
        $renameTag->shouldHaveBeenCalled();
79
    }
80
}
81