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:
Complex classes like AbstractIvoryGoogleMapExtensionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractIvoryGoogleMapExtensionTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | abstract class AbstractIvoryGoogleMapExtensionTest extends \PHPUnit_Framework_TestCase |
||
34 | { |
||
35 | /** |
||
36 | * @var ContainerBuilder |
||
37 | */ |
||
38 | private $container; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $debug; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $locale; |
||
|
|||
49 | |||
50 | /** |
||
51 | * @var HttpClient|\PHPUnit_Framework_MockObject_MockObject |
||
52 | */ |
||
53 | private $client; |
||
54 | |||
55 | /** |
||
56 | * @var MessageFactory|\PHPUnit_Framework_MockObject_MockObject |
||
57 | */ |
||
58 | private $messageFactory; |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | protected function setUp() |
||
74 | |||
75 | /** |
||
76 | * @param ContainerBuilder $container |
||
77 | * @param string $configuration |
||
78 | */ |
||
79 | abstract protected function loadConfiguration(ContainerBuilder $container, $configuration); |
||
80 | |||
81 | public function testDefaultState() |
||
118 | |||
119 | View Code Duplication | public function testTemplatingHelpers() |
|
128 | |||
129 | View Code Duplication | public function testTwigExtensions() |
|
138 | |||
139 | View Code Duplication | public function testTemplatingFormResources() |
|
149 | |||
150 | View Code Duplication | public function testTwigFormResources() |
|
160 | |||
161 | public function testFormatterDebug() |
||
168 | |||
169 | View Code Duplication | public function testMapLanguage() |
|
176 | |||
177 | View Code Duplication | public function testMapApiKey() |
|
184 | |||
185 | View Code Duplication | public function testDirection() |
|
186 | { |
||
187 | $this->loadConfiguration($this->container, 'direction'); |
||
188 | $this->container->compile(); |
||
189 | |||
190 | $direction = $this->container->get('ivory.google_map.direction'); |
||
191 | |||
192 | $this->assertInstanceOf(Direction::class, $direction); |
||
193 | $this->assertSame($this->client, $direction->getClient()); |
||
194 | $this->assertSame($this->messageFactory, $direction->getMessageFactory()); |
||
195 | $this->assertTrue($direction->isHttps()); |
||
196 | $this->assertSame(Direction::FORMAT_JSON, $direction->getFormat()); |
||
197 | $this->assertFalse($direction->hasBusinessAccount()); |
||
198 | } |
||
199 | |||
200 | public function testDirectionHttps() |
||
201 | { |
||
202 | $this->loadConfiguration($this->container, 'direction_https'); |
||
203 | $this->container->compile(); |
||
204 | |||
205 | $this->assertFalse($this->container->get('ivory.google_map.direction')->isHttps()); |
||
206 | } |
||
207 | |||
208 | public function testDirectionFormat() |
||
209 | { |
||
210 | $this->loadConfiguration($this->container, 'direction_format'); |
||
211 | $this->container->compile(); |
||
212 | |||
213 | $this->assertSame(Direction::FORMAT_XML, $this->container->get('ivory.google_map.direction')->getFormat()); |
||
214 | } |
||
215 | |||
216 | View Code Duplication | public function testDirectionApiKey() |
|
217 | { |
||
218 | $this->loadConfiguration($this->container, 'direction_api_key'); |
||
219 | $this->container->compile(); |
||
220 | |||
221 | $this->assertSame('key', $this->container->get('ivory.google_map.direction')->getKey()); |
||
222 | } |
||
223 | |||
224 | View Code Duplication | public function testDirectionBusinessAccount() |
|
225 | { |
||
226 | $this->loadConfiguration($this->container, 'direction_business_account'); |
||
227 | $this->container->compile(); |
||
228 | |||
229 | $direction = $this->container->get('ivory.google_map.direction'); |
||
230 | |||
231 | $this->assertTrue($direction->hasBusinessAccount()); |
||
232 | $this->assertSame('my-client', $direction->getBusinessAccount()->getClientId()); |
||
233 | $this->assertSame('my-secret', $direction->getBusinessAccount()->getSecret()); |
||
234 | $this->assertFalse($direction->getBusinessAccount()->hasChannel()); |
||
235 | } |
||
236 | |||
237 | View Code Duplication | public function testDirectionBusinessAccountChannel() |
|
238 | { |
||
239 | $this->loadConfiguration($this->container, 'direction_business_account_channel'); |
||
240 | $this->container->compile(); |
||
241 | |||
242 | $direction = $this->container->get('ivory.google_map.direction'); |
||
243 | |||
244 | $this->assertTrue($direction->hasBusinessAccount()); |
||
245 | $this->assertSame('my-client', $direction->getBusinessAccount()->getClientId()); |
||
246 | $this->assertSame('my-secret', $direction->getBusinessAccount()->getSecret()); |
||
247 | $this->assertSame('my-channel', $direction->getBusinessAccount()->getChannel()); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
252 | */ |
||
253 | public function testDirectionBusinessAccountInvalid() |
||
254 | { |
||
255 | $this->loadConfiguration($this->container, 'direction_business_account_invalid'); |
||
256 | $this->container->compile(); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
261 | */ |
||
262 | public function testDirectionInvalid() |
||
263 | { |
||
264 | $this->loadConfiguration($this->container, 'direction_invalid'); |
||
265 | $this->container->compile(); |
||
266 | } |
||
267 | |||
268 | View Code Duplication | public function testDistanceMatrix() |
|
282 | |||
283 | public function testDistanceMatrixHttps() |
||
290 | |||
291 | View Code Duplication | public function testDistanceMatrixFormat() |
|
301 | |||
302 | View Code Duplication | public function testDistanceMatrixApiKey() |
|
309 | |||
310 | View Code Duplication | public function testDistanceMatrixBusinessAccount() |
|
322 | |||
323 | View Code Duplication | public function testDistanceMatrixBusinessAccountChannel() |
|
335 | |||
336 | /** |
||
337 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
338 | */ |
||
339 | public function testDistanceMatrixBusinessAccountInvalid() |
||
344 | |||
345 | /** |
||
346 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
347 | */ |
||
348 | public function testDistanceMatrixInvalid() |
||
353 | |||
354 | View Code Duplication | public function testElevation() |
|
368 | |||
369 | public function testElevationHttps() |
||
376 | |||
377 | public function testElevationFormat() |
||
378 | { |
||
379 | $this->loadConfiguration($this->container, 'elevation_format'); |
||
380 | $this->container->compile(); |
||
381 | |||
382 | $this->assertSame(Elevation::FORMAT_XML, $this->container->get('ivory.google_map.elevation')->getFormat()); |
||
383 | } |
||
384 | |||
385 | View Code Duplication | public function testElevationApiKey() |
|
392 | |||
393 | View Code Duplication | public function testElevationBusinessAccount() |
|
405 | |||
406 | View Code Duplication | public function testElevationBusinessAccountChannel() |
|
418 | |||
419 | /** |
||
420 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
421 | */ |
||
422 | public function testElevationBusinessAccountInvalid() |
||
427 | |||
428 | /** |
||
429 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
430 | */ |
||
431 | public function testElevationInvalid() |
||
436 | |||
437 | View Code Duplication | public function testGeocoder() |
|
451 | |||
452 | public function testGeocoderHttps() |
||
459 | |||
460 | View Code Duplication | public function testGeocoderFormat() |
|
470 | |||
471 | View Code Duplication | public function testGeocoderApiKey() |
|
478 | |||
479 | View Code Duplication | public function testGeocoderBusinessAccount() |
|
491 | |||
492 | View Code Duplication | public function testGeocoderBusinessAccountChannel() |
|
504 | |||
505 | /** |
||
506 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
507 | */ |
||
508 | public function testGeocoderBusinessAccountInvalid() |
||
513 | |||
514 | /** |
||
515 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
516 | */ |
||
517 | public function testGeocoderInvalid() |
||
522 | |||
523 | View Code Duplication | public function testTimeZone() |
|
537 | |||
538 | /** |
||
539 | * @expectedException \InvalidArgumentException |
||
540 | * @expectedExceptionMessage The http scheme is not supported. |
||
541 | */ |
||
542 | public function testTimeZoneHttps() |
||
549 | |||
550 | View Code Duplication | public function testTimeZoneFormat() |
|
557 | |||
558 | public function testTimeZoneApiKey() |
||
559 | { |
||
560 | $this->loadConfiguration($this->container, 'time_zone_api_key'); |
||
561 | $this->container->compile(); |
||
562 | |||
563 | $this->assertSame('key', $this->container->get('ivory.google_map.time_zone')->getKey()); |
||
564 | } |
||
565 | |||
566 | public function testTimeZoneBusinessAccount() |
||
567 | { |
||
568 | $this->loadConfiguration($this->container, 'time_zone_business_account'); |
||
569 | $this->container->compile(); |
||
570 | |||
571 | $timeZone = $this->container->get('ivory.google_map.time_zone'); |
||
572 | |||
573 | $this->assertTrue($timeZone->hasBusinessAccount()); |
||
574 | $this->assertSame('my-client', $timeZone->getBusinessAccount()->getClientId()); |
||
575 | $this->assertSame('my-secret', $timeZone->getBusinessAccount()->getSecret()); |
||
576 | $this->assertFalse($timeZone->getBusinessAccount()->hasChannel()); |
||
577 | } |
||
578 | |||
579 | public function testTimeZoneBusinessAccountChannel() |
||
580 | { |
||
581 | $this->loadConfiguration($this->container, 'time_zone_business_account_channel'); |
||
582 | $this->container->compile(); |
||
583 | |||
584 | $timeZone = $this->container->get('ivory.google_map.time_zone'); |
||
585 | |||
586 | $this->assertTrue($timeZone->hasBusinessAccount()); |
||
587 | $this->assertSame('my-client', $timeZone->getBusinessAccount()->getClientId()); |
||
588 | $this->assertSame('my-secret', $timeZone->getBusinessAccount()->getSecret()); |
||
589 | $this->assertSame('my-channel', $timeZone->getBusinessAccount()->getChannel()); |
||
590 | } |
||
591 | |||
592 | /** |
||
593 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
594 | */ |
||
595 | public function testTimeZoneBusinessAccountInvalid() |
||
600 | |||
601 | /** |
||
602 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
603 | */ |
||
604 | public function testTimeZoneInvalid() |
||
609 | |||
610 | /** |
||
611 | * @expectedException \RuntimeException |
||
612 | * @expectedExceptionMessage No "class" attribute found for the tag "ivory.google_map.helper.renderer.extendable" on the service "acme.map.helper.renderer.extendable". |
||
613 | */ |
||
614 | public function testMissingExtendableRendererClassTagAttribute() |
||
619 | |||
620 | /** |
||
621 | * @return \PHPUnit_Framework_MockObject_MockObject|HttpClient |
||
622 | */ |
||
623 | private function createClientMock() |
||
627 | |||
628 | /** |
||
629 | * @return \PHPUnit_Framework_MockObject_MockObject|MessageFactory |
||
630 | */ |
||
631 | private function createMessageFactoryMock() |
||
635 | } |
||
636 |