Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

DisableKeyCommandTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 42 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 21
loc 50
rs 10
wmc 3
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 1
A providedApiKeyIsDisabled() 0 9 1
A errorIsReturnedIfServiceThrowsException() 13 13 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\Api;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
use Prophecy\Prophecy\ObjectProphecy;
6
use Shlinkio\Shlink\CLI\Command\Api\DisableKeyCommand;
7
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
8
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
9
use Symfony\Component\Console\Application;
10
use Symfony\Component\Console\Tester\CommandTester;
11
use Zend\I18n\Translator\Translator;
12
13
class DisableKeyCommandTest extends TestCase
14
{
15
    /**
16
     * @var CommandTester
17
     */
18
    protected $commandTester;
19
    /**
20
     * @var ObjectProphecy
21
     */
22
    protected $apiKeyService;
23
24 View Code Duplication
    public function setUp()
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...
25
    {
26
        $this->apiKeyService = $this->prophesize(ApiKeyService::class);
27
        $command = new DisableKeyCommand($this->apiKeyService->reveal(), Translator::factory([]));
28
        $app = new Application();
29
        $app->add($command);
30
        $this->commandTester = new CommandTester($command);
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function providedApiKeyIsDisabled()
37
    {
38
        $apiKey = 'abcd1234';
39
        $this->apiKeyService->disable($apiKey)->shouldBeCalledTimes(1);
40
        $this->commandTester->execute([
41
            'command' => 'api-key:disable',
42
            'apiKey' => $apiKey,
43
        ]);
44
    }
45
46
    /**
47
     * @test
48
     */
49 View Code Duplication
    public function errorIsReturnedIfServiceThrowsException()
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...
50
    {
51
        $apiKey = 'abcd1234';
52
        $this->apiKeyService->disable($apiKey)->willThrow(InvalidArgumentException::class)
53
                                              ->shouldBeCalledTimes(1);
54
55
        $this->commandTester->execute([
56
            'command' => 'api-key:disable',
57
            'apiKey' => $apiKey,
58
        ]);
59
        $output = $this->commandTester->getDisplay();
60
        $this->assertEquals('API key "abcd1234" does not exist.' . PHP_EOL, $output);
61
    }
62
}
63