2021-02-09 16:41:51 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\Visit;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 15:25:54 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-02-09 16:41:51 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
|
|
|
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
|
2021-02-09 16:41:51 -06:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Action\Visit\OrphanVisitsAction;
|
|
|
|
|
|
|
|
use function count;
|
|
|
|
|
|
|
|
class OrphanVisitsActionTest extends TestCase
|
|
|
|
{
|
|
|
|
private OrphanVisitsAction $action;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & VisitsStatsHelperInterface $visitsHelper;
|
|
|
|
private MockObject & DataTransformerInterface $orphanVisitTransformer;
|
2021-02-09 16:41:51 -06:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-23 15:25:54 -05:00
|
|
|
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
|
|
|
|
$this->orphanVisitTransformer = $this->createMock(DataTransformerInterface::class);
|
2021-02-09 16:41:51 -06:00
|
|
|
|
2022-10-23 15:25:54 -05:00
|
|
|
$this->action = new OrphanVisitsAction($this->visitsHelper, $this->orphanVisitTransformer);
|
2021-02-09 16:41:51 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-02-09 16:41:51 -06:00
|
|
|
public function requestIsHandled(): void
|
|
|
|
{
|
|
|
|
$visitor = Visitor::emptyInstance();
|
|
|
|
$visits = [Visit::forInvalidShortUrl($visitor), Visit::forRegularNotFound($visitor)];
|
2022-10-23 15:25:54 -05:00
|
|
|
$this->visitsHelper->expects($this->once())->method('orphanVisits')->with(
|
|
|
|
$this->isInstanceOf(VisitsParams::class),
|
|
|
|
)->willReturn(new Paginator(new ArrayAdapter($visits)));
|
2021-12-10 07:00:55 -06:00
|
|
|
$visitsAmount = count($visits);
|
2022-10-23 15:25:54 -05:00
|
|
|
$this->orphanVisitTransformer->expects($this->exactly($visitsAmount))->method('transform')->with(
|
|
|
|
$this->isInstanceOf(Visit::class),
|
|
|
|
)->willReturn([]);
|
2021-02-09 16:41:51 -06:00
|
|
|
|
2021-12-10 07:00:55 -06:00
|
|
|
/** @var JsonResponse $response */
|
2021-02-09 16:41:51 -06:00
|
|
|
$response = $this->action->handle(ServerRequestFactory::fromGlobals());
|
2021-12-10 07:00:55 -06:00
|
|
|
$payload = $response->getPayload();
|
2021-02-09 16:41:51 -06:00
|
|
|
|
2021-12-10 07:00:55 -06:00
|
|
|
self::assertCount($visitsAmount, $payload['visits']['data']);
|
2021-02-09 16:41:51 -06:00
|
|
|
self::assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|