1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ipGeolocation\tests; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use GuzzleHttp\Handler\MockHandler; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use GuzzleHttp\Psr7\Response; |
9
|
|
|
use ipGeolocation\GeoIPLocation; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tests for GeoIPLocation class |
14
|
|
|
* |
15
|
|
|
* PHP version 7 |
16
|
|
|
* |
17
|
|
|
* @author yasir khurshid <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class GeoIPLocationTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Success response |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $responseSuccess = array ( |
27
|
|
|
'status' => 'success', |
28
|
|
|
'country' => 'Germany', |
29
|
|
|
'countryCode' => 'DE', |
30
|
|
|
'region' => 'NW', |
31
|
|
|
'regionName' => 'North Rhine-Westphalia', |
32
|
|
|
'city' => 'Wesseling', |
33
|
|
|
'zip' => '50389', |
34
|
|
|
'lat' => '50.8271', |
35
|
|
|
'lon' => '6.9747', |
36
|
|
|
'timezone' => 'Europe/Berlin', |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Fail response |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $responseFail = array ( |
45
|
|
|
'status' => 'fail', |
46
|
|
|
'message' => 'reserved range', |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Test for getGeoLocation method |
51
|
|
|
*/ |
52
|
|
|
public function testGetGeoLocation() |
53
|
|
|
{ |
54
|
|
|
$_SERVER['REMOTE_ADDR'] = '188.110.9.8'; |
55
|
|
|
|
56
|
|
|
$location = $this->getMock($this->responseSuccess)->getGeoLocation(); |
57
|
|
|
|
58
|
|
|
$this->assertInstanceOf('ipGeolocation\Location', $location); |
59
|
|
|
$this->assertTrue($location->getStatus()); |
60
|
|
|
$this->assertSame('Germany', $location->getCountry()); |
61
|
|
|
$this->assertSame('Wesseling', $location->getCity()); |
62
|
|
|
$this->assertSame('DE', $location->getCountryCode()); |
63
|
|
|
$this->assertSame(50.8271, $location->getLatitude()); |
64
|
|
|
$this->assertSame(6.9747, $location->getLongitude()); |
65
|
|
|
$this->assertSame('NW', $location->getRegionCode()); |
66
|
|
|
$this->assertSame('North Rhine-Westphalia', $location->getRegionName()); |
67
|
|
|
$this->assertSame('Europe/Berlin', $location->getTimezone()); |
68
|
|
|
$this->assertSame(50389, $location->getPostalCode()); |
69
|
|
|
$this->assertSame('EUR', $location->getCurrencyIso()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Fail test case for getGeoLocation |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function testGetGeoLocationFail() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$_SERVER['REMOTE_ADDR'] = ''; |
78
|
|
|
$location = $this->getMock($this->responseFail)->getGeoLocation(); |
79
|
|
|
|
80
|
|
|
$this->assertFalse($location->getStatus()); |
81
|
|
|
$this->assertSame('reserved range', $location->getMessage()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Test case for invalid Ip Address |
86
|
|
|
*/ |
87
|
|
|
public function testGetGeoLocationIpInvalid() |
88
|
|
|
{ |
89
|
|
|
$_SERVER['REMOTE_ADDR'] = '188.11098'; |
90
|
|
|
$geoIp = $this->getMock($this->responseFail); |
91
|
|
|
$location = $geoIp->getGeoLocation(); |
92
|
|
|
|
93
|
|
|
$this->assertFalse($location->getStatus()); |
94
|
|
|
$this->assertSame('reserved range', $location->getMessage()); |
95
|
|
|
$this->assertSame('127.0.0.1', $geoIp->getIpAddress()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Test case for invalid request |
100
|
|
|
*/ |
101
|
|
|
public function testRequestInvalid() |
102
|
|
|
{ |
103
|
|
|
$location = $this->getMockRequestException()->getGeoLocation(); |
104
|
|
|
|
105
|
|
|
$this->assertFalse($location->getStatus()); |
106
|
|
|
$this->assertSame('Client error: 400', $location->getMessage()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test case for invalid response |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function testResponseInvalid() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$location = $this->getMock(array(), 500)->getGeoLocation(); |
115
|
|
|
|
116
|
|
|
$this->assertFalse($location->getStatus()); |
117
|
|
|
$this->assertSame('Request failed with response code: 500 and response: Internal Server Error', $location->getMessage()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get mocked object to avoid live api requests |
122
|
|
|
* |
123
|
|
|
* @param array $response Response |
124
|
|
|
* @param int $statusCode Http status code |
125
|
|
|
* |
126
|
|
|
* @return GeoIPLocation |
127
|
|
|
*/ |
128
|
|
|
private function getMock(array $response = array(), int $statusCode = 200): GeoIPLocation |
129
|
|
|
{ |
130
|
|
|
$mock = new MockHandler(array( |
131
|
|
|
new Response($statusCode, [], \json_encode($response), '1.1', ''), |
132
|
|
|
new RequestException('Client error: 400', new Request('GET', 'test')) |
133
|
|
|
)); |
134
|
|
|
|
135
|
|
|
return (new GeoIPLocation())->setMockHandler($mock); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get mock object for exception to test edge error case |
140
|
|
|
* |
141
|
|
|
* @return GeoIPLocation |
142
|
|
|
*/ |
143
|
|
|
private function getMockRequestException() |
144
|
|
|
{ |
145
|
|
|
$mock = new MockHandler(array( |
146
|
|
|
new RequestException('Client error: 400', new Request('GET', 'test')) |
147
|
|
|
)); |
148
|
|
|
|
149
|
|
|
return (new GeoIPLocation())->setMockHandler($mock); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.