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

GetVisitsActionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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