GetVisitsCommandTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
  4. use Cake\Chronos\Chronos;
  5. use Laminas\Paginator\Adapter\ArrayAdapter;
  6. use Laminas\Paginator\Paginator;
  7. use PHPUnit\Framework\TestCase;
  8. use Prophecy\Argument;
  9. use Prophecy\Prophecy\ObjectProphecy;
  10. use Shlinkio\Shlink\CLI\Command\ShortUrl\GetVisitsCommand;
  11. use Shlinkio\Shlink\Common\Util\DateRange;
  12. use Shlinkio\Shlink\Core\Entity\ShortUrl;
  13. use Shlinkio\Shlink\Core\Entity\Visit;
  14. use Shlinkio\Shlink\Core\Entity\VisitLocation;
  15. use Shlinkio\Shlink\Core\Model\Visitor;
  16. use Shlinkio\Shlink\Core\Model\VisitsParams;
  17. use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
  18. use Shlinkio\Shlink\IpGeolocation\Model\Location;
  19. use Symfony\Component\Console\Application;
  20. use Symfony\Component\Console\Tester\CommandTester;
  21. use function sprintf;
  22. class GetVisitsCommandTest extends TestCase
  23. {
  24. private CommandTester $commandTester;
  25. private ObjectProphecy $visitsTracker;
  26. public function setUp(): void
  27. {
  28. $this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
  29. $command = new GetVisitsCommand($this->visitsTracker->reveal());
  30. $app = new Application();
  31. $app->add($command);
  32. $this->commandTester = new CommandTester($command);
  33. }
  34. /** @test */
  35. public function noDateFlagsTriesToListWithoutDateRange(): void
  36. {
  37. $shortCode = 'abc123';
  38. $this->visitsTracker->info($shortCode, new VisitsParams(new DateRange(null, null)))->willReturn(
  39. new Paginator(new ArrayAdapter([])),
  40. )->shouldBeCalledOnce();
  41. $this->commandTester->execute(['shortCode' => $shortCode]);
  42. }
  43. /** @test */
  44. public function providingDateFlagsTheListGetsFiltered(): void
  45. {
  46. $shortCode = 'abc123';
  47. $startDate = '2016-01-01';
  48. $endDate = '2016-02-01';
  49. $this->visitsTracker->info(
  50. $shortCode,
  51. new VisitsParams(new DateRange(Chronos::parse($startDate), Chronos::parse($endDate))),
  52. )
  53. ->willReturn(new Paginator(new ArrayAdapter([])))
  54. ->shouldBeCalledOnce();
  55. $this->commandTester->execute([
  56. 'shortCode' => $shortCode,
  57. '--startDate' => $startDate,
  58. '--endDate' => $endDate,
  59. ]);
  60. }
  61. /** @test */
  62. public function providingInvalidDatesPrintsWarning(): void
  63. {
  64. $shortCode = 'abc123';
  65. $startDate = 'foo';
  66. $info = $this->visitsTracker->info($shortCode, new VisitsParams(new DateRange()))
  67. ->willReturn(new Paginator(new ArrayAdapter([])));
  68. $this->commandTester->execute([
  69. 'shortCode' => $shortCode,
  70. '--startDate' => $startDate,
  71. ]);
  72. $output = $this->commandTester->getDisplay();
  73. $info->shouldHaveBeenCalledOnce();
  74. $this->assertStringContainsString(
  75. sprintf('Ignored provided "startDate" since its value "%s" is not a valid date', $startDate),
  76. $output,
  77. );
  78. }
  79. /** @test */
  80. public function outputIsProperlyGenerated(): void
  81. {
  82. $shortCode = 'abc123';
  83. $this->visitsTracker->info($shortCode, Argument::any())->willReturn(
  84. new Paginator(new ArrayAdapter([
  85. (new Visit(new ShortUrl(''), new Visitor('bar', 'foo', '')))->locate(
  86. new VisitLocation(new Location('', 'Spain', '', '', 0, 0, '')),
  87. ),
  88. ])),
  89. )->shouldBeCalledOnce();
  90. $this->commandTester->execute(['shortCode' => $shortCode]);
  91. $output = $this->commandTester->getDisplay();
  92. $this->assertStringContainsString('foo', $output);
  93. $this->assertStringContainsString('Spain', $output);
  94. $this->assertStringContainsString('bar', $output);
  95. }
  96. }