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() |
|
|
|
|
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
|
|
|
|
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.