GetVisitsCommandTest::outputIsProperlyGenerated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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