mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 13:03:52 +03:00
Migrated ShortUrlVisitsActionTest to use PHPUnit mocks
This commit is contained in:
parent
d4684fd01f
commit
d414496a3c
1 changed files with 17 additions and 20 deletions
|
@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Action\Visit;
|
||||||
use Cake\Chronos\Chronos;
|
use Cake\Chronos\Chronos;
|
||||||
use Laminas\Diactoros\ServerRequestFactory;
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
use Pagerfanta\Adapter\ArrayAdapter;
|
use Pagerfanta\Adapter\ArrayAdapter;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
||||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||||
|
@ -22,27 +20,24 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||||
|
|
||||||
class ShortUrlVisitsActionTest extends TestCase
|
class ShortUrlVisitsActionTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
|
||||||
|
|
||||||
private ShortUrlVisitsAction $action;
|
private ShortUrlVisitsAction $action;
|
||||||
private ObjectProphecy $visitsHelper;
|
private MockObject $visitsHelper;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class);
|
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
|
||||||
$this->action = new ShortUrlVisitsAction($this->visitsHelper->reveal());
|
$this->action = new ShortUrlVisitsAction($this->visitsHelper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function providingCorrectShortCodeReturnsVisits(): void
|
public function providingCorrectShortCodeReturnsVisits(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->visitsHelper->visitsForShortUrl(
|
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
|
||||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
||||||
Argument::type(VisitsParams::class),
|
$this->isInstanceOf(VisitsParams::class),
|
||||||
Argument::type(ApiKey::class),
|
$this->isInstanceOf(ApiKey::class),
|
||||||
)->willReturn(new Paginator(new ArrayAdapter([])))
|
)->willReturn(new Paginator(new ArrayAdapter([])));
|
||||||
->shouldBeCalledOnce();
|
|
||||||
|
|
||||||
$response = $this->action->handle($this->requestWithApiKey()->withAttribute('shortCode', $shortCode));
|
$response = $this->action->handle($this->requestWithApiKey()->withAttribute('shortCode', $shortCode));
|
||||||
self::assertEquals(200, $response->getStatusCode());
|
self::assertEquals(200, $response->getStatusCode());
|
||||||
|
@ -52,13 +47,15 @@ class ShortUrlVisitsActionTest extends TestCase
|
||||||
public function paramsAreReadFromQuery(): void
|
public function paramsAreReadFromQuery(): void
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->visitsHelper->visitsForShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), new VisitsParams(
|
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
|
||||||
DateRange::until(Chronos::parse('2016-01-01 00:00:00')),
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
||||||
3,
|
new VisitsParams(
|
||||||
10,
|
DateRange::until(Chronos::parse('2016-01-01 00:00:00')),
|
||||||
), Argument::type(ApiKey::class))
|
3,
|
||||||
->willReturn(new Paginator(new ArrayAdapter([])))
|
10,
|
||||||
->shouldBeCalledOnce();
|
),
|
||||||
|
$this->isInstanceOf(ApiKey::class),
|
||||||
|
)->willReturn(new Paginator(new ArrayAdapter([])));
|
||||||
|
|
||||||
$response = $this->action->handle(
|
$response = $this->action->handle(
|
||||||
$this->requestWithApiKey()->withAttribute('shortCode', $shortCode)
|
$this->requestWithApiKey()->withAttribute('shortCode', $shortCode)
|
||||||
|
|
Loading…
Add table
Reference in a new issue