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

CreateTagCommandTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 55
loc 55
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 10 10 1
A errorIsReturnedWhenNoTagsAreProvided() 7 7 1
A serviceIsInvokedOnSuccess() 14 14 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\CreateTagCommand;
8
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
9
use Symfony\Component\Console\Application;
10
use Symfony\Component\Console\Tester\CommandTester;
11
use Zend\I18n\Translator\Translator;
12
13 View Code Duplication
class CreateTagCommandTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * @var CreateTagCommand
17
     */
18
    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...
19
    /**
20
     * @var CommandTester
21
     */
22
    private $commandTester;
23
    /**
24
     * @var ObjectProphecy
25
     */
26
    private $tagService;
27
28
    public function setUp()
29
    {
30
        $this->tagService = $this->prophesize(TagServiceInterface::class);
31
32
        $command = new CreateTagCommand($this->tagService->reveal(), Translator::factory([]));
33
        $app = new Application();
34
        $app->add($command);
35
36
        $this->commandTester = new CommandTester($command);
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function errorIsReturnedWhenNoTagsAreProvided()
43
    {
44
        $this->commandTester->execute([]);
45
46
        $output = $this->commandTester->getDisplay();
47
        $this->assertContains('You have to provide at least one tag name', $output);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function serviceIsInvokedOnSuccess()
54
    {
55
        $tagNames = ['foo', 'bar'];
56
        /** @var MethodProphecy $createTags */
57
        $createTags = $this->tagService->createTags($tagNames)->willReturn([]);
58
59
        $this->commandTester->execute([
60
            '--name' => $tagNames,
61
        ]);
62
        $output = $this->commandTester->getDisplay();
63
64
        $this->assertContains(sprintf('Created tags: ["%s"]', implode('", "', $tagNames)), $output);
65
        $createTags->shouldHaveBeenCalled();
66
    }
67
}
68