Completed
Pull Request — master (#148)
by Alejandro
04:16
created

GetVisitsActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A providingCorrectShortCodeReturnsVisits() 0 9 1
A providingInvalidShortCodeReturnsError() 10 10 1
A unexpectedExceptionWillReturnError() 10 10 1
A datesAreReadFromQuery() 0 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
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
10
use Shlinkio\Shlink\Common\Util\DateRange;
11
use Shlinkio\Shlink\Core\Service\VisitsTracker;
12
use Shlinkio\Shlink\Rest\Action\Visit\GetVisitsAction;
13
use Zend\Diactoros\ServerRequestFactory;
14
use Zend\I18n\Translator\Translator;
15
16
class GetVisitsActionTest extends TestCase
17
{
18
    /**
19
     * @var GetVisitsAction
20
     */
21
    protected $action;
22
    /**
23
     * @var ObjectProphecy
24
     */
25
    protected $visitsTracker;
26
27
    public function setUp()
28
    {
29
        $this->visitsTracker = $this->prophesize(VisitsTracker::class);
30
        $this->action = new GetVisitsAction($this->visitsTracker->reveal(), Translator::factory([]));
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function providingCorrectShortCodeReturnsVisits()
37
    {
38
        $shortCode = 'abc123';
39
        $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willReturn([])
40
                                                                                ->shouldBeCalledTimes(1);
41
42
        $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
43
        $this->assertEquals(200, $response->getStatusCode());
44
    }
45
46
    /**
47
     * @test
48
     */
49 View Code Duplication
    public function providingInvalidShortCodeReturnsError()
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
        $shortCode = 'abc123';
52
        $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow(
53
            InvalidArgumentException::class
54
        )->shouldBeCalledTimes(1);
55
56
        $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
57
        $this->assertEquals(404, $response->getStatusCode());
58
    }
59
60
    /**
61
     * @test
62
     */
63 View Code Duplication
    public function unexpectedExceptionWillReturnError()
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...
64
    {
65
        $shortCode = 'abc123';
66
        $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow(
67
            \Exception::class
68
        )->shouldBeCalledTimes(1);
69
70
        $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
71
        $this->assertEquals(500, $response->getStatusCode());
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function datesAreReadFromQuery()
78
    {
79
        $shortCode = 'abc123';
80
        $this->visitsTracker->info($shortCode, new DateRange(null, new \DateTime('2016-01-01 00:00:00')))
81
            ->willReturn([])
82
            ->shouldBeCalledTimes(1);
83
84
        $response = $this->action->handle(
85
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
86
                                               ->withQueryParams(['endDate' => '2016-01-01 00:00:00'])
87
        );
88
        $this->assertEquals(200, $response->getStatusCode());
89
    }
90
}
91