Completed
Push — master ( 1de050...162d05 )
by Alejandro
46:23 queued 09:21
created

ProcessVisitsCommandTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 124
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 16
loc 124
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 16 16 1
A allReturnedVisitsIpsAreProcessed() 0 22 1
A localhostAndEmptyAddressIsIgnored() 0 25 1
A sleepsEveryTimeTheApiLimitIsReached() 0 33 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Service\IpApiLocationResolver;
11
use Shlinkio\Shlink\Core\Entity\Visit;
12
use Shlinkio\Shlink\Core\Service\VisitService;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Tester\CommandTester;
16
use Zend\I18n\Translator\Translator;
17
use function count;
18
use function round;
19
20
class ProcessVisitsCommandTest extends TestCase
21
{
22
    /**
23
     * @var CommandTester
24
     */
25
    protected $commandTester;
26
    /**
27
     * @var ObjectProphecy
28
     */
29
    protected $visitService;
30
    /**
31
     * @var ObjectProphecy
32
     */
33
    protected $ipResolver;
34
35 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $this->visitService = $this->prophesize(VisitService::class);
38
        $this->ipResolver = $this->prophesize(IpApiLocationResolver::class);
39
        $this->ipResolver->getApiLimit()->willReturn(10000000000);
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
        $visits = [
58
            (new Visit())->setRemoteAddr('1.2.3.4'),
59
            (new Visit())->setRemoteAddr('4.3.2.1'),
60
            (new Visit())->setRemoteAddr('12.34.56.78'),
61
        ];
62
        $this->visitService->getUnlocatedVisits()->willReturn($visits)
63
                                                 ->shouldBeCalledTimes(1);
64
65
        $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits));
66
        $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
67
                                                             ->shouldBeCalledTimes(count($visits));
68
69
        $this->commandTester->execute([
70
            'command' => 'visit:process',
71
        ]);
72
        $output = $this->commandTester->getDisplay();
73
        $this->assertContains('Processing IP 1.2.3.0', $output);
74
        $this->assertContains('Processing IP 4.3.2.0', $output);
75
        $this->assertContains('Processing IP 12.34.56.0', $output);
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function localhostAndEmptyAddressIsIgnored()
82
    {
83
        $visits = [
84
            (new Visit())->setRemoteAddr('1.2.3.4'),
85
            (new Visit())->setRemoteAddr('4.3.2.1'),
86
            (new Visit())->setRemoteAddr('12.34.56.78'),
87
            (new Visit())->setRemoteAddr('127.0.0.1'),
88
            (new Visit())->setRemoteAddr('127.0.0.1'),
89
            (new Visit())->setRemoteAddr(''),
90
            (new Visit())->setRemoteAddr(null),
91
        ];
92
        $this->visitService->getUnlocatedVisits()->willReturn($visits)
93
            ->shouldBeCalledTimes(1);
94
95
        $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 4);
96
        $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
97
                                                             ->shouldBeCalledTimes(count($visits) - 4);
98
99
        $this->commandTester->execute([
100
            'command' => 'visit:process',
101
        ], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
102
        $output = $this->commandTester->getDisplay();
103
        $this->assertContains('Ignored localhost address', $output);
104
        $this->assertContains('Ignored visit with no IP address', $output);
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function sleepsEveryTimeTheApiLimitIsReached()
111
    {
112
        $visits = [
113
            (new Visit())->setRemoteAddr('1.2.3.4'),
114
            (new Visit())->setRemoteAddr('4.3.2.1'),
115
            (new Visit())->setRemoteAddr('12.34.56.78'),
116
            (new Visit())->setRemoteAddr('1.2.3.4'),
117
            (new Visit())->setRemoteAddr('4.3.2.1'),
118
            (new Visit())->setRemoteAddr('12.34.56.78'),
119
            (new Visit())->setRemoteAddr('1.2.3.4'),
120
            (new Visit())->setRemoteAddr('4.3.2.1'),
121
            (new Visit())->setRemoteAddr('12.34.56.78'),
122
            (new Visit())->setRemoteAddr('4.3.2.1'),
123
        ];
124
        $apiLimit = 3;
125
126
        $this->visitService->getUnlocatedVisits()->willReturn($visits);
127
        $this->visitService->saveVisit(Argument::any())->will(function () {
128
        });
129
130
        $getApiLimit = $this->ipResolver->getApiLimit()->willReturn($apiLimit);
131
        $getApiInterval = $this->ipResolver->getApiInterval()->willReturn(0);
132
        $resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
133
            ->shouldBeCalledTimes(count($visits));
134
135
        $this->commandTester->execute([
136
            'command' => 'visit:process',
137
        ]);
138
139
        $getApiLimit->shouldHaveBeenCalledTimes(count($visits));
140
        $getApiInterval->shouldHaveBeenCalledTimes(round(count($visits) / $apiLimit));
141
        $resolveIpLocation->shouldHaveBeenCalledTimes(count($visits));
142
    }
143
}
144