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

93 lines
2.9 KiB
PHP
Raw Normal View History

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