Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 21 | class FreeGeoIpTest extends BaseTestCase |
||
| 22 | { |
||
| 23 | protected function getCacheDir() |
||
| 24 | { |
||
| 25 | return __DIR__.'/.cached_responses'; |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testGetName() |
||
| 29 | { |
||
| 30 | $provider = new FreeGeoIp($this->getMockedHttpClient()); |
||
| 31 | $this->assertEquals('free_geo_ip', $provider->getName()); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 36 | * @expectedExceptionMessage The FreeGeoIp provider does not support street addresses. |
||
| 37 | */ |
||
| 38 | public function testGeocodeWithAddress() |
||
| 39 | { |
||
| 40 | $provider = new FreeGeoIp($this->getMockedHttpClient()); |
||
| 41 | $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testGeocodeWithLocalhostIPv4() |
||
| 45 | { |
||
| 46 | $provider = new FreeGeoIp($this->getMockedHttpClient()); |
||
| 47 | $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
||
| 48 | |||
| 49 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 50 | $this->assertCount(1, $results); |
||
| 51 | |||
| 52 | /** @var Location $result */ |
||
| 53 | $result = $results->first(); |
||
| 54 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 55 | $this->assertEquals('localhost', $result->getLocality()); |
||
| 56 | $this->assertEquals('localhost', $result->getCountry()->getName()); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testGeocodeWithLocalhostIPv6() |
||
| 60 | { |
||
| 61 | $provider = new FreeGeoIp($this->getMockedHttpClient()); |
||
| 62 | $results = $provider->geocodeQuery(GeocodeQuery::create('::1')); |
||
| 63 | |||
| 64 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 65 | $this->assertCount(1, $results); |
||
| 66 | |||
| 67 | /** @var Location $result */ |
||
| 68 | $result = $results->first(); |
||
| 69 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 70 | $this->assertEquals('localhost', $result->getLocality()); |
||
| 71 | $this->assertEquals('localhost', $result->getCountry()->getName()); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function testGeocodeWithRealIPv4() |
||
| 75 | { |
||
| 76 | $provider = new FreeGeoIp($this->getHttpClient()); |
||
| 77 | $results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
| 78 | |||
| 79 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 80 | $this->assertCount(1, $results); |
||
| 81 | |||
| 82 | /** @var Location $result */ |
||
| 83 | $result = $results->first(); |
||
| 84 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 85 | $this->assertEquals(33.0347, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 86 | $this->assertEquals(-96.8134, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 87 | $this->assertEquals(75093, $result->getPostalCode()); |
||
| 88 | $this->assertEquals('Plano', $result->getLocality()); |
||
| 89 | $this->assertCount(1, $result->getAdminLevels()); |
||
| 90 | $this->assertEquals('Texas', $result->getAdminLevels()->get(1)->getName()); |
||
| 91 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
| 92 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testGeocodeWithRealIPv6() |
||
| 96 | { |
||
| 97 | $provider = new FreeGeoIp($this->getHttpClient()); |
||
| 98 | $results = $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
||
| 99 | |||
| 100 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 101 | $this->assertCount(1, $results); |
||
| 102 | |||
| 103 | /** @var Location $result */ |
||
| 104 | $result = $results->first(); |
||
| 105 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 106 | $this->assertEquals(33.0347, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 107 | $this->assertEquals(-96.8134, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 108 | $this->assertEquals(75093, $result->getPostalCode()); |
||
| 109 | $this->assertEquals('Plano', $result->getLocality()); |
||
| 110 | $this->assertCount(1, $result->getAdminLevels()); |
||
| 111 | $this->assertEquals('Texas', $result->getAdminLevels()->get(1)->getName()); |
||
| 112 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
| 113 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function testGeocodeWithUSIPv4() |
||
| 117 | { |
||
| 118 | $provider = new FreeGeoIp($this->getHttpClient()); |
||
| 119 | $results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
| 120 | |||
| 121 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 122 | $this->assertCount(1, $results); |
||
| 123 | |||
| 124 | $this->assertCount(1, $results->first()->getAdminLevels()); |
||
| 125 | $this->assertEquals('TX', $results->first()->getAdminLevels()->get(1)->getCode()); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function testGeocodeWithUSIPv6() |
||
| 129 | { |
||
| 130 | $provider = new FreeGeoIp($this->getHttpClient()); |
||
| 131 | $results = $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
||
| 132 | |||
| 133 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 134 | $this->assertCount(1, $results); |
||
| 135 | |||
| 136 | $this->assertCount(1, $results->first()->getAdminLevels()); |
||
| 137 | $this->assertEquals('TX', $results->first()->getAdminLevels()->get(1)->getCode()); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function testGeocodeWithUKIPv4() |
||
| 141 | { |
||
| 142 | $provider = new FreeGeoIp($this->getHttpClient()); |
||
| 143 | $results = $provider->geocodeQuery(GeocodeQuery::create('129.67.242.154')); |
||
| 144 | |||
| 145 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 146 | $this->assertCount(1, $results); |
||
| 147 | $this->assertEquals('GB', $results->first()->getCountry()->getCode()); |
||
| 148 | |||
| 149 | $this->assertCount(1, $results->first()->getAdminLevels()); |
||
| 150 | $this->assertEquals('ENG', $results->first()->getAdminLevels()->get(1)->getCode()); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function testGeocodeWithUKIPv6() |
||
| 154 | { |
||
| 155 | $provider = new FreeGeoIp($this->getHttpClient()); |
||
| 156 | $results = $provider->geocodeQuery(GeocodeQuery::create('::ffff:129.67.242.154')); |
||
| 157 | |||
| 158 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 159 | $this->assertCount(1, $results); |
||
| 160 | $this->assertEquals('GB', $results->first()->getCountry()->getCode()); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 165 | * @expectedExceptionMessage The FreeGeoIp provider is not able to do reverse geocoding. |
||
| 166 | */ |
||
| 167 | public function testReverse() |
||
| 168 | { |
||
| 169 | $provider = new FreeGeoIp($this->getMockedHttpClient()); |
||
| 170 | $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2)); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @expectedException \Geocoder\Exception\InvalidServerResponse |
||
| 175 | */ |
||
| 176 | public function testServerEmptyResponse() |
||
| 177 | { |
||
| 178 | $provider = new FreeGeoIp($this->getMockedHttpClient()); |
||
| 179 | $provider->geocodeQuery(GeocodeQuery::create('87.227.124.53')); |
||
| 180 | } |
||
| 181 | } |
||
| 182 |