|
| 1 | +<?php |
| 2 | + |
| 3 | +use GuzzleHttp\Client; |
| 4 | +use GuzzleHttp\HandlerStack; |
| 5 | +use GuzzleHttp\Psr7\Response; |
| 6 | +use GuzzleHttp\Handler\MockHandler; |
| 7 | +use PulkitJalan\IPGeolocation\Drivers\IP2LocationDriver; |
| 8 | +use PulkitJalan\IPGeolocation\Exceptions\InvalidCredentialsException; |
| 9 | + |
| 10 | +test('it throws exception with invalid credentials', function () { |
| 11 | + expect(fn () => new IP2LocationDriver([])) |
| 12 | + ->toThrow(InvalidCredentialsException::class); |
| 13 | +}); |
| 14 | + |
| 15 | +test('it returns default for invalid response', function () { |
| 16 | + $mock = new MockHandler([ |
| 17 | + new Response(200, [], json_encode(['response' => 'FAILED'])), |
| 18 | + ]); |
| 19 | + |
| 20 | + $handler = HandlerStack::create($mock); |
| 21 | + $client = new Client(['handler' => $handler]); |
| 22 | + |
| 23 | + $driver = new IP2LocationDriver(['api_key' => 'test_key'], $client); |
| 24 | + |
| 25 | + expect($driver->get('127.0.0.1'))->toEqual([ |
| 26 | + 'city' => null, |
| 27 | + 'country' => null, |
| 28 | + 'countryCode' => null, |
| 29 | + 'latitude' => null, |
| 30 | + 'longitude' => null, |
| 31 | + 'region' => null, |
| 32 | + 'regionCode' => null, |
| 33 | + 'timezone' => null, |
| 34 | + 'postalCode' => null, |
| 35 | + ]); |
| 36 | +}); |
| 37 | + |
| 38 | +test('it returns correct data', function () { |
| 39 | + $mockResponse = [ |
| 40 | + 'response' => 'OK', |
| 41 | + 'country_code' => 'US', |
| 42 | + 'country_name' => 'United States of America', |
| 43 | + 'region_name' => 'California', |
| 44 | + 'region_code' => 'CA', |
| 45 | + 'city_name' => 'Los Angeles', |
| 46 | + 'latitude' => 34.05223, |
| 47 | + 'longitude' => -118.24368, |
| 48 | + 'zip_code' => '90001', |
| 49 | + 'time_zone' => '-07:00', |
| 50 | + ]; |
| 51 | + |
| 52 | + $mock = new MockHandler([ |
| 53 | + new Response(200, [], json_encode($mockResponse)), |
| 54 | + ]); |
| 55 | + |
| 56 | + $handler = HandlerStack::create($mock); |
| 57 | + $client = new Client(['handler' => $handler]); |
| 58 | + |
| 59 | + $driver = new IP2LocationDriver(['api_key' => 'test_key'], $client); |
| 60 | + |
| 61 | + $result = $driver->get('127.0.0.1'); |
| 62 | + |
| 63 | + $expected = [ |
| 64 | + 'city' => 'Los Angeles', |
| 65 | + 'country' => 'United States of America', |
| 66 | + 'countryCode' => 'US', |
| 67 | + 'latitude' => 34.05223, |
| 68 | + 'longitude' => -118.24368, |
| 69 | + 'region' => 'California', |
| 70 | + 'regionCode' => 'CA', |
| 71 | + 'timezone' => '-07:00', |
| 72 | + 'postalCode' => '90001', |
| 73 | + ]; |
| 74 | + |
| 75 | + expect($result)->toBe($expected); |
| 76 | +}); |
| 77 | + |
| 78 | +test('it handles raw data', function () { |
| 79 | + $mockResponse = [ |
| 80 | + 'response' => 'OK', |
| 81 | + 'country_code' => 'US', |
| 82 | + 'country_name' => 'United States of America', |
| 83 | + 'region_name' => 'California', |
| 84 | + 'city_name' => 'Los Angeles', |
| 85 | + ]; |
| 86 | + |
| 87 | + $mock = new MockHandler([ |
| 88 | + new Response(200, [], json_encode($mockResponse)), |
| 89 | + ]); |
| 90 | + |
| 91 | + $handler = HandlerStack::create($mock); |
| 92 | + $client = new Client(['handler' => $handler]); |
| 93 | + |
| 94 | + $driver = new IP2LocationDriver(['api_key' => 'test_key'], $client); |
| 95 | + |
| 96 | + $result = $driver->getRaw('127.0.0.1'); |
| 97 | + |
| 98 | + expect($result)->toBe($mockResponse); |
| 99 | +}); |
0 commit comments