-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathGeoIP2AdapterTest.php
More file actions
168 lines (141 loc) · 4.92 KB
/
GeoIP2AdapterTest.php
File metadata and controls
168 lines (141 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\GeoIP2\Tests;
use Geocoder\Provider\GeoIP2\GeoIP2Adapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* @author Jens Wiese <jens@howtrueisfalse.de>
*/
class GeoIP2AdapterTest extends TestCase
{
/**
* @var GeoIP2Adapter
*/
protected $adapter;
/**
* @throws \RuntimeException
*/
public static function setUpBeforeClass(): void
{
if (false === class_exists('\GeoIp2\Database\Reader')) {
throw new \RuntimeException("The maxmind's lib 'geoip2/geoip2' is required to run this test.");
}
}
public function setUp(): void
{
$this->adapter = new GeoIP2Adapter($this->getGeoIP2ProviderMock());
}
public function testGetName(): void
{
$expectedName = 'maxmind_geoip2';
$this->assertEquals($expectedName, $this->adapter->getName());
}
public function testGetContentMustBeCalledWithUrl(): void
{
$this->expectException(\Geocoder\Exception\InvalidArgument::class);
$this->expectExceptionMessage('must be called with a valid url. Got "127.0.0.1" instead.');
$url = '127.0.0.1';
$this->adapter->getContent($url);
}
public function testAddressPassedToReaderMustBeIpAddress(): void
{
$this->expectException(\Geocoder\Exception\InvalidArgument::class);
$this->expectExceptionMessage('URL must contain a valid query-string (an IP address, 127.0.0.1 for instance)');
$url = 'file://database?not-valid=1';
$this->adapter->getContent($url);
}
/**
* @return array<string[]>
*/
public static function provideDataForSwitchingRequestMethods(): array
{
return [
[GeoIP2Adapter::GEOIP2_MODEL_CITY],
[GeoIP2Adapter::GEOIP2_MODEL_COUNTRY],
];
}
/**
* @dataProvider provideDataForSwitchingRequestMethods
*/
public function testIpAddressIsPassedCorrectToReader(string $geoIp2Model): void
{
$geoIp2Provider = $this->getGeoIP2ProviderMock();
$geoIp2Provider
->expects($this->once())
->method($geoIp2Model)
->with('127.0.0.1')
->will($this->returnValue(
$this->getGeoIP2ModelMock($geoIp2Model)
));
$adapter = new GeoIP2Adapter($geoIp2Provider, $geoIp2Model);
$adapter->getContent('file://geoip?127.0.0.1');
}
public function testNotSupportedGeoIP2ModelLeadsToException(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('Model "unsupported_model" is not available.');
new GeoIP2Adapter($this->getGeoIP2ProviderMock(), 'unsupported_model');
}
public function testReaderResponseIsJsonEncoded(): void
{
$cityModel = $this->getGeoIP2ModelMock(GeoIP2Adapter::GEOIP2_MODEL_CITY);
$geoIp2Provider = $this->getGeoIP2ProviderMock();
$geoIp2Provider
->expects($this->any())
->method('city')
->will($this->returnValue($cityModel));
$adapter = new GeoIP2Adapter($geoIp2Provider);
$result = $adapter->getContent('file://database?127.0.0.1');
$this->assertJson($result);
$decodedResult = json_decode($result);
$this->assertObjectHasProperty('city', $decodedResult);
}
/**
* @return MockObject
*/
protected function getGeoIP2ProviderMock()
{
$mock = $this->getMockBuilder('\GeoIp2\ProviderInterface')->getMock();
return $mock;
}
/**
* @param string $geoIP2Model (e.g. GeoIP2Adapter::GEOIP2_MODEL_CITY, ...)
*
* @return MockObject
*/
protected function getGeoIP2ModelMock($geoIP2Model)
{
/** @var class-string $mockClass */
$mockClass = '\\GeoIp2\\Model\\'.ucfirst($geoIP2Model);
$mock = $this->getMockBuilder($mockClass)->disableOriginalConstructor()->getMock();
$mock
->expects($this->any())
->method('jsonSerialize')
->will($this->returnValue(
[
'city' => [
'geoname_id' => 2911298,
'names' => [
'de' => 'Hamburg',
'en' => 'Hamburg',
'es' => 'Hamburgo',
'fr' => 'Hambourg',
'ja' => 'ハンブルク',
'pt-BR' => 'Hamburgo',
'ru' => 'Гамбург',
'zh-CN' => '汉堡市',
],
],
]
));
return $mock;
}
}