|
| 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\IPInfoDriver; |
| 8 | +use PulkitJalan\IPGeolocation\Exceptions\InvalidCredentialsException; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->validConfig = ['token' => 'test_token']; |
| 12 | + $this->invalidConfig = []; |
| 13 | +}); |
| 14 | + |
| 15 | +it('throws exception with invalid config', function () { |
| 16 | + expect(fn () => new IPInfoDriver($this->invalidConfig)) |
| 17 | + ->toThrow(InvalidCredentialsException::class); |
| 18 | +}); |
| 19 | + |
| 20 | +it('returns correct data for valid IP', function () { |
| 21 | + $mockResponse = [ |
| 22 | + 'ip' => '8.8.8.8', |
| 23 | + 'city' => 'Mountain View', |
| 24 | + 'region' => 'California', |
| 25 | + 'country' => 'US', |
| 26 | + 'loc' => '37.4056,-122.0775', |
| 27 | + 'postal' => '94043', |
| 28 | + 'timezone' => 'America/Los_Angeles', |
| 29 | + ]; |
| 30 | + |
| 31 | + $mock = new MockHandler([ |
| 32 | + new Response(200, [], json_encode($mockResponse)), |
| 33 | + ]); |
| 34 | + |
| 35 | + $handlerStack = HandlerStack::create($mock); |
| 36 | + $client = new Client(['handler' => $handlerStack]); |
| 37 | + |
| 38 | + $driver = new IPInfoDriver($this->validConfig, $client); |
| 39 | + $result = $driver->get('8.8.8.8'); |
| 40 | + |
| 41 | + expect($result)->toMatchArray([ |
| 42 | + 'city' => 'Mountain View', |
| 43 | + 'country' => 'US', |
| 44 | + 'countryCode' => 'US', |
| 45 | + 'latitude' => 37.4056, |
| 46 | + 'longitude' => -122.0775, |
| 47 | + 'region' => 'California', |
| 48 | + 'regionCode' => 'California', |
| 49 | + 'timezone' => 'America/Los_Angeles', |
| 50 | + 'postalCode' => '94043', |
| 51 | + ]); |
| 52 | +}); |
| 53 | + |
| 54 | +it('returns default data for invalid response', function () { |
| 55 | + $mock = new MockHandler([ |
| 56 | + new Response(400, []), |
| 57 | + ]); |
| 58 | + |
| 59 | + $handlerStack = HandlerStack::create($mock); |
| 60 | + $client = new Client(['handler' => $handlerStack]); |
| 61 | + |
| 62 | + $driver = new IPInfoDriver($this->validConfig, $client); |
| 63 | + |
| 64 | + expect($driver->get('invalid_ip'))->toEqual([ |
| 65 | + 'city' => null, |
| 66 | + 'country' => null, |
| 67 | + 'countryCode' => null, |
| 68 | + 'latitude' => null, |
| 69 | + 'longitude' => null, |
| 70 | + 'region' => null, |
| 71 | + 'regionCode' => null, |
| 72 | + 'timezone' => null, |
| 73 | + 'postalCode' => null, |
| 74 | + ]); |
| 75 | +}); |
| 76 | + |
| 77 | +it('returns raw data for valid IP', function () { |
| 78 | + $mockResponse = [ |
| 79 | + 'ip' => '8.8.8.8', |
| 80 | + 'city' => 'Mountain View', |
| 81 | + 'region' => 'California', |
| 82 | + 'country' => 'US', |
| 83 | + 'loc' => '37.4056,-122.0775', |
| 84 | + 'postal' => '94043', |
| 85 | + 'timezone' => 'America/Los_Angeles', |
| 86 | + ]; |
| 87 | + |
| 88 | + $mock = new MockHandler([ |
| 89 | + new Response(200, [], json_encode($mockResponse)), |
| 90 | + ]); |
| 91 | + |
| 92 | + $handlerStack = HandlerStack::create($mock); |
| 93 | + $client = new Client(['handler' => $handlerStack]); |
| 94 | + |
| 95 | + $driver = new IPInfoDriver($this->validConfig, $client); |
| 96 | + $result = $driver->getRaw('8.8.8.8'); |
| 97 | + |
| 98 | + expect($result)->toBe($mockResponse); |
| 99 | +}); |
0 commit comments