Passed
Pull Request — master (#258)
by Alejandro
04:00
created

ProcessVisitsCommandTest::sleepsEveryTimeTheApiLimitIsReached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 34
rs 9.536
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\CLI\Command\Visit\ProcessVisitsCommand;
10
use Shlinkio\Shlink\Common\IpGeolocation\IpApiLocationResolver;
11
use Shlinkio\Shlink\Core\Entity\ShortUrl;
12
use Shlinkio\Shlink\Core\Entity\Visit;
13
use Shlinkio\Shlink\Core\Model\Visitor;
14
use Shlinkio\Shlink\Core\Service\VisitService;
15
use Symfony\Component\Console\Application;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Tester\CommandTester;
18
use Zend\I18n\Translator\Translator;
19
use function count;
20
21
class ProcessVisitsCommandTest extends TestCase
22
{
23
    /**
24
     * @var CommandTester
25
     */
26
    private $commandTester;
27
    /**
28
     * @var ObjectProphecy
29
     */
30
    private $visitService;
31
    /**
32
     * @var ObjectProphecy
33
     */
34
    private $ipResolver;
35
36
    public function setUp()
37
    {
38
        $this->visitService = $this->prophesize(VisitService::class);
39
        $this->ipResolver = $this->prophesize(IpApiLocationResolver::class);
40
41
        $command = new ProcessVisitsCommand(
42
            $this->visitService->reveal(),
43
            $this->ipResolver->reveal(),
44
            Translator::factory([])
45
        );
46
        $app = new Application();
47
        $app->add($command);
48
49
        $this->commandTester = new CommandTester($command);
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function allReturnedVisitsIpsAreProcessed()
56
    {
57
        $shortUrl = new ShortUrl('');
58
59
        $visits = [
60
            new Visit($shortUrl, new Visitor('', '', '1.2.3.4')),
61
            new Visit($shortUrl, new Visitor('', '', '4.3.2.1')),
62
            new Visit($shortUrl, new Visitor('', '', '12.34.56.78')),
63
        ];
64
        $this->visitService->getUnlocatedVisits()->willReturn($visits)
65
                                                 ->shouldBeCalledOnce();
66
67
        $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits));
68
        $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
69
                                                             ->shouldBeCalledTimes(count($visits));
70
71
        $this->commandTester->execute([
72
            'command' => 'visit:process',
73
        ]);
74
        $output = $this->commandTester->getDisplay();
75
        $this->assertContains('Processing IP 1.2.3.0', $output);
76
        $this->assertContains('Processing IP 4.3.2.0', $output);
77
        $this->assertContains('Processing IP 12.34.56.0', $output);
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function localhostAndEmptyAddressIsIgnored()
84
    {
85
        $shortUrl = new ShortUrl('');
86
87
        $visits = [
88
            new Visit($shortUrl, new Visitor('', '', '1.2.3.4')),
89
            new Visit($shortUrl, new Visitor('', '', '4.3.2.1')),
90
            new Visit($shortUrl, new Visitor('', '', '12.34.56.78')),
91
            new Visit($shortUrl, new Visitor('', '', '127.0.0.1')),
92
            new Visit($shortUrl, new Visitor('', '', '127.0.0.1')),
93
            new Visit($shortUrl, new Visitor('', '', '')),
94
            new Visit($shortUrl, new Visitor('', '', null)),
95
        ];
96
        $this->visitService->getUnlocatedVisits()->willReturn($visits)
97
            ->shouldBeCalledOnce();
98
99
        $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 4);
100
        $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
101
                                                             ->shouldBeCalledTimes(count($visits) - 4);
102
103
        $this->commandTester->execute([
104
            'command' => 'visit:process',
105
        ], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
106
        $output = $this->commandTester->getDisplay();
107
        $this->assertContains('Ignored localhost address', $output);
108
        $this->assertContains('Ignored visit with no IP address', $output);
109
    }
110
}
111