2022-05-24 18:54:44 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Domain;
|
|
|
|
|
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2022-10-22 14:21:00 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-05-24 18:54:44 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Domain\GetDomainVisitsCommand;
|
|
|
|
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-05-24 18:54:44 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2022-05-24 18:54:44 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
|
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
class GetDomainVisitsCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
use CliTestUtilsTrait;
|
|
|
|
|
|
|
|
private CommandTester $commandTester;
|
2022-10-22 14:21:00 +03:00
|
|
|
private MockObject $visitsHelper;
|
|
|
|
private MockObject $stringifier;
|
2022-05-24 18:54:44 +03:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-22 14:21:00 +03:00
|
|
|
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
|
|
|
|
$this->stringifier = $this->createMock(ShortUrlStringifierInterface::class);
|
2022-05-24 18:54:44 +03:00
|
|
|
|
|
|
|
$this->commandTester = $this->testerForCommand(
|
2022-10-22 14:21:00 +03:00
|
|
|
new GetDomainVisitsCommand($this->visitsHelper, $this->stringifier),
|
2022-05-24 18:54:44 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function outputIsProperlyGenerated(): void
|
|
|
|
{
|
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
|
|
|
$visit = Visit::forValidShortUrl($shortUrl, new Visitor('bar', 'foo', '', ''))->locate(
|
|
|
|
VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')),
|
|
|
|
);
|
|
|
|
$domain = 'doma.in';
|
2022-10-22 14:21:00 +03:00
|
|
|
$this->visitsHelper->expects($this->once())->method('visitsForDomain')->with(
|
|
|
|
$this->equalTo($domain),
|
|
|
|
$this->anything(),
|
|
|
|
)->willReturn(new Paginator(new ArrayAdapter([$visit])));
|
|
|
|
$this->stringifier->expects($this->once())->method('stringify')->with($this->equalTo($shortUrl))->willReturn(
|
|
|
|
'the_short_url',
|
2022-05-24 18:54:44 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->commandTester->execute(['domain' => $domain]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
<<<OUTPUT
|
|
|
|
+---------+---------------------------+------------+---------+--------+---------------+
|
|
|
|
| Referer | Date | User agent | Country | City | Short Url |
|
|
|
|
+---------+---------------------------+------------+---------+--------+---------------+
|
|
|
|
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid | the_short_url |
|
|
|
|
+---------+---------------------------+------------+---------+--------+---------------+
|
|
|
|
|
|
|
|
OUTPUT,
|
|
|
|
$output,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|