1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\Visit; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use Laminas\Diactoros\ServerRequest; |
9
|
|
|
use Laminas\Paginator\Adapter\ArrayAdapter; |
10
|
|
|
use Laminas\Paginator\Paginator; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Prophecy\Argument; |
13
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
14
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange; |
15
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; |
16
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams; |
17
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTracker; |
18
|
|
|
use Shlinkio\Shlink\Rest\Action\Visit\ShortUrlVisitsAction; |
19
|
|
|
|
20
|
|
|
class ShortUrlVisitsActionTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
private ShortUrlVisitsAction $action; |
23
|
|
|
private ObjectProphecy $visitsTracker; |
24
|
|
|
|
25
|
|
|
public function setUp(): void |
26
|
|
|
{ |
27
|
|
|
$this->visitsTracker = $this->prophesize(VisitsTracker::class); |
28
|
|
|
$this->action = new ShortUrlVisitsAction($this->visitsTracker->reveal()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @test */ |
32
|
|
|
public function providingCorrectShortCodeReturnsVisits(): void |
33
|
|
|
{ |
34
|
|
|
$shortCode = 'abc123'; |
35
|
|
|
$this->visitsTracker->info(new ShortUrlIdentifier($shortCode), Argument::type(VisitsParams::class))->willReturn( |
36
|
|
|
new Paginator(new ArrayAdapter([])), |
37
|
|
|
)->shouldBeCalledOnce(); |
38
|
|
|
|
39
|
|
|
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode)); |
40
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @test */ |
44
|
|
|
public function paramsAreReadFromQuery(): void |
45
|
|
|
{ |
46
|
|
|
$shortCode = 'abc123'; |
47
|
|
|
$this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams( |
48
|
|
|
new DateRange(null, Chronos::parse('2016-01-01 00:00:00')), |
49
|
|
|
3, |
50
|
|
|
10, |
51
|
|
|
)) |
52
|
|
|
->willReturn(new Paginator(new ArrayAdapter([]))) |
53
|
|
|
->shouldBeCalledOnce(); |
54
|
|
|
|
55
|
|
|
$response = $this->action->handle( |
56
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode) |
57
|
|
|
->withQueryParams([ |
58
|
|
|
'endDate' => '2016-01-01 00:00:00', |
59
|
|
|
'page' => '3', |
60
|
|
|
'itemsPerPage' => '10', |
61
|
|
|
]), |
62
|
|
|
); |
63
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|