|
| 1 | +<?php |
| 2 | + |
| 3 | +use GuzzleHttp\Client; |
| 4 | +use Mockery\MockInterface; |
| 5 | +use GuzzleHttp\Psr7\Response; |
| 6 | +use PulkitJalan\IPGeolocation\IPGeolocation; |
| 7 | +use PulkitJalan\IPGeolocation\Exceptions\IPGeolocationException; |
| 8 | + |
| 9 | +test('ipquery driver', function () { |
| 10 | + $config = [ |
| 11 | + 'driver' => 'ipquery', |
| 12 | + 'ipquery' => [ |
| 13 | + 'key' => 'test-key', |
| 14 | + ], |
| 15 | + ]; |
| 16 | + |
| 17 | + /** @var MockInterface|Client $client */ |
| 18 | + $client = Mockery::mock(Client::class); |
| 19 | + |
| 20 | + $client->shouldReceive('get') |
| 21 | + ->once() |
| 22 | + ->andReturn( |
| 23 | + new Response(200, [], json_encode([ |
| 24 | + 'location' => [ |
| 25 | + 'city' => 'Los Angeles', |
| 26 | + 'country' => 'United States', |
| 27 | + 'country_code' => 'US', |
| 28 | + 'latitude' => 34.0522, |
| 29 | + 'longitude' => -118.2437, |
| 30 | + 'state' => 'California', |
| 31 | + 'timezone' => 'America/Los_Angeles', |
| 32 | + 'zipcode' => '90012', |
| 33 | + ], |
| 34 | + ])) |
| 35 | + ); |
| 36 | + |
| 37 | + $ip = new IPGeolocation($config, $client); |
| 38 | + $ip = $ip->setIp('8.8.8.8'); |
| 39 | + |
| 40 | + expect($ip->get()) |
| 41 | + ->toHaveKey('city', 'Los Angeles') |
| 42 | + ->toHaveKey('country', 'United States') |
| 43 | + ->toHaveKey('countryCode', 'US') |
| 44 | + ->toHaveKey('latitude', 34.05220) |
| 45 | + ->toHaveKey('longitude', -118.24370) |
| 46 | + ->toHaveKey('region', 'California') |
| 47 | + ->toHaveKey('regionCode', null) |
| 48 | + ->toHaveKey('timezone', 'America/Los_Angeles') |
| 49 | + ->toHaveKey('postalCode', '90012'); |
| 50 | + |
| 51 | + expect('Los Angeles')->toEqual($ip->getCity()); |
| 52 | + expect('United States')->toEqual($ip->getCountry()); |
| 53 | +}); |
| 54 | + |
| 55 | +test('ipquery driver returns default values when error occurs', function () { |
| 56 | + $config = [ |
| 57 | + 'driver' => 'ipquery', |
| 58 | + 'ipquery' => [ |
| 59 | + 'key' => 'test-key', |
| 60 | + ], |
| 61 | + ]; |
| 62 | + |
| 63 | + /** @var MockInterface|Client $client */ |
| 64 | + $client = Mockery::mock(Client::class); |
| 65 | + |
| 66 | + $client->shouldReceive('get') |
| 67 | + ->once() |
| 68 | + ->andReturn( |
| 69 | + new Response(200, [], json_encode(['error' => 'Invalid IP address'])) |
| 70 | + ); |
| 71 | + |
| 72 | + $ip = new IPGeolocation($config, $client); |
| 73 | + $ip = $ip->setIp('invalid-ip'); |
| 74 | + |
| 75 | + expect($ip->get())->toEqual([ |
| 76 | + 'city' => null, |
| 77 | + 'country' => null, |
| 78 | + 'countryCode' => null, |
| 79 | + 'latitude' => null, |
| 80 | + 'longitude' => null, |
| 81 | + 'region' => null, |
| 82 | + 'regionCode' => null, |
| 83 | + 'timezone' => null, |
| 84 | + 'postalCode' => null, |
| 85 | + ]); |
| 86 | + |
| 87 | + expect($ip->getCity())->toEqual(''); |
| 88 | + expect($ip->getCountry())->toEqual(''); |
| 89 | +}); |
| 90 | + |
| 91 | +test('ipquery driver throws exception when api key is missing', function () { |
| 92 | + $config = [ |
| 93 | + 'driver' => 'ipquery', |
| 94 | + ]; |
| 95 | + |
| 96 | + $ip = new IPGeolocation($config); |
| 97 | + $ip = $ip->setIp('8.8.8.8'); |
| 98 | + |
| 99 | + expect(fn () => $ip->get()) |
| 100 | + ->toThrow(IPGeolocationException::class, 'Failed to get ip geolocation data'); |
| 101 | +}); |
0 commit comments