shlink/module/Rest/test/Action/Visit/GetVisitsActionTest.php

80 lines
2.6 KiB
PHP
Raw Normal View History

2016-07-31 17:10:16 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
2016-07-31 17:10:16 +03:00
use Cake\Chronos\Chronos;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-07-31 17:10:16 +03:00
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Model\VisitsParams;
2016-07-31 17:10:16 +03:00
use Shlinkio\Shlink\Core\Service\VisitsTracker;
2018-05-03 19:04:00 +03:00
use Shlinkio\Shlink\Rest\Action\Visit\GetVisitsAction;
use Zend\Diactoros\ServerRequest;
2018-11-28 22:39:08 +03:00
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
2016-07-31 17:10:16 +03:00
class GetVisitsActionTest extends TestCase
{
/** @var GetVisitsAction */
private $action;
/** @var ObjectProphecy */
private $visitsTracker;
2016-07-31 17:10:16 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2016-07-31 17:10:16 +03:00
{
$this->visitsTracker = $this->prophesize(VisitsTracker::class);
2018-11-18 18:28:04 +03:00
$this->action = new GetVisitsAction($this->visitsTracker->reveal());
2016-07-31 17:10:16 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
2016-07-31 17:10:16 +03:00
public function providingCorrectShortCodeReturnsVisits()
{
$shortCode = 'abc123';
2018-11-28 22:39:08 +03:00
$this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willReturn(
new Paginator(new ArrayAdapter([]))
)->shouldBeCalledOnce();
2016-07-31 17:10:16 +03:00
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode));
2016-07-31 17:10:16 +03:00
$this->assertEquals(200, $response->getStatusCode());
}
2019-02-17 22:28:34 +03:00
/** @test */
2016-07-31 17:10:16 +03:00
public function providingInvalidShortCodeReturnsError()
{
$shortCode = 'abc123';
$this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willThrow(
2016-07-31 17:10:16 +03:00
InvalidArgumentException::class
2018-11-11 15:18:21 +03:00
)->shouldBeCalledOnce();
2016-07-31 17:10:16 +03:00
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode));
$this->assertEquals(404, $response->getStatusCode());
2016-07-31 17:10:16 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function paramsAreReadFromQuery()
2016-07-31 17:10:16 +03:00
{
$shortCode = 'abc123';
$this->visitsTracker->info($shortCode, new VisitsParams(
new DateRange(null, Chronos::parse('2016-01-01 00:00:00')),
3,
10
))
2018-11-28 22:39:08 +03:00
->willReturn(new Paginator(new ArrayAdapter([])))
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
2016-07-31 17:10:16 +03:00
2018-03-26 20:02:41 +03:00
$response = $this->action->handle(
(new ServerRequest())->withAttribute('shortCode', $shortCode)
->withQueryParams([
'endDate' => '2016-01-01 00:00:00',
'page' => '3',
'itemsPerPage' => '10',
])
2016-07-31 17:10:16 +03:00
);
$this->assertEquals(200, $response->getStatusCode());
}
}