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 |
||
16 | class YahooWeatherClientTests extends TestCase |
||
17 | { |
||
18 | /** @test */ |
||
19 | public function testCallApiWoeidException() |
||
20 | { |
||
21 | $service = new YahooWeatherAPI(); |
||
22 | |||
23 | $this->expectException(\Exception::class); |
||
24 | |||
25 | try { |
||
26 | $response = $service->callApiWoeid(null); |
||
27 | } catch (\Exception $e) { |
||
28 | throw $e; |
||
29 | } |
||
30 | } |
||
31 | |||
32 | /** @test */ |
||
33 | public function testCallApiCityNameException() |
||
34 | { |
||
35 | $service = new YahooWeatherAPI(); |
||
36 | |||
37 | $this->expectException(\Exception::class); |
||
38 | |||
39 | try { |
||
40 | $response = $service->callApiCityName(null); |
||
41 | } catch (\Exception $e) { |
||
42 | throw $e; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** @test */ |
||
47 | public function testCallApiTestException() |
||
48 | { |
||
49 | $service = new YahooWeatherAPI(); |
||
50 | |||
51 | $this->expectException(\Exception::class); |
||
52 | |||
53 | try { |
||
54 | $response = $service->callApi(null); |
||
55 | } catch (\Exception $e) { |
||
56 | throw $e; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** @test */ |
||
61 | public function testCallApiLastRes() |
||
62 | { |
||
63 | $errYql = 'https://query.yahooapis.com/v1/public/yql?q=from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text=%22Taipei%22)&format=json&env=store://datatables.org/alltableswithkeys'; |
||
64 | |||
65 | $service = new YahooWeatherAPI(); |
||
66 | |||
67 | $this->expectException(\Exception::class); |
||
68 | |||
69 | try { |
||
70 | $response = $service->callApi($errYql); |
||
71 | } catch (\Exception $e) { |
||
72 | throw $e; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** @test */ |
||
77 | public function setClientTest() |
||
78 | { |
||
79 | $service = new YahooWeatherAPI(); |
||
80 | $response = $service->setClient(new Client()); |
||
81 | $this->assertSame($response, null); |
||
82 | } |
||
83 | |||
84 | /** @test */ |
||
85 | public function setLastResTest() |
||
86 | { |
||
87 | $service = new YahooWeatherAPI(); |
||
88 | |||
89 | $response = $service->setLastResponse(null); |
||
90 | $this->assertSame($response, null); |
||
91 | } |
||
92 | |||
93 | /** @test */ |
||
94 | public function callApiWoeidTest() |
||
95 | { |
||
96 | $service = new YahooWeatherAPI(); |
||
97 | $response = $service->callApiWoeid(1232345678); |
||
98 | $this->assertSame($response, false); |
||
99 | |||
100 | $response = $service->callApiWoeid(2306179); |
||
101 | $city = str_replace(' ', '', $response['location']['city']); |
||
102 | $country = str_replace(' ', '', $response['location']['country']); |
||
103 | $region = str_replace(' ', '', $response['location']['region']); |
||
104 | $this->assertSame($city, 'TaipeiCity'); |
||
105 | $this->assertSame($country, 'Taiwan'); |
||
106 | $this->assertSame($region, 'TaipeiCity'); |
||
107 | |||
108 | $woeid = 2306179; |
||
109 | $response = $this->getWoeidTest($service); |
||
110 | $this->assertSame($response, $woeid); |
||
111 | } |
||
112 | |||
113 | /** @test */ |
||
114 | public function callApiCityNameTest() |
||
115 | { |
||
116 | $service = new YahooWeatherAPI(); |
||
117 | $response = $service->callApiCityName('Taipei'); |
||
118 | $city = str_replace(' ', '', $response['location']['city']); |
||
119 | $country = str_replace(' ', '', $response['location']['country']); |
||
120 | $region = str_replace(' ', '', $response['location']['region']); |
||
121 | $this->assertSame($city, 'TaipeiCity'); |
||
122 | $this->assertSame($country, 'Taiwan'); |
||
123 | $this->assertSame($region, 'TaipeiCity'); |
||
124 | |||
125 | $city = 'Taipei'; |
||
126 | $response = $this->getCityTest($service); |
||
127 | $this->assertSame($response, $city); |
||
128 | } |
||
129 | |||
130 | /** @test */ |
||
131 | public function callApiTest() |
||
132 | { |
||
133 | $service = new YahooWeatherAPI(); |
||
134 | $yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="123456789")'; |
||
135 | $url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys'; |
||
136 | $url = $this->encodeURI($url); |
||
137 | $response = $service->callApi($url); |
||
138 | $this->assertSame($response, false); |
||
139 | |||
140 | $yql = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="Taipei")'; |
||
141 | $url = 'https://query.yahooapis.com/v1/public/yql?q='.$yql.'&format=json&env=store://datatables.org/alltableswithkeys'; |
||
142 | $url = $this->encodeURI($url); |
||
143 | $response = $service->callApi($url); |
||
144 | $city = str_replace(' ', '', $response['location']['city']); |
||
145 | $country = str_replace(' ', '', $response['location']['country']); |
||
146 | $region = str_replace(' ', '', $response['location']['region']); |
||
147 | $this->assertSame($city, 'TaipeiCity'); |
||
148 | $this->assertSame($country, 'Taiwan'); |
||
149 | $this->assertSame($region, 'TaipeiCity'); |
||
150 | |||
151 | $response = $this->getYqlTest($service); |
||
152 | $this->assertSame($response, $url); |
||
153 | } |
||
154 | |||
155 | /** @test */ |
||
156 | public function getLastResTest() |
||
157 | { |
||
158 | $service = new YahooWeatherAPI(); |
||
159 | |||
160 | $service->setLastResponse($this->getResponseData()); |
||
161 | |||
162 | $response = $service->getLastResponse(false); |
||
163 | $this->assertSame(is_array($response), true); |
||
164 | |||
165 | $response = $service->getLastResponse(true); |
||
166 | $this->assertSame(is_array($response), false); |
||
167 | } |
||
168 | |||
169 | /** @test */ |
||
170 | public function getTemperatureTest() |
||
171 | { |
||
172 | $service = new YahooWeatherAPI(); |
||
173 | $response = $this->getResponseData(); |
||
174 | $storeRes = $response; |
||
175 | |||
176 | $service->setLastResponse(null); |
||
177 | $service->getLastResponse(true); |
||
178 | $response = $service->getTemperature(true); |
||
179 | $this->assertSame($response, ''); |
||
180 | |||
181 | $noTemp = $storeRes; |
||
182 | $noTemp['item']['condition']['temp'] = null; |
||
183 | $service->setLastResponse($noTemp); |
||
184 | $response = $service->getTemperature(false); |
||
185 | $this->assertSame($response, ''); |
||
186 | |||
187 | $service->setLastResponse($storeRes); |
||
188 | $response = $service->getTemperature(true); |
||
189 | $this->assertSame(is_string($response), true); |
||
190 | |||
191 | $response = $service->getTemperature(false); |
||
192 | $this->assertSame(is_string((int) $response), false); |
||
193 | } |
||
194 | |||
195 | /** @test */ |
||
196 | public function getLocationTest() |
||
197 | { |
||
198 | $service = new YahooWeatherAPI(); |
||
199 | $response = $this->getResponseData(); |
||
200 | $storeRes = $response; |
||
201 | |||
202 | $service->setLastResponse(null); |
||
203 | $response = $service->getLocation(); |
||
204 | $this->assertSame($response, ''); |
||
205 | |||
206 | $noCity = $storeRes; |
||
207 | $noCity['location']['city'] = null; |
||
208 | $service->setLastResponse($noCity); |
||
209 | $response = $service->getLocation(); |
||
210 | $this->assertSame($response, ''); |
||
211 | |||
212 | |||
213 | $service->setLastResponse($storeRes); |
||
214 | $response = $service->getLocation(); |
||
215 | $city = str_replace(' ', '', $response); |
||
216 | $this->assertSame($city, 'TaipeiCity'); |
||
217 | } |
||
218 | |||
219 | /** @test */ |
||
220 | public function getForecastTest() |
||
221 | { |
||
222 | $service = new YahooWeatherAPI(); |
||
223 | $response = $this->getResponseData(); |
||
224 | $storeRes = $response; |
||
225 | |||
226 | $service->setLastResponse(null); |
||
227 | $response = $service->getForecast(); |
||
228 | $this->assertSame(count($response), 0); |
||
229 | |||
230 | $noForecast = $storeRes; |
||
231 | $noForecast['item']['forecast'] = null; |
||
232 | $service->setLastResponse($noForecast); |
||
233 | $response = $service->getForecast(); |
||
234 | $this->assertSame(count($response), 0); |
||
235 | |||
236 | $service->setLastResponse($storeRes); |
||
237 | $response = $service->getForecast(); |
||
238 | $this->assertSame(count($response), 10); |
||
239 | } |
||
240 | |||
241 | /** @test */ |
||
242 | public function getWindTest() |
||
243 | { |
||
244 | $service = new YahooWeatherAPI(); |
||
245 | $response = $this->getResponseData(); |
||
246 | $storeRes = $response; |
||
247 | |||
248 | $service->setLastResponse(null); |
||
249 | $response = $service->getWind(true); |
||
250 | $this->assertSame(count($response), 0); |
||
251 | |||
252 | $noSpeed = $storeRes; |
||
253 | $noSpeed['wind']['speed'] = null; |
||
254 | $service->setLastResponse($noSpeed); |
||
255 | $response = $service->getWind(true); |
||
256 | $this->assertSame(count($response), 0); |
||
257 | |||
258 | $service->setLastResponse($storeRes); |
||
259 | $expectRes = array( |
||
260 | 'chill' => $storeRes['wind']['chill'], |
||
261 | 'direction' => $storeRes['wind']['direction'], |
||
262 | 'speed' => $storeRes['wind']['speed'], |
||
263 | ); |
||
264 | |||
265 | $response = $service->getWind(true); |
||
266 | $expectRes['speed'] .= ' '.$storeRes['units']['speed']; |
||
267 | $this->assertSame($response['speed'], $expectRes['speed']); |
||
268 | |||
269 | $expectRes = array( |
||
270 | 'chill' => $storeRes['wind']['chill'], |
||
271 | 'direction' => $storeRes['wind']['direction'], |
||
272 | 'speed' => $storeRes['wind']['speed'], |
||
273 | ); |
||
274 | |||
275 | $response = $service->getWind(false); |
||
276 | $this->assertSame($response['speed'], $expectRes['speed']); |
||
277 | } |
||
278 | |||
279 | public function getWoeidTest(YahooWeatherAPI $service) |
||
280 | { |
||
281 | return $service->getWoeid(); |
||
282 | } |
||
283 | |||
284 | public function getCityTest(YahooWeatherAPI $service) |
||
285 | { |
||
286 | return $service->getCity(); |
||
287 | } |
||
288 | |||
289 | public function getYqlTest(YahooWeatherAPI $service) |
||
290 | { |
||
291 | return $service->getYql(); |
||
292 | } |
||
293 | |||
294 | public function encodeURI($url) |
||
295 | { |
||
296 | $unescaped = array( |
||
297 | '%2D' => '-', '%5F' => '_', '%2E' => '.', '%21' => '!', '%7E' => '~', |
||
298 | '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')', |
||
299 | ); |
||
300 | $reserved = array( |
||
301 | '%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':', |
||
302 | '%40' => '@', '%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$', |
||
303 | ); |
||
304 | $score = array( |
||
305 | '%23' => '#', |
||
306 | ); |
||
307 | |||
308 | return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score)); |
||
309 | } |
||
310 | |||
311 | public function getResponseData() |
||
312 | { |
||
313 | $response = file_get_contents('tests/response.json'); |
||
314 | $response = json_decode($response, true); |
||
315 | |||
316 | return $response; |
||
317 | } |
||
318 | } |
||
319 |