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

UpdateTagActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 37.33 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 28
loc 75
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A whenInvalidParamsAreProvidedAnErrorIsReturned() 0 7 1
A provideParams() 0 8 1
A requestingInvalidTagReturnsError() 14 14 1
A correctInvocationRenamesTag() 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\Rest\Action\Tag;
3
4
use Interop\Http\ServerMiddleware\DelegateInterface;
5
use PHPUnit\Framework\TestCase;
6
use Prophecy\Prophecy\MethodProphecy;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\Core\Entity\Tag;
9
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
10
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
11
use Shlinkio\Shlink\Rest\Action\Tag\UpdateTagAction;
12
use Zend\Diactoros\ServerRequestFactory;
13
use Zend\I18n\Translator\Translator;
14
15
class UpdateTagActionTest extends TestCase
16
{
17
    /**
18
     * @var UpdateTagAction
19
     */
20
    private $action;
21
    /**
22
     * @var ObjectProphecy
23
     */
24
    private $tagService;
25
26
    public function setUp()
27
    {
28
        $this->tagService = $this->prophesize(TagServiceInterface::class);
29
        $this->action = new UpdateTagAction($this->tagService->reveal(), Translator::factory([]));
30
    }
31
32
    /**
33
     * @test
34
     * @dataProvider provideParams
35
     * @param array $bodyParams
36
     */
37
    public function whenInvalidParamsAreProvidedAnErrorIsReturned(array $bodyParams)
38
    {
39
        $request = ServerRequestFactory::fromGlobals()->withParsedBody($bodyParams);
40
        $resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal());
41
42
        $this->assertEquals(400, $resp->getStatusCode());
43
    }
44
45
    public function provideParams()
46
    {
47
        return [
48
            [['oldName' => 'foo']],
49
            [['newName' => 'foo']],
50
            [[]],
51
        ];
52
    }
53
54
    /**
55
     * @test
56
     */
57 View Code Duplication
    public function requestingInvalidTagReturnsError()
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...
58
    {
59
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
60
            'oldName' => 'foo',
61
            'newName' => 'bar',
62
        ]);
63
        /** @var MethodProphecy $rename */
64
        $rename = $this->tagService->renameTag('foo', 'bar')->willThrow(EntityDoesNotExistException::class);
65
66
        $resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal());
67
68
        $this->assertEquals(404, $resp->getStatusCode());
69
        $rename->shouldHaveBeenCalled();
70
    }
71
72
    /**
73
     * @test
74
     */
75 View Code Duplication
    public function correctInvocationRenamesTag()
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...
76
    {
77
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
78
            'oldName' => 'foo',
79
            'newName' => 'bar',
80
        ]);
81
        /** @var MethodProphecy $rename */
82
        $rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag());
83
84
        $resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal());
85
86
        $this->assertEquals(204, $resp->getStatusCode());
87
        $rename->shouldHaveBeenCalled();
88
    }
89
}
90