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