2022-05-24 17:59:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
|
|
|
|
|
|
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-02-10 18:19:28 +01:00
|
|
|
use PHPUnit\Framework\Attributes\TestWith;
|
2022-10-22 14:06:54 +02:00
|
|
|
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;
|
2022-09-23 19:03:32 +02:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
2024-02-10 18:19:28 +01:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitsParams;
|
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\OrphanVisitType;
|
2022-09-23 18:05:17 +02:00
|
|
|
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;
|
2023-06-18 10:51:59 +02:00
|
|
|
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;
|
2026-05-22 08:42:47 +02:00
|
|
|
private MockObject&VisitsStatsHelperInterface $visitsHelper;
|
2022-05-24 17:59:06 +02:00
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
2022-10-22 14:06:54 +02:00
|
|
|
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
|
2023-06-18 10:51:59 +02:00
|
|
|
$this->commandTester = CliTestUtils::testerForCommand(new GetOrphanVisitsCommand($this->visitsHelper));
|
2022-05-24 17:59:06 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2024-02-10 18:19:28 +01:00
|
|
|
#[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
|
|
|
{
|
2024-11-14 14:48:18 +01:00
|
|
|
$visit = Visit::forBasePath(Visitor::fromParams('bar', 'foo', ''))->locate(
|
2025-11-17 12:12:06 +01:00
|
|
|
VisitLocation::fromLocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')),
|
2022-05-24 17:59:06 +02:00
|
|
|
);
|
2026-05-22 08:42:47 +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
|
|
|
|
2024-02-10 18:19:28 +01:00
|
|
|
$this->commandTester->execute($args);
|
2022-05-24 17:59:06 +02:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2026-01-03 11:45:29 +01:00
|
|
|
$type = OrphanVisitType::BASE_URL->value;
|
2022-05-24 17:59:06 +02:00
|
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
|
<<<OUTPUT
|
2026-05-22 08:42:47 +02:00
|
|
|
+---------------------------+---------------+------------+---------+---------+--------+--------+-------------+--------------+----------+
|
|
|
|
|
| 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
|
|
|
|
2026-05-22 08:42:47 +02:00
|
|
|
OUTPUT,
|
2022-05-24 17:59:06 +02:00
|
|
|
$output,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|