2016-07-31 17:10:16 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-31 17:10:16 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action;
|
|
|
|
|
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\Service\VisitsTracker;
|
|
|
|
use Shlinkio\Shlink\Rest\Action\GetVisitsAction;
|
2017-03-25 12:04:48 +03:00
|
|
|
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
2016-07-31 17:10:16 +03: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([])
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
|
2017-03-25 12:04:48 +03:00
|
|
|
$response = $this->action->process(
|
2016-07-31 17:10:16 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 12:04:48 +03:00
|
|
|
TestUtils::createDelegateMock()->reveal()
|
2016-07-31 17:10:16 +03:00
|
|
|
);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function providingInvalidShortCodeReturnsError()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow(
|
|
|
|
InvalidArgumentException::class
|
|
|
|
)->shouldBeCalledTimes(1);
|
|
|
|
|
2017-03-25 12:04:48 +03:00
|
|
|
$response = $this->action->process(
|
2016-07-31 17:10:16 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 12:04:48 +03:00
|
|
|
TestUtils::createDelegateMock()->reveal()
|
2016-07-31 17:10:16 +03:00
|
|
|
);
|
2016-08-08 11:02:52 +03:00
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
2016-07-31 17:10:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function unexpectedExceptionWillReturnError()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willThrow(
|
|
|
|
\Exception::class
|
|
|
|
)->shouldBeCalledTimes(1);
|
|
|
|
|
2017-03-25 12:04:48 +03:00
|
|
|
$response = $this->action->process(
|
2016-07-31 17:10:16 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 12:04:48 +03:00
|
|
|
TestUtils::createDelegateMock()->reveal()
|
2016-07-31 17:10:16 +03:00
|
|
|
);
|
|
|
|
$this->assertEquals(500, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function datesAreReadFromQuery()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$this->visitsTracker->info($shortCode, new DateRange(null, new \DateTime('2016-01-01 00:00:00')))
|
|
|
|
->willReturn([])
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
|
2017-03-25 12:04:48 +03:00
|
|
|
$response = $this->action->process(
|
2016-07-31 17:10:16 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
|
|
|
|
->withQueryParams(['endDate' => '2016-01-01 00:00:00']),
|
2017-03-25 12:04:48 +03:00
|
|
|
TestUtils::createDelegateMock()->reveal()
|
2016-07-31 17:10:16 +03:00
|
|
|
);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|