Passed
Push — master ( 05e56c...b87687 )
by Alejandro
04:13
created

GetVisitsActionTest::datesAreReadFromQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
5
6
use Cake\Chronos\Chronos;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Argument;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
11
use Shlinkio\Shlink\Common\Util\DateRange;
12
use Shlinkio\Shlink\Core\Model\VisitsParams;
13
use Shlinkio\Shlink\Core\Service\VisitsTracker;
14
use Shlinkio\Shlink\Rest\Action\Visit\GetVisitsAction;
15
use Zend\Diactoros\ServerRequestFactory;
16
use Zend\Paginator\Adapter\ArrayAdapter;
17
use Zend\Paginator\Paginator;
18
19
class GetVisitsActionTest extends TestCase
20
{
21
    /** @var GetVisitsAction */
22
    private $action;
23
    /** @var ObjectProphecy */
24
    private $visitsTracker;
25
26
    public function setUp()
27
    {
28
        $this->visitsTracker = $this->prophesize(VisitsTracker::class);
29
        $this->action = new GetVisitsAction($this->visitsTracker->reveal());
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function providingCorrectShortCodeReturnsVisits()
36
    {
37
        $shortCode = 'abc123';
38
        $this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willReturn(
39
            new Paginator(new ArrayAdapter([]))
40
        )->shouldBeCalledOnce();
41
42
        $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
43
        $this->assertEquals(200, $response->getStatusCode());
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function providingInvalidShortCodeReturnsError()
50
    {
51
        $shortCode = 'abc123';
52
        $this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willThrow(
53
            InvalidArgumentException::class
54
        )->shouldBeCalledOnce();
55
56
        $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
57
        $this->assertEquals(404, $response->getStatusCode());
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function paramsAreReadFromQuery()
64
    {
65
        $shortCode = 'abc123';
66
        $this->visitsTracker->info($shortCode, new VisitsParams(
67
            new DateRange(null, Chronos::parse('2016-01-01 00:00:00')),
68
            3,
69
            10
70
        ))
71
            ->willReturn(new Paginator(new ArrayAdapter([])))
72
            ->shouldBeCalledOnce();
73
74
        $response = $this->action->handle(
75
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
76
                                               ->withQueryParams([
77
                                                   'endDate' => '2016-01-01 00:00:00',
78
                                                   'page' => '3',
79
                                                   'itemsPerPage' => '10',
80
                                               ])
81
        );
82
        $this->assertEquals(200, $response->getStatusCode());
83
    }
84
}
85