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 |
||
22 | class MaxMindBinaryTest extends BaseTestCase |
||
23 | { |
||
24 | private $binaryFile; |
||
25 | |||
26 | public function setUp() |
||
27 | { |
||
28 | $this->binaryFile = __DIR__.'/fixtures/GeoLiteCity.dat'; |
||
29 | } |
||
30 | |||
31 | protected function getCacheDir() |
||
32 | { |
||
33 | return __DIR__.'/.cached_responses'; |
||
34 | } |
||
35 | |||
36 | public static function setUpBeforeClass() |
||
37 | { |
||
38 | if (false == function_exists('geoip_open')) { |
||
39 | self::markTestSkipped('The maxmind\'s official lib required to run these tests.'); |
||
40 | } |
||
41 | |||
42 | if (false == function_exists('GeoIP_record_by_addr')) { |
||
43 | self::markTestSkipped('The maxmind\'s official lib required to run these tests.'); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | public static function provideIps() |
||
48 | { |
||
49 | return [ |
||
50 | '24.24.24.24' => ['24.24.24.24', 'East Syracuse', 'United States'], |
||
51 | '80.24.24.24' => ['80.24.24.24', 'Sabadell', 'Spain'], |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @expectedException \Geocoder\Exception\InvalidArgument |
||
57 | * @expectedExceptionMessage Given MaxMind dat file "not_exist.dat" does not exist. |
||
58 | */ |
||
59 | public function testThrowIfNotExistBinaryFileGiven() |
||
60 | { |
||
61 | new MaxMindBinary('not_exist.dat'); |
||
62 | } |
||
63 | |||
64 | public function testLocationResultContainsExpectedFieldsForAnAmericanIp() |
||
65 | { |
||
66 | $provider = new MaxMindBinary($this->binaryFile); |
||
67 | $results = $provider->geocodeQuery(GeocodeQuery::create('24.24.24.24')); |
||
68 | |||
69 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
70 | $this->assertCount(1, $results); |
||
71 | |||
72 | /** @var Location $result */ |
||
73 | $result = $results->first(); |
||
74 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
75 | |||
76 | $this->assertEquals('43.089200000000005', $result->getCoordinates()->getLatitude(), '', 0.001); |
||
77 | $this->assertEquals('-76.025000000000006', $result->getCoordinates()->getLongitude(), '', 0.001); |
||
78 | $this->assertNull($result->getBounds()); |
||
79 | $this->assertNull($result->getStreetNumber()); |
||
80 | $this->assertNull($result->getStreetName()); |
||
81 | $this->assertEquals('13057', $result->getPostalCode()); |
||
82 | $this->assertEquals('East Syracuse', $result->getLocality()); |
||
83 | $this->assertNull($result->getSubLocality()); |
||
84 | $this->assertCount(1, $result->getAdminLevels()); |
||
85 | $this->assertEquals('NY', $result->getAdminLevels()->get(1)->getName()); |
||
86 | $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
||
87 | $this->assertEquals('United States', $result->getCountry()->getName()); |
||
88 | $this->assertEquals('US', $result->getCountry()->getCode()); |
||
89 | $this->assertNull($result->getTimezone()); |
||
90 | } |
||
91 | |||
92 | public function testLocationResultContainsExpectedFieldsForASpanishIp() |
||
93 | { |
||
94 | $provider = new MaxMindBinary($this->binaryFile); |
||
95 | $results = $provider->geocodeQuery(GeocodeQuery::create('80.24.24.24')); |
||
96 | |||
97 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
98 | $this->assertCount(1, $results); |
||
99 | |||
100 | /** @var Location $result */ |
||
101 | $result = $results->first(); |
||
102 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
103 | |||
104 | $this->assertEquals('41.543299999999988', $result->getCoordinates()->getLatitude(), '', 0.001); |
||
105 | $this->assertEquals('2.1093999999999937', $result->getCoordinates()->getLongitude(), '', 0.001); |
||
106 | $this->assertNull($result->getBounds()); |
||
107 | $this->assertNull($result->getStreetNumber()); |
||
108 | $this->assertNull($result->getStreetName()); |
||
109 | $this->assertNull($result->getPostalCode()); |
||
110 | $this->assertEquals('Sabadell', $result->getLocality()); |
||
111 | $this->assertNull($result->getSubLocality()); |
||
112 | $this->assertCount(1, $result->getAdminLevels()); |
||
113 | $this->assertEquals('56', $result->getAdminLevels()->get(1)->getName()); |
||
114 | $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
||
115 | $this->assertEquals('Spain', $result->getCountry()->getName()); |
||
116 | $this->assertEquals('ES', $result->getCountry()->getCode()); |
||
117 | $this->assertNull($result->getTimezone()); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @dataProvider provideIps |
||
122 | */ |
||
123 | public function testFindLocationByIp($ip, $expectedCity, $expectedCountry) |
||
124 | { |
||
125 | $provider = new MaxMindBinary($this->binaryFile); |
||
126 | $results = $provider->geocodeQuery(GeocodeQuery::create($ip)); |
||
127 | |||
128 | $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
||
129 | $this->assertCount(1, $results); |
||
130 | |||
131 | /** @var Location $result */ |
||
132 | $result = $results->first(); |
||
133 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
134 | $this->assertEquals($expectedCity, $result->getLocality()); |
||
135 | $this->assertEquals($expectedCountry, $result->getCountry()->getName()); |
||
136 | } |
||
137 | |||
138 | public function testShouldReturnResultsAsUtf8Encoded() |
||
139 | { |
||
140 | $provider = new MaxMindBinary($this->binaryFile); |
||
141 | $results = $provider->geocodeQuery(GeocodeQuery::create('212.51.181.237')); |
||
142 | |||
143 | /** @var Location $result */ |
||
144 | $result = $results->first(); |
||
145 | $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
||
146 | $this->assertSame('Châlette-sur-loing', $result->getLocality()); |
||
147 | } |
||
148 | |||
149 | public function testGetName() |
||
150 | { |
||
151 | $provider = new MaxMindBinary($this->binaryFile); |
||
152 | |||
153 | $this->assertEquals('maxmind_binary', $provider->getName()); |
||
154 | } |
||
155 | |||
156 | public function testThrowIfIpAddressCouldNotBeLocated() |
||
157 | { |
||
158 | $provider = new MaxMindBinary($this->binaryFile); |
||
159 | $result = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
||
160 | |||
161 | $this->assertInstanceOf(Collection::class, $result); |
||
162 | $this->assertEquals(0, $result->count()); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
167 | * @expectedExceptionMessage The MaxMindBinary provider does not support IPv6 addresses. |
||
168 | */ |
||
169 | public function testThrowIfIpAddressIsNotIpV4() |
||
170 | { |
||
171 | $provider = new MaxMindBinary($this->binaryFile); |
||
172 | |||
173 | $provider->geocodeQuery(GeocodeQuery::create('2002:5018:1818:0:0:0:0:0')); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
178 | * @expectedExceptionMessage The MaxMindBinary provider does not support street addresses. |
||
179 | */ |
||
180 | public function testThrowIfInvalidIpAddressGiven() |
||
181 | { |
||
182 | $provider = new MaxMindBinary($this->binaryFile); |
||
183 | |||
184 | $provider->geocodeQuery(GeocodeQuery::create('invalidIp')); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @expectedException \Geocoder\Exception\UnsupportedOperation |
||
189 | * @expectedExceptionMessage The MaxMindBinary is not able to do reverse geocoding. |
||
190 | */ |
||
191 | public function testThrowOnReverseMethodUsage() |
||
192 | { |
||
193 | $provider = new MaxMindBinary($this->binaryFile); |
||
194 | |||
195 | $provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); |
||
196 | } |
||
197 | } |
||
198 |