1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Core\Service; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
10
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
11
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit; |
12
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation; |
13
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException; |
14
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor; |
15
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository; |
16
|
|
|
use Shlinkio\Shlink\Core\Service\VisitService; |
17
|
|
|
use function array_shift; |
18
|
|
|
use function count; |
19
|
|
|
use function func_get_args; |
20
|
|
|
|
21
|
|
|
class VisitServiceTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var VisitService |
25
|
|
|
*/ |
26
|
|
|
protected $visitService; |
27
|
|
|
/** |
28
|
|
|
* @var ObjectProphecy |
29
|
|
|
*/ |
30
|
|
|
protected $em; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->em = $this->prophesize(EntityManager::class); |
35
|
|
|
$this->visitService = new VisitService($this->em->reveal()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
|
public function locateVisitsIteratesAndLocatesUnlocatedVisits() |
42
|
|
|
{ |
43
|
|
|
$unlocatedVisits = [ |
44
|
|
|
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())], |
45
|
|
|
[new Visit(new ShortUrl('bar'), Visitor::emptyInstance())], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$repo = $this->prophesize(VisitRepository::class); |
49
|
|
|
$findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits); |
50
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal()); |
51
|
|
|
|
52
|
|
|
$persist = $this->em->persist(Argument::type(Visit::class))->will(function () { |
53
|
|
|
}); |
54
|
|
|
$flush = $this->em->flush()->will(function () { |
55
|
|
|
}); |
56
|
|
|
$clear = $this->em->clear()->will(function () { |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$this->visitService->locateVisits(function () { |
60
|
|
|
return []; |
61
|
|
|
}, function () { |
62
|
|
|
$args = func_get_args(); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(VisitLocation::class, array_shift($args)); |
65
|
|
|
$this->assertInstanceOf(Visit::class, array_shift($args)); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$findUnlocatedVisits->shouldHaveBeenCalledOnce(); |
69
|
|
|
$getRepo->shouldHaveBeenCalledOnce(); |
70
|
|
|
$persist->shouldHaveBeenCalledTimes(count($unlocatedVisits)); |
71
|
|
|
$flush->shouldHaveBeenCalledTimes(count($unlocatedVisits)); |
72
|
|
|
$clear->shouldHaveBeenCalledTimes(count($unlocatedVisits)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function visitsWhichCannotBeLocatedAreIgnored() |
79
|
|
|
{ |
80
|
|
|
$unlocatedVisits = [ |
81
|
|
|
[new Visit(new ShortUrl('foo'), Visitor::emptyInstance())], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
$repo = $this->prophesize(VisitRepository::class); |
85
|
|
|
$findUnlocatedVisits = $repo->findUnlocatedVisits()->willReturn($unlocatedVisits); |
86
|
|
|
$getRepo = $this->em->getRepository(Visit::class)->willReturn($repo->reveal()); |
87
|
|
|
|
88
|
|
|
$persist = $this->em->persist(Argument::type(Visit::class))->will(function () { |
89
|
|
|
}); |
90
|
|
|
$flush = $this->em->flush()->will(function () { |
91
|
|
|
}); |
92
|
|
|
$clear = $this->em->clear()->will(function () { |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$this->visitService->locateVisits(function () { |
96
|
|
|
throw new IpCannotBeLocatedException('Cannot be located'); |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
$findUnlocatedVisits->shouldHaveBeenCalledOnce(); |
100
|
|
|
$getRepo->shouldHaveBeenCalledOnce(); |
101
|
|
|
$persist->shouldNotHaveBeenCalled(); |
102
|
|
|
$flush->shouldNotHaveBeenCalled(); |
103
|
|
|
$clear->shouldNotHaveBeenCalled(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|