|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace InteractionDesignFoundation\GeoIP\Tests\Services; |
| 6 | + |
| 7 | +use InteractionDesignFoundation\GeoIP\Exceptions\MissingConfigurationException; |
| 8 | +use InteractionDesignFoundation\GeoIP\Location; |
| 9 | +use InteractionDesignFoundation\GeoIP\Services\IP2Location; |
| 10 | +use InteractionDesignFoundation\GeoIP\Support\HttpClient; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\Attributes\Test; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +#[CoversClass(IP2Location::class)] |
| 16 | +class IP2LocationTest extends TestCase |
| 17 | +{ |
| 18 | + #[Test] |
| 19 | + public function it_throws_when_api_key_is_missing(): void |
| 20 | + { |
| 21 | + $this->expectException(MissingConfigurationException::class); |
| 22 | + |
| 23 | + new IP2Location([]); |
| 24 | + } |
| 25 | + |
| 26 | + #[Test] |
| 27 | + public function it_boots_with_valid_config(): void |
| 28 | + { |
| 29 | + $service = new IP2Location(['key' => 'test-key']); |
| 30 | + |
| 31 | + $this->assertSame('test-key', $service->config('key')); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function it_returns_a_location_instance(): void |
| 36 | + { |
| 37 | + $service = $this->createServiceWithMockedClient(json_encode($this->sampleApiResponse())); |
| 38 | + |
| 39 | + $location = $service->locate('8.8.8.8'); |
| 40 | + |
| 41 | + $this->assertInstanceOf(Location::class, $location); |
| 42 | + $this->assertSame('8.8.8.8', $location->ip); |
| 43 | + $this->assertSame('US', $location->iso_code); |
| 44 | + $this->assertSame('United States of America', $location->country); |
| 45 | + $this->assertSame('Mountain View', $location->city); |
| 46 | + $this->assertSame('California', $location->state_name); |
| 47 | + $this->assertSame('94043', $location->postal_code); |
| 48 | + $this->assertSame(37.38605, $location->lat); |
| 49 | + $this->assertSame(-122.08385, $location->lon); |
| 50 | + $this->assertSame('-07:00', $location->timezone); |
| 51 | + $this->assertFalse($location->default); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function it_throws_on_curl_error(): void |
| 56 | + { |
| 57 | + $this->expectException(\RuntimeException::class); |
| 58 | + $this->expectExceptionMessage('Request failed'); |
| 59 | + |
| 60 | + $service = $this->createServiceWithMockedClient('', 'Connection timed out'); |
| 61 | + |
| 62 | + $service->locate('8.8.8.8'); |
| 63 | + } |
| 64 | + |
| 65 | + #[Test] |
| 66 | + public function it_throws_on_invalid_json_response(): void |
| 67 | + { |
| 68 | + $this->expectException(\RuntimeException::class); |
| 69 | + $this->expectExceptionMessage('Unexpected ip2location.io response'); |
| 70 | + |
| 71 | + $service = $this->createServiceWithMockedClient('not-json'); |
| 72 | + |
| 73 | + $service->locate('8.8.8.8'); |
| 74 | + } |
| 75 | + |
| 76 | + #[Test] |
| 77 | + public function it_handles_partial_response_gracefully(): void |
| 78 | + { |
| 79 | + $partialResponse = (object) [ |
| 80 | + 'country_code' => 'DE', |
| 81 | + 'country_name' => 'Germany', |
| 82 | + ]; |
| 83 | + |
| 84 | + $service = $this->createServiceWithMockedClient(json_encode($partialResponse)); |
| 85 | + |
| 86 | + $location = $service->locate('1.2.3.4'); |
| 87 | + |
| 88 | + $this->assertInstanceOf(Location::class, $location); |
| 89 | + $this->assertSame('1.2.3.4', $location->ip); |
| 90 | + $this->assertSame('DE', $location->iso_code); |
| 91 | + $this->assertSame('Germany', $location->country); |
| 92 | + $this->assertNull($location->city); |
| 93 | + } |
| 94 | + |
| 95 | + private function createServiceWithMockedClient(string $responseBody, ?string $error = null): IP2Location |
| 96 | + { |
| 97 | + $client = $this->createMock(HttpClient::class); |
| 98 | + $client->method('get')->willReturn([$responseBody, []]); |
| 99 | + $client->method('getErrors')->willReturn($error); |
| 100 | + |
| 101 | + $service = new IP2Location(['key' => 'test-key']); |
| 102 | + |
| 103 | + $reflection = new \ReflectionClass($service); |
| 104 | + $property = $reflection->getProperty('client'); |
| 105 | + $property->setValue($service, $client); |
| 106 | + |
| 107 | + return $service; |
| 108 | + } |
| 109 | + |
| 110 | + private function sampleApiResponse(): object |
| 111 | + { |
| 112 | + return (object) [ |
| 113 | + 'ip' => '8.8.8.8', |
| 114 | + 'country_code' => 'US', |
| 115 | + 'country_name' => 'United States of America', |
| 116 | + 'region_name' => 'California', |
| 117 | + 'city_name' => 'Mountain View', |
| 118 | + 'latitude' => 37.38605, |
| 119 | + 'longitude' => -122.08385, |
| 120 | + 'zip_code' => '94043', |
| 121 | + 'time_zone' => '-07:00', |
| 122 | + 'isp' => 'Google LLC', |
| 123 | + 'domain' => 'google.com', |
| 124 | + 'is_proxy' => false, |
| 125 | + ]; |
| 126 | + } |
| 127 | +} |
0 commit comments