1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Repository; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange; |
9
|
|
|
use Shlinkio\Shlink\Core\Entity\Domain; |
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\Model\ShortUrlMeta; |
14
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor; |
15
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository; |
16
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location; |
17
|
|
|
use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase; |
18
|
|
|
|
19
|
|
|
use function Functional\map; |
20
|
|
|
use function range; |
21
|
|
|
use function sprintf; |
22
|
|
|
|
23
|
|
|
class VisitRepositoryTest extends DatabaseTestCase |
24
|
|
|
{ |
25
|
|
|
protected const ENTITIES_TO_EMPTY = [ |
26
|
|
|
VisitLocation::class, |
27
|
|
|
Visit::class, |
28
|
|
|
ShortUrl::class, |
29
|
|
|
Domain::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
private VisitRepository $repo; |
33
|
|
|
|
34
|
|
|
protected function setUp(): void |
35
|
|
|
{ |
36
|
|
|
$this->repo = $this->getEntityManager()->getRepository(Visit::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
* @dataProvider provideBlockSize |
42
|
|
|
*/ |
43
|
|
|
public function findUnlocatedVisitsReturnsProperVisits(int $blockSize): void |
44
|
|
|
{ |
45
|
|
|
$shortUrl = new ShortUrl(''); |
46
|
|
|
$this->getEntityManager()->persist($shortUrl); |
47
|
|
|
|
48
|
|
|
for ($i = 0; $i < 6; $i++) { |
49
|
|
|
$visit = new Visit($shortUrl, Visitor::emptyInstance()); |
50
|
|
|
|
51
|
|
|
if ($i % 2 === 0) { |
52
|
|
|
$location = new VisitLocation(Location::emptyInstance()); |
53
|
|
|
$this->getEntityManager()->persist($location); |
54
|
|
|
$visit->locate($location); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->getEntityManager()->persist($visit); |
58
|
|
|
} |
59
|
|
|
$this->getEntityManager()->flush(); |
60
|
|
|
|
61
|
|
|
$resultsCount = 0; |
62
|
|
|
$results = $this->repo->findUnlocatedVisits(true, $blockSize); |
63
|
|
|
foreach ($results as $value) { |
64
|
|
|
$resultsCount++; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->assertEquals(3, $resultsCount); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function provideBlockSize(): iterable |
71
|
|
|
{ |
72
|
|
|
return map(range(1, 5), fn (int $value) => [$value]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @test */ |
76
|
|
|
public function findVisitsByShortCodeReturnsProperData(): void |
77
|
|
|
{ |
78
|
|
|
[$shortCode, $domain] = $this->createShortUrlsAndVisits(); |
79
|
|
|
|
80
|
|
|
$this->assertCount(0, $this->repo->findVisitsByShortCode('invalid')); |
81
|
|
|
$this->assertCount(6, $this->repo->findVisitsByShortCode($shortCode)); |
82
|
|
|
$this->assertCount(3, $this->repo->findVisitsByShortCode($shortCode, $domain)); |
83
|
|
|
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortCode, null, new DateRange( |
84
|
|
|
Chronos::parse('2016-01-02'), |
85
|
|
|
Chronos::parse('2016-01-03'), |
86
|
|
|
))); |
87
|
|
|
$this->assertCount(4, $this->repo->findVisitsByShortCode($shortCode, null, new DateRange( |
88
|
|
|
Chronos::parse('2016-01-03'), |
89
|
|
|
))); |
90
|
|
|
$this->assertCount(1, $this->repo->findVisitsByShortCode($shortCode, $domain, new DateRange( |
91
|
|
|
Chronos::parse('2016-01-03'), |
92
|
|
|
))); |
93
|
|
|
$this->assertCount(3, $this->repo->findVisitsByShortCode($shortCode, null, null, 3, 2)); |
94
|
|
|
$this->assertCount(2, $this->repo->findVisitsByShortCode($shortCode, null, null, 5, 4)); |
95
|
|
|
$this->assertCount(1, $this->repo->findVisitsByShortCode($shortCode, $domain, null, 3, 2)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @test */ |
99
|
|
|
public function countVisitsByShortCodeReturnsProperData(): void |
100
|
|
|
{ |
101
|
|
|
[$shortCode, $domain] = $this->createShortUrlsAndVisits(); |
102
|
|
|
|
103
|
|
|
$this->assertEquals(0, $this->repo->countVisitsByShortCode('invalid')); |
104
|
|
|
$this->assertEquals(6, $this->repo->countVisitsByShortCode($shortCode)); |
105
|
|
|
$this->assertEquals(3, $this->repo->countVisitsByShortCode($shortCode, $domain)); |
106
|
|
|
$this->assertEquals(2, $this->repo->countVisitsByShortCode($shortCode, null, new DateRange( |
107
|
|
|
Chronos::parse('2016-01-02'), |
108
|
|
|
Chronos::parse('2016-01-03'), |
109
|
|
|
))); |
110
|
|
|
$this->assertEquals(4, $this->repo->countVisitsByShortCode($shortCode, null, new DateRange( |
111
|
|
|
Chronos::parse('2016-01-03'), |
112
|
|
|
))); |
113
|
|
|
$this->assertEquals(1, $this->repo->countVisitsByShortCode($shortCode, $domain, new DateRange( |
114
|
|
|
Chronos::parse('2016-01-03'), |
115
|
|
|
))); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function createShortUrlsAndVisits(): array |
119
|
|
|
{ |
120
|
|
|
$shortUrl = new ShortUrl(''); |
121
|
|
|
$domain = 'example.com'; |
122
|
|
|
$shortCode = $shortUrl->getShortCode(); |
123
|
|
|
$shortUrlWithDomain = new ShortUrl('', ShortUrlMeta::fromRawData([ |
124
|
|
|
'customSlug' => $shortCode, |
125
|
|
|
'domain' => $domain, |
126
|
|
|
])); |
127
|
|
|
|
128
|
|
|
$this->getEntityManager()->persist($shortUrl); |
129
|
|
|
$this->getEntityManager()->persist($shortUrlWithDomain); |
130
|
|
|
|
131
|
|
|
for ($i = 0; $i < 6; $i++) { |
132
|
|
|
$visit = new Visit($shortUrl, Visitor::emptyInstance(), Chronos::parse(sprintf('2016-01-0%s', $i + 1))); |
133
|
|
|
$this->getEntityManager()->persist($visit); |
134
|
|
|
} |
135
|
|
|
for ($i = 0; $i < 3; $i++) { |
136
|
|
|
$visit = new Visit( |
137
|
|
|
$shortUrlWithDomain, |
138
|
|
|
Visitor::emptyInstance(), |
139
|
|
|
Chronos::parse(sprintf('2016-01-0%s', $i + 1)), |
140
|
|
|
); |
141
|
|
|
$this->getEntityManager()->persist($visit); |
142
|
|
|
} |
143
|
|
|
$this->getEntityManager()->flush(); |
144
|
|
|
|
145
|
|
|
return [$shortCode, $domain]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|