|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace PulkitJalan\GeoIP\Tests\Drivers; |
4 | | - |
5 | 3 | use Illuminate\Support\Arr; |
6 | 4 | use PulkitJalan\GeoIP\GeoIP; |
7 | | -use PulkitJalan\GeoIP\Tests\AbstractTestCase; |
8 | 5 | use PulkitJalan\GeoIP\Exceptions\GeoIPException; |
9 | 6 |
|
10 | | -class IPApiDriverTest extends AbstractTestCase |
11 | | -{ |
12 | | - public function test_ip_api() |
13 | | - { |
14 | | - $config = [ |
15 | | - 'driver' => 'ip-api', |
16 | | - ]; |
17 | | - |
18 | | - $geoip = new GeoIP($config); |
19 | | - $geoip = $geoip->setIp($this->validIp); |
20 | | - |
21 | | - $this->assertEquals($geoip->getCountry(), 'United Kingdom'); |
22 | | - |
23 | | - $geoip = $geoip->setIp($this->invalidIp); |
24 | | - |
25 | | - $this->assertEquals('fail', Arr::get($geoip->getRaw(), 'status')); |
26 | | - |
27 | | - $this->assertEquals([ |
28 | | - 'city' => null, |
29 | | - 'country' => null, |
30 | | - 'countryCode' => null, |
31 | | - 'latitude' => null, |
32 | | - 'longitude' => null, |
33 | | - 'region' => null, |
34 | | - 'regionCode' => null, |
35 | | - 'timezone' => null, |
36 | | - 'postalCode' => null, |
37 | | - ], $geoip->get()); |
38 | | - |
39 | | - $this->assertEquals('', $geoip->getCountry()); |
40 | | - } |
41 | | - |
42 | | - public function test_get_multiple_ipaddress() |
43 | | - { |
44 | | - $config = [ |
45 | | - 'driver' => 'ip-api', |
46 | | - ]; |
47 | | - |
48 | | - $geoip = new GeoIP($config); |
49 | | - $geoip->setIp($this->multipleIps); |
50 | | - $ip = $geoip->getIp(); |
51 | | - |
52 | | - $this->assertEquals($this->validIp, $ip); |
53 | | - $this->assertTrue(! filter_var($ip, FILTER_VALIDATE_IP) === false); |
54 | | - } |
55 | | - |
56 | | - public function test_get_random_ipaddress() |
57 | | - { |
58 | | - $config = [ |
59 | | - 'driver' => 'ip-api', |
60 | | - 'random' => true, |
61 | | - ]; |
62 | | - |
63 | | - $geoip = new GeoIP($config); |
64 | | - $ip = $geoip->getIp(); |
65 | | - |
66 | | - $this->assertNotEquals($this->invalidIp, $ip); |
67 | | - $this->assertTrue(! filter_var($ip, FILTER_VALIDATE_IP) === false); |
68 | | - } |
69 | | - |
70 | | - public function test_get_non_random_ipaddress() |
71 | | - { |
72 | | - $config = [ |
73 | | - 'driver' => 'ip-api', |
74 | | - 'random' => false, |
75 | | - ]; |
76 | | - |
77 | | - $geoip = new GeoIP($config); |
78 | | - $ip = $geoip->getIp(); |
79 | | - |
80 | | - $this->assertEquals($this->invalidIp, $ip); |
81 | | - $this->assertTrue(! filter_var($ip, FILTER_VALIDATE_IP) === false); |
82 | | - } |
83 | | - |
84 | | - public function test_ip_api_pro_exception() |
85 | | - { |
86 | | - $config = [ |
87 | | - 'driver' => 'ip-api', |
88 | | - 'ip-api' => [ |
89 | | - 'key' => 'test', |
90 | | - ], |
91 | | - ]; |
92 | | - |
93 | | - $this->expectException(GeoIPException::class); |
94 | | - |
95 | | - $geoip = new GeoIP($config); |
96 | | - $geoip = $geoip->setIp($this->validIp); |
97 | | - |
98 | | - $geoip->get(); |
99 | | - } |
100 | | - |
101 | | - public function test_ip_api_pro_exception_getRaw() |
102 | | - { |
103 | | - $config = [ |
104 | | - 'driver' => 'ip-api', |
105 | | - 'ip-api' => [ |
106 | | - 'key' => 'test', |
107 | | - ], |
108 | | - ]; |
109 | | - |
110 | | - $this->expectException(GeoIPException::class); |
111 | | - |
112 | | - $geoip = new GeoIP($config); |
113 | | - $geoip = $geoip->setIp($this->validIp); |
114 | | - |
115 | | - $geoip->getRaw(); |
116 | | - } |
117 | | -} |
| 7 | +test('ip api', function () { |
| 8 | + $config = [ |
| 9 | + 'driver' => 'ip-api', |
| 10 | + ]; |
| 11 | + |
| 12 | + $geoip = new GeoIP($config); |
| 13 | + $geoip = $geoip->setIp($this->validIp); |
| 14 | + |
| 15 | + expect('United Kingdom')->toEqual($geoip->getCountry()); |
| 16 | + |
| 17 | + $geoip = $geoip->setIp($this->invalidIp); |
| 18 | + |
| 19 | + expect(Arr::get($geoip->getRaw(), 'status'))->toEqual('fail'); |
| 20 | + |
| 21 | + expect($geoip->get())->toEqual([ |
| 22 | + 'city' => null, |
| 23 | + 'country' => null, |
| 24 | + 'countryCode' => null, |
| 25 | + 'latitude' => null, |
| 26 | + 'longitude' => null, |
| 27 | + 'region' => null, |
| 28 | + 'regionCode' => null, |
| 29 | + 'timezone' => null, |
| 30 | + 'postalCode' => null, |
| 31 | + ]); |
| 32 | + |
| 33 | + expect($geoip->getCountry())->toEqual(''); |
| 34 | +}); |
| 35 | + |
| 36 | +test('get multiple ipaddress', function () { |
| 37 | + $config = [ |
| 38 | + 'driver' => 'ip-api', |
| 39 | + ]; |
| 40 | + |
| 41 | + $geoip = new GeoIP($config); |
| 42 | + $geoip->setIp($this->multipleIps); |
| 43 | + $ip = $geoip->getIp(); |
| 44 | + |
| 45 | + expect($ip)->toEqual($this->validIp); |
| 46 | + expect(! filter_var($ip, FILTER_VALIDATE_IP) === false)->toBeTrue(); |
| 47 | +}); |
| 48 | + |
| 49 | +test('get random ipaddress', function () { |
| 50 | + $config = [ |
| 51 | + 'driver' => 'ip-api', |
| 52 | + 'random' => true, |
| 53 | + ]; |
| 54 | + |
| 55 | + $geoip = new GeoIP($config); |
| 56 | + $ip = $geoip->getIp(); |
| 57 | + |
| 58 | + $this->assertNotEquals($this->invalidIp, $ip); |
| 59 | + expect(! filter_var($ip, FILTER_VALIDATE_IP) === false)->toBeTrue(); |
| 60 | +}); |
| 61 | + |
| 62 | +test('get non random ipaddress', function () { |
| 63 | + $config = [ |
| 64 | + 'driver' => 'ip-api', |
| 65 | + 'random' => false, |
| 66 | + ]; |
| 67 | + |
| 68 | + $geoip = new GeoIP($config); |
| 69 | + $ip = $geoip->getIp(); |
| 70 | + |
| 71 | + expect($ip)->toEqual($this->invalidIp); |
| 72 | + expect(! filter_var($ip, FILTER_VALIDATE_IP) === false)->toBeTrue(); |
| 73 | +}); |
| 74 | + |
| 75 | +test('ip api pro exception', function () { |
| 76 | + $config = [ |
| 77 | + 'driver' => 'ip-api', |
| 78 | + 'ip-api' => [ |
| 79 | + 'key' => 'test', |
| 80 | + ], |
| 81 | + ]; |
| 82 | + |
| 83 | + $this->expectException(GeoIPException::class); |
| 84 | + |
| 85 | + $geoip = new GeoIP($config); |
| 86 | + $geoip = $geoip->setIp($this->validIp); |
| 87 | + |
| 88 | + $geoip->get(); |
| 89 | +}); |
| 90 | + |
| 91 | +test('ip api pro exception get raw', function () { |
| 92 | + $config = [ |
| 93 | + 'driver' => 'ip-api', |
| 94 | + 'ip-api' => [ |
| 95 | + 'key' => 'test', |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + $this->expectException(GeoIPException::class); |
| 100 | + |
| 101 | + $geoip = new GeoIP($config); |
| 102 | + $geoip = $geoip->setIp($this->validIp); |
| 103 | + |
| 104 | + $geoip->getRaw(); |
| 105 | +}); |
0 commit comments