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 |
||
| 20 | class GeoPluginTest extends BaseTestCase |
||
| 21 | { |
||
| 22 | protected function getCacheDir() |
||
| 23 | { |
||
| 24 | return __DIR__.'/.cached_responses'; |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testgetName() |
||
| 28 | { |
||
| 29 | $provider = new GeoPlugin($this->getMockedHttpClient()); |
||
| 30 | $this->assertEquals('geo_plugin', $provider->getName()); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 35 | * @expectedExceptionMessage The GeoPlugin provider does not support street addresses, only IP addresses. |
||
| 36 | */ |
||
| 37 | public function testGeocodeWithAddress() |
||
| 38 | { |
||
| 39 | $provider = new GeoPlugin($this->getMockedHttpClient()); |
||
| 40 | $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testGeocodeWithLocalhostIPv4() |
||
| 44 | { |
||
| 45 | $provider = new GeoPlugin($this->getMockedHttpClient()); |
||
| 46 | $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
||
| 47 | |||
| 48 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 49 | $this->assertCount(1, $results); |
||
| 50 | |||
| 51 | $result = $results->first(); |
||
| 52 | $this->assertEquals('localhost', $result->getLocality()); |
||
| 53 | $this->assertEquals('localhost', $result->getCountry()->getName()); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function testGeocodeWithLocalhostIPv6() |
||
| 57 | { |
||
| 58 | $provider = new GeoPlugin($this->getMockedHttpClient()); |
||
| 59 | $results = $provider->geocodeQuery(GeocodeQuery::create('::1')); |
||
| 60 | |||
| 61 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 62 | $this->assertCount(1, $results); |
||
| 63 | |||
| 64 | $result = $results->first(); |
||
| 65 | $this->assertEquals('localhost', $result->getLocality()); |
||
| 66 | $this->assertEquals('localhost', $result->getCountry()->getName()); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testGeocodeWithRealIPv4() |
||
| 70 | { |
||
| 71 | $provider = new GeoPlugin($this->getHttpClient()); |
||
| 72 | $results = $provider->geocodeQuery(GeocodeQuery::create('66.147.244.214')); |
||
| 73 | |||
| 74 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
| 75 | $this->assertCount(1, $results); |
||
| 76 | |||
| 77 | $result = $results->first(); |
||
| 78 | |||
| 79 | $this->assertEquals(40.711101999999997, $result->getCoordinates()->getLatitude(), '', 0.0001); |
||
| 80 | $this->assertEquals(-73.946899000000002, $result->getCoordinates()->getLongitude(), '', 0.0001); |
||
| 81 | $this->assertNull($result->getLocality()); |
||
| 82 | $this->assertCount(1, $result->getAdminLevels()); |
||
| 83 | $this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName()); |
||
| 84 | $this->assertEquals('NY', $result->getAdminLevels()->get(1)->getCode()); |
||
| 85 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
| 86 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
| 91 | * @expectedExceptionMessage The GeoPlugin provider is not able to do reverse geocoding. |
||
| 92 | */ |
||
| 93 | public function testReverse() |
||
| 94 | { |
||
| 95 | $provider = new GeoPlugin($this->getMockedHttpClient()); |
||
| 96 | $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2)); |
||
| 97 | } |
||
| 98 | } |
||
| 99 |