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 |
||
| 25 | class MapQuestTest extends BaseTestCase |
||
| 26 | { |
||
| 27 | protected function getCacheDir() |
||
| 28 | { |
||
| 29 | return __DIR__.'/.cached_responses'; |
||
| 30 | } |
||
| 31 | |||
| 32 | public function testGetName() |
||
| 33 | { |
||
| 34 | $provider = new MapQuest($this->getMockedHttpClient(), 'api_key'); |
||
| 35 | $this->assertEquals('map_quest', $provider->getName()); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function testGeocode() |
||
| 39 | { |
||
| 40 | $provider = new MapQuest($this->getMockedHttpClient('{}'), 'api_key'); |
||
| 41 | $result = $provider->geocodeQuery(GeocodeQuery::create('foobar')); |
||
| 42 | |||
| 43 | $this->assertInstanceOf(Collection::class, $result); |
||
| 44 | $this->assertEquals(0, $result->count()); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testGetNotRelevantData() |
||
| 48 | { |
||
| 49 | $json = '{"results":[{"locations":[{"street":"","postalCode":"","adminArea5":"","adminArea4":"","adminArea3":"","adminArea1":""}]}]}'; |
||
| 50 | |||
| 51 | $provider = new MapQuest($this->getMockedHttpClient($json), 'api_key'); |
||
| 52 | $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(11, 12)); |
||
| 53 | |||
| 54 | $this->assertInstanceOf(Collection::class, $result); |
||
| 55 | $this->assertEquals(0, $result->count()); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testGeocodeWithRealAddress() |
||
| 59 | { |
||
| 60 | if (!isset($_SERVER['MAPQUEST_API_KEY'])) { |
||
| 61 | $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml'); |
||
| 62 | } |
||
| 63 | |||
| 64 | $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']); |
||
| 65 | $results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
||
| 66 | |||
| 67 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 68 | $this->assertCount(1, $results); |
||
| 69 | |||
| 70 | /** @var Location $result */ |
||
| 71 | $result = $results->first(); |
||
| 72 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 73 | $this->assertEquals(48.866205, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 74 | $this->assertEquals(2.389089, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 75 | $this->assertEquals('10 Avenue Gambetta', $result->getStreetName()); |
||
| 76 | $this->assertEquals(75020, $result->getPostalCode()); |
||
| 77 | $this->assertEquals('Paris', $result->getLocality()); |
||
| 78 | $this->assertCount(2, $result->getAdminLevels()); |
||
| 79 | $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName()); |
||
| 80 | $this->assertEquals('Ile-de-France', $result->getAdminLevels()->get(1)->getName()); |
||
| 81 | $this->assertEquals('FR', $result->getCountry()->getName()); |
||
| 82 | $this->assertEquals('FR', $result->getCountry()->getCode()); |
||
| 83 | |||
| 84 | $this->assertNull($result->getBounds()); |
||
| 85 | $this->assertNull($result->getStreetNumber()); |
||
| 86 | $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
||
| 87 | $this->assertNull($result->getTimezone()); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testReverseWithRealCoordinates() |
||
| 91 | { |
||
| 92 | if (!isset($_SERVER['MAPQUEST_API_KEY'])) { |
||
| 93 | $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml'); |
||
| 94 | } |
||
| 95 | |||
| 96 | $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']); |
||
| 97 | $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345)); |
||
| 98 | |||
| 99 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 100 | $this->assertCount(1, $results); |
||
| 101 | |||
| 102 | /** @var Location $result */ |
||
| 103 | $result = $results->first(); |
||
| 104 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 105 | $this->assertEquals(54.0484068, $result->getCoordinates()->getLatitude(), '', 0.001); |
||
| 106 | $this->assertEquals(-2.7990345, $result->getCoordinates()->getLongitude(), '', 0.001); |
||
| 107 | $this->assertEquals('Collegian W.M.C.', $result->getStreetName()); |
||
| 108 | $this->assertEquals('LA1 1NP', $result->getPostalCode()); |
||
| 109 | $this->assertEquals('Lancaster', $result->getLocality()); |
||
| 110 | $this->assertCount(1, $result->getAdminLevels()); |
||
| 111 | $this->assertEquals('England', $result->getAdminLevels()->get(1)->getName()); |
||
| 112 | $this->assertEquals('GB', $result->getCountry()->getName()); |
||
| 113 | $this->assertEquals('GB', $result->getCountry()->getCode()); |
||
| 114 | |||
| 115 | $this->assertNull($result->getBounds()); |
||
| 116 | $this->assertNull($result->getStreetNumber()); |
||
| 117 | $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
||
| 118 | $this->assertNull($result->getTimezone()); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testGeocodeWithCity() |
||
| 122 | { |
||
| 123 | if (!isset($_SERVER['MAPQUEST_API_KEY'])) { |
||
| 124 | $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml'); |
||
| 125 | } |
||
| 126 | |||
| 127 | $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']); |
||
| 128 | $results = $provider->geocodeQuery(GeocodeQuery::create('Hanover')); |
||
| 129 | |||
| 130 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 131 | $this->assertCount(5, $results); |
||
| 132 | |||
| 133 | /** @var Location $result */ |
||
| 134 | $result = $results->first(); |
||
| 135 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 136 | $this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 137 | $this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 138 | $this->assertEquals('Hanover', $result->getLocality()); |
||
| 139 | $this->assertCount(2, $result->getAdminLevels()); |
||
| 140 | $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); |
||
| 141 | $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName()); |
||
| 142 | $this->assertEquals('DE', $result->getCountry()->getName()); |
||
| 143 | $this->assertEquals('DE', $result->getCountry()->getCode()); |
||
| 144 | |||
| 145 | /** @var Location $result */ |
||
| 146 | $result = $results->first(); |
||
| 147 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 148 | $this->assertEquals(52.374478000000003, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 149 | $this->assertEquals(9.7385529999999996, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 150 | $this->assertEquals('Hanover', $result->getLocality()); |
||
| 151 | $this->assertCount(2, $result->getAdminLevels()); |
||
| 152 | $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); |
||
| 153 | $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName()); |
||
| 154 | $this->assertEquals('DE', $result->getCountry()->getName()); |
||
| 155 | $this->assertEquals('DE', $result->getCountry()->getCode()); |
||
| 156 | |||
| 157 | /** @var Location $result */ |
||
| 158 | $result = $results->first(); |
||
| 159 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 160 | $this->assertEquals(52.374478000000003, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 161 | $this->assertEquals(9.7385529999999996, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 162 | $this->assertEquals('Hanover', $result->getLocality()); |
||
| 163 | $this->assertCount(2, $result->getAdminLevels()); |
||
| 164 | $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); |
||
| 165 | $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName()); |
||
| 166 | $this->assertEquals('DE', $result->getCountry()->getName()); |
||
| 167 | $this->assertEquals('DE', $result->getCountry()->getCode()); |
||
| 168 | |||
| 169 | /** @var Location $result */ |
||
| 170 | $result = $results->first(); |
||
| 171 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 172 | $this->assertEquals(52.374478000000003, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 173 | $this->assertEquals(9.7385529999999996, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 174 | $this->assertEquals('Hanover', $result->getLocality()); |
||
| 175 | $this->assertCount(2, $result->getAdminLevels()); |
||
| 176 | $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); |
||
| 177 | $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName()); |
||
| 178 | $this->assertEquals('DE', $result->getCountry()->getName()); |
||
| 179 | $this->assertEquals('DE', $result->getCountry()->getCode()); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function testGeocodeWithCityDistrict() |
||
| 183 | { |
||
| 184 | if (!isset($_SERVER['MAPQUEST_API_KEY'])) { |
||
| 185 | $this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml'); |
||
| 186 | } |
||
| 187 | |||
| 188 | $provider = new MapQuest($this->getHttpClient($_SERVER['MAPQUEST_API_KEY']), $_SERVER['MAPQUEST_API_KEY']); |
||
| 189 | $results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany')); |
||
| 190 | |||
| 191 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 192 | $this->assertCount(1, $results); |
||
| 193 | |||
| 194 | /** @var Location $result */ |
||
| 195 | $result = $results->first(); |
||
| 196 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
| 197 | $this->assertEquals(50.189062, $result->getCoordinates()->getLatitude(), '', 0.01); |
||
| 198 | $this->assertEquals(8.636567, $result->getCoordinates()->getLongitude(), '', 0.01); |
||
| 199 | $this->assertEquals('Kalbacher Hauptstraße 10', $result->getStreetName()); |
||
| 200 | $this->assertEquals(60437, $result->getPostalCode()); |
||
| 201 | $this->assertEquals('Frankfurt', $result->getLocality()); |
||
| 202 | $this->assertCount(1, $result->getAdminLevels()); |
||
| 203 | $this->assertEquals('Hesse', $result->getAdminLevels()->get(1)->getName()); |
||
| 204 | $this->assertEquals('DE', $result->getCountry()->getName()); |
||
| 205 | $this->assertEquals('DE', $result->getCountry()->getCode()); |
||
| 206 | |||
| 207 | $this->assertNull($result->getBounds()); |
||
| 208 | $this->assertNull($result->getStreetNumber()); |
||
| 209 | $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
||
| 210 | $this->assertNull($result->getTimezone()); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 215 | * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. |
||
| 216 | */ |
||
| 217 | public function testGeocodeWithLocalhostIPv4() |
||
| 218 | { |
||
| 219 | $provider = new MapQuest($this->getMockedHttpClient(), 'api_key'); |
||
| 220 | $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 225 | * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. |
||
| 226 | */ |
||
| 227 | public function testGeocodeWithLocalhostIPv6() |
||
| 228 | { |
||
| 229 | $provider = new MapQuest($this->getMockedHttpClient(), 'api_key'); |
||
| 230 | $provider->geocodeQuery(GeocodeQuery::create('::1')); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 235 | * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. |
||
| 236 | */ |
||
| 237 | public function testGeocodeWithRealIPv4() |
||
| 238 | { |
||
| 239 | $provider = new MapQuest($this->getMockedHttpClient(), 'api_key'); |
||
| 240 | $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 245 | * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. |
||
| 246 | */ |
||
| 247 | public function testGeocodeWithRealIPv6() |
||
| 248 | { |
||
| 249 | $provider = new MapQuest($this->getMockedHttpClient(), 'api_key'); |
||
| 250 | $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
||
| 251 | } |
||
| 252 | } |
||
| 253 |