|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\IpGeolocation\GeoLite2; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Exception\RuntimeException; |
|
12
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater; |
|
13
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\GeoLite2Options; |
|
14
|
|
|
use Symfony\Component\Filesystem\Exception as FilesystemException; |
|
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
16
|
|
|
use Zend\Diactoros\Response; |
|
17
|
|
|
|
|
18
|
|
|
class DbUpdaterTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DbUpdater |
|
22
|
|
|
*/ |
|
23
|
|
|
private $dbUpdater; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ObjectProphecy |
|
26
|
|
|
*/ |
|
27
|
|
|
private $httpClient; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ObjectProphecy |
|
30
|
|
|
*/ |
|
31
|
|
|
private $filesystem; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var GeoLite2Options |
|
34
|
|
|
*/ |
|
35
|
|
|
private $options; |
|
36
|
|
|
|
|
37
|
|
|
public function setUp() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class); |
|
40
|
|
|
$this->filesystem = $this->prophesize(Filesystem::class); |
|
41
|
|
|
$this->options = new GeoLite2Options([ |
|
42
|
|
|
'temp_dir' => __DIR__ . '/../../../test-resources', |
|
43
|
|
|
'db_location' => '', |
|
44
|
|
|
'download_from' => '', |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$this->dbUpdater = new DbUpdater($this->httpClient->reveal(), $this->filesystem->reveal(), $this->options); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
*/ |
|
53
|
|
|
public function anExceptionIsThrownIfFreshDbCannotBeDownloaded() |
|
54
|
|
|
{ |
|
55
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class); |
|
56
|
|
|
|
|
57
|
|
|
$this->expectException(RuntimeException::class); |
|
58
|
|
|
$this->expectExceptionMessage( |
|
59
|
|
|
'An error occurred while trying to download a fresh copy of the GeoLite2 database' |
|
60
|
|
|
); |
|
61
|
|
|
$request->shouldBeCalledOnce(); |
|
62
|
|
|
|
|
63
|
|
|
$this->dbUpdater->downloadFreshCopy(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @test |
|
68
|
|
|
*/ |
|
69
|
|
|
public function anExceptionIsThrownIfFreshDbCannotBeExtracted() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->options->tempDir = '__invalid__'; |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response()); |
|
74
|
|
|
|
|
75
|
|
|
$this->expectException(RuntimeException::class); |
|
76
|
|
|
$this->expectExceptionMessage( |
|
77
|
|
|
'An error occurred while trying to extract the GeoLite2 database from __invalid__/GeoLite2-City.tar.gz' |
|
78
|
|
|
); |
|
79
|
|
|
$request->shouldBeCalledOnce(); |
|
80
|
|
|
|
|
81
|
|
|
$this->dbUpdater->downloadFreshCopy(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @test |
|
86
|
|
|
* @dataProvider provideFilesystemExceptions |
|
87
|
|
|
*/ |
|
88
|
|
|
public function anExceptionIsThrownIfFreshDbCannotBeCopiedToDestination(string $e) |
|
89
|
|
|
{ |
|
90
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response()); |
|
91
|
|
|
$copy = $this->filesystem->copy(Argument::cetera())->willThrow($e); |
|
92
|
|
|
|
|
93
|
|
|
$this->expectException(RuntimeException::class); |
|
94
|
|
|
$this->expectExceptionMessage('An error occurred while trying to copy GeoLite2 db file to destination'); |
|
95
|
|
|
$request->shouldBeCalledOnce(); |
|
96
|
|
|
$copy->shouldBeCalledOnce(); |
|
97
|
|
|
|
|
98
|
|
|
$this->dbUpdater->downloadFreshCopy(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function provideFilesystemExceptions(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
|
|
[FilesystemException\FileNotFoundException::class], |
|
105
|
|
|
[FilesystemException\IOException::class], |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @test |
|
111
|
|
|
*/ |
|
112
|
|
|
public function noExceptionsAreThrownIfEverythingWorksFine() |
|
113
|
|
|
{ |
|
114
|
|
|
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response()); |
|
115
|
|
|
$copy = $this->filesystem->copy(Argument::cetera())->will(function () { |
|
116
|
|
|
}); |
|
117
|
|
|
$remove = $this->filesystem->remove(Argument::cetera())->will(function () { |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
$this->dbUpdater->downloadFreshCopy(); |
|
121
|
|
|
|
|
122
|
|
|
$request->shouldHaveBeenCalledOnce(); |
|
123
|
|
|
$copy->shouldHaveBeenCalledOnce(); |
|
124
|
|
|
$remove->shouldHaveBeenCalledOnce(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|