Completed
Push — master ( 03ee46...05e56c )
by Alejandro
24s queued 10s
created

GetVisitsActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A providingCorrectShortCodeReturnsVisits() 0 9 1
A datesAreReadFromQuery() 0 14 1
A providingInvalidShortCodeReturnsError() 0 9 1
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 datesAreReadFromQuery()
64
    {
65
        $shortCode = 'abc123';
66
        $this->visitsTracker->info($shortCode, new VisitsParams(
67
            new DateRange(null, Chronos::parse('2016-01-01 00:00:00'))
68
        ))
69
            ->willReturn(new Paginator(new ArrayAdapter([])))
70
            ->shouldBeCalledOnce();
71
72
        $response = $this->action->handle(
73
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
74
                                               ->withQueryParams(['endDate' => '2016-01-01 00:00:00'])
75
        );
76
        $this->assertEquals(200, $response->getStatusCode());
77
    }
78
}
79