2016-07-31 09:10:16 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-05-03 11:34:45 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
|
2016-07-31 09:10:16 -05:00
|
|
|
|
2018-09-29 05:52:32 -05:00
|
|
|
use Cake\Chronos\Chronos;
|
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\Util\DateRange;
|
2018-11-27 14:09:27 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams;
|
2016-07-31 09:10:16 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTracker;
|
2018-05-03 11:04:00 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Action\Visit\GetVisitsAction;
|
2018-12-25 16:01:30 -06:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2018-11-28 13:39:08 -06:00
|
|
|
use Zend\Paginator\Adapter\ArrayAdapter;
|
|
|
|
use Zend\Paginator\Paginator;
|
2016-07-31 09:10:16 -05:00
|
|
|
|
|
|
|
class GetVisitsActionTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var GetVisitsAction */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $action;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $visitsTracker;
|
2016-07-31 09:10:16 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-07-31 09:10:16 -05:00
|
|
|
{
|
|
|
|
$this->visitsTracker = $this->prophesize(VisitsTracker::class);
|
2018-11-18 09:28:04 -06:00
|
|
|
$this->action = new GetVisitsAction($this->visitsTracker->reveal());
|
2016-07-31 09:10:16 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-11-20 13:58:16 -06:00
|
|
|
public function providingCorrectShortCodeReturnsVisits(): void
|
2016-07-31 09:10:16 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2018-11-28 13:39:08 -06:00
|
|
|
$this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willReturn(
|
|
|
|
new Paginator(new ArrayAdapter([]))
|
|
|
|
)->shouldBeCalledOnce();
|
2016-07-31 09:10:16 -05:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode));
|
2016-07-31 09:10:16 -05:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-11-20 13:58:16 -06:00
|
|
|
public function paramsAreReadFromQuery(): void
|
2016-07-31 09:10:16 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2018-11-27 14:09:27 -06:00
|
|
|
$this->visitsTracker->info($shortCode, new VisitsParams(
|
2018-11-29 01:02:22 -06:00
|
|
|
new DateRange(null, Chronos::parse('2016-01-01 00:00:00')),
|
|
|
|
3,
|
|
|
|
10
|
2018-11-27 14:09:27 -06:00
|
|
|
))
|
2018-11-28 13:39:08 -06:00
|
|
|
->willReturn(new Paginator(new ArrayAdapter([])))
|
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(
|
2018-12-25 16:01:30 -06:00
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode)
|
|
|
|
->withQueryParams([
|
|
|
|
'endDate' => '2016-01-01 00:00:00',
|
|
|
|
'page' => '3',
|
|
|
|
'itemsPerPage' => '10',
|
|
|
|
])
|
2016-07-31 09:10:16 -05:00
|
|
|
);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|