Files

70 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2022-05-24 17:59:06 +02:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
2022-05-24 17:59:06 +02:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Visit\GetOrphanVisitsCommand;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams;
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitType;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
2022-05-24 17:59:06 +02:00
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
2026-11-02 14:00:46 +01:00
use Shlinkio\Shlink\Geolocation\Model\Location;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
2022-05-24 17:59:06 +02:00
use Symfony\Component\Console\Tester\CommandTester;
class GetOrphanVisitsCommandTest extends TestCase
{
private CommandTester $commandTester;
private MockObject&VisitsStatsHelperInterface $visitsHelper;
2022-05-24 17:59:06 +02:00
protected function setUp(): void
{
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$this->commandTester = CliTestUtils::testerForCommand(new GetOrphanVisitsCommand($this->visitsHelper));
2022-05-24 17:59:06 +02:00
}
#[Test]
#[TestWith([[], false])]
#[TestWith([['--type' => OrphanVisitType::BASE_URL->value], true])]
public function outputIsProperlyGenerated(array $args, bool $includesType): void
2022-05-24 17:59:06 +02:00
{
$visit = Visit::forBasePath(Visitor::fromParams('bar', 'foo', ''))->locate(
VisitLocation::fromLocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')),
2022-05-24 17:59:06 +02:00
);
$this->visitsHelper
->expects($this->once())
->method('orphanVisits')
->with($this->callback(
static fn (OrphanVisitsParams $param) => (
($includesType && $param->type !== null)
|| (!$includesType && $param->type === null)
),
))
->willReturn(new Paginator(new ArrayAdapter([$visit])));
2022-05-24 17:59:06 +02:00
$this->commandTester->execute($args);
2022-05-24 17:59:06 +02:00
$output = $this->commandTester->getDisplay();
$type = OrphanVisitType::BASE_URL->value;
2022-05-24 17:59:06 +02:00
self::assertEquals(
<<<OUTPUT
+---------------------------+---------------+------------+---------+---------+--------+--------+-------------+--------------+----------+
| Date | Potential bot | User agent | Referer | Country | Region | City | Visited URL | Redirect URL | Type |
+---------------------------+---------------+------------+---------+---------+--------+--------+-------------+--------------+----------+
| {$visit->date->toAtomString()} | | bar | foo | Spain | | Madrid | | Unknown | {$type} |
+---------------------------+---------------+------------+--- Page 1 of 1 ---+--------+--------+-------------+--------------+----------+
2022-05-24 17:59:06 +02:00
OUTPUT,
2022-05-24 17:59:06 +02:00
$output,
);
}
}