1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ipGeolocation\tests; |
4
|
|
|
|
5
|
|
|
use ipGeolocation\GeoIPLocation; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tests for GeoIPLocation class |
10
|
|
|
* |
11
|
|
|
* PHP version 7 |
12
|
|
|
* |
13
|
|
|
* @author yasir khurshid <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class GeoIPLocationTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testGetGeoLocation() |
18
|
|
|
{ |
19
|
|
|
$_SERVER['REMOTE_ADDR'] = '188.110.9.8'; |
20
|
|
|
$location = (new GeoIPLocation())->getGeoLocation(); |
21
|
|
|
|
22
|
|
|
$this->assertInstanceOf('ipGeolocation\Location', $location); |
23
|
|
|
|
24
|
|
|
$this->assertTrue($location->getStatus()); |
25
|
|
|
$this->assertSame('Wesseling', $location->getCity()); |
26
|
|
|
$this->assertSame('Germany', $location->getCountry()); |
27
|
|
|
$this->assertSame('DE', $location->getCountryCode()); |
28
|
|
|
$this->assertSame(50.8271, $location->getLatitude()); |
29
|
|
|
$this->assertSame(6.9747, $location->getLongitude()); |
30
|
|
|
$this->assertSame('NW', $location->getRegionCode()); |
31
|
|
|
$this->assertSame('North Rhine-Westphalia', $location->getRegionName()); |
32
|
|
|
$this->assertSame('Europe/Berlin', $location->getTimezone()); |
33
|
|
|
$this->assertSame(50389, $location->getPostalCode()); |
34
|
|
|
$this->assertSame('EUR', $location->getCurrencyIso()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testGetGeoLocationFail() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$_SERVER['REMOTE_ADDR'] = ''; |
40
|
|
|
$location = (new GeoIPLocation())->getGeoLocation(); |
41
|
|
|
|
42
|
|
|
$this->assertInstanceOf('ipGeolocation\Location', $location); |
43
|
|
|
|
44
|
|
|
$this->assertFalse($location->getStatus()); |
45
|
|
|
$this->assertSame('reserved range', $location->getMessage()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testGetGeoLocationIpInvalid() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$_SERVER['REMOTE_ADDR'] = '188.11098'; |
51
|
|
|
$geoIp = new GeoIPLocation(); |
52
|
|
|
$location = $geoIp->getGeoLocation(); |
53
|
|
|
|
54
|
|
|
$this->assertFalse($location->getStatus()); |
55
|
|
|
$this->assertSame('reserved range', $location->getMessage()); |
56
|
|
|
$this->assertSame('127.0.0.1', $geoIp->getIpAddress()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
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.