Completed
Push — master ( 065cdd...96faaf )
by Alejandro
09:28
created

ProcessVisitsCommandTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 17.07 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 14
loc 82
rs 10
wmc 3
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 14 14 1
A allReturnedVisitsIpsAreProcessed() 0 22 1
A localhostAddressIsIgnored() 0 22 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
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
use Prophecy\Argument;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Shlinkio\Shlink\CLI\Command\Visit\ProcessVisitsCommand;
8
use Shlinkio\Shlink\Common\Service\IpLocationResolver;
9
use Shlinkio\Shlink\Core\Entity\Visit;
10
use Shlinkio\Shlink\Core\Service\VisitService;
11
use Symfony\Component\Console\Application;
12
use Symfony\Component\Console\Tester\CommandTester;
13
use Zend\I18n\Translator\Translator;
14
15
class ProcessVisitsCommandTest extends TestCase
16
{
17
    /**
18
     * @var CommandTester
19
     */
20
    protected $commandTester;
21
    /**
22
     * @var ObjectProphecy
23
     */
24
    protected $visitService;
25
    /**
26
     * @var ObjectProphecy
27
     */
28
    protected $ipResolver;
29
30 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...
31
    {
32
        $this->visitService = $this->prophesize(VisitService::class);
33
        $this->ipResolver = $this->prophesize(IpLocationResolver::class);
34
        $command = new ProcessVisitsCommand(
35
            $this->visitService->reveal(),
36
            $this->ipResolver->reveal(),
37
            Translator::factory([])
38
        );
39
        $app = new Application();
40
        $app->add($command);
41
42
        $this->commandTester = new CommandTester($command);
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function allReturnedVisitsIpsAreProcessed()
49
    {
50
        $visits = [
51
            (new Visit())->setRemoteAddr('1.2.3.4'),
52
            (new Visit())->setRemoteAddr('4.3.2.1'),
53
            (new Visit())->setRemoteAddr('12.34.56.78'),
54
        ];
55
        $this->visitService->getUnlocatedVisits()->willReturn($visits)
56
                                                 ->shouldBeCalledTimes(1);
57
58
        $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits));
59
        $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
60
                                                             ->shouldBeCalledTimes(count($visits));
61
62
        $this->commandTester->execute([
63
            'command' => 'visit:process',
64
        ]);
65
        $output = $this->commandTester->getDisplay();
66
        $this->assertTrue(strpos($output, 'Processing IP 1.2.3.4') === 0);
67
        $this->assertTrue(strpos($output, 'Processing IP 4.3.2.1') > 0);
68
        $this->assertTrue(strpos($output, 'Processing IP 12.34.56.78') > 0);
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function localhostAddressIsIgnored()
75
    {
76
        $visits = [
77
            (new Visit())->setRemoteAddr('1.2.3.4'),
78
            (new Visit())->setRemoteAddr('4.3.2.1'),
79
            (new Visit())->setRemoteAddr('12.34.56.78'),
80
            (new Visit())->setRemoteAddr('127.0.0.1'),
81
            (new Visit())->setRemoteAddr('127.0.0.1'),
82
        ];
83
        $this->visitService->getUnlocatedVisits()->willReturn($visits)
84
            ->shouldBeCalledTimes(1);
85
86
        $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 2);
87
        $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
88
                                                             ->shouldBeCalledTimes(count($visits) - 2);
89
90
        $this->commandTester->execute([
91
            'command' => 'visit:process',
92
        ]);
93
        $output = $this->commandTester->getDisplay();
94
        $this->assertTrue(strpos($output, 'Ignored localhost address') > 0);
95
    }
96
}
97