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 |
||
9 | class YoutubeProviderTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | public function testParseProviderReference() |
||
12 | { |
||
13 | $youtube = new YouTubeProvider(); |
||
14 | $this->assertEquals('uN2nqdRLhQ8', $youtube->parseProviderReference('https://www.youtube.com/watch?v=uN2nqdRLhQ8')); |
||
15 | $this->assertEquals('uN2nqdRLhQ8', $youtube->parseProviderReference('https://www.youtube.com/watch?v=uN2nqdRLhQ8&foo=bar')); |
||
16 | $this->assertEquals('uN2nqdRLhQ8', $youtube->parseProviderReference('https://youtu.be/uN2nqdRLhQ8')); |
||
17 | $this->assertEquals('uN2nqdRLhQ8', $youtube->parseProviderReference('https://youtu.be/uN2nqdRLhQ8?foo=bar')); |
||
18 | $this->assertEquals('uN2nqdRLhQ8', $youtube->parseProviderReference('uN2nqdRLhQ8')); |
||
19 | } |
||
20 | |||
21 | public function testParseInvalidProviderReference() |
||
22 | { |
||
23 | $this->setExpectedException(InvalidProviderUrlException::class); |
||
24 | $youtube = new YouTubeProvider(); |
||
25 | $youtube->parseProviderReference('https://www.youtube.com/watch?foo=bar'); |
||
26 | } |
||
27 | |||
28 | public function testParseInvalidProviderReference2() |
||
29 | { |
||
30 | $this->setExpectedException(InvalidProviderUrlException::class); |
||
31 | $youtube = new YouTubeProvider(); |
||
32 | $youtube->parseProviderReference('https://www.youtube.com'); |
||
33 | } |
||
34 | |||
35 | public function testParseInvalidProviderReferenceShort() |
||
36 | { |
||
37 | $this->setExpectedException(InvalidProviderUrlException::class); |
||
38 | $youtube = new YouTubeProvider(); |
||
39 | $youtube->parseProviderReference('https://youtu.be'); |
||
40 | } |
||
41 | |||
42 | public function testSupports() |
||
43 | { |
||
44 | $provider = new YouTubeProvider(); |
||
45 | $this->assertFalse($provider->supports(AbstractProvider::SUPPORT_DOWNLOAD)); |
||
46 | $this->assertTrue($provider->supports(AbstractProvider::SUPPORT_EMBED)); |
||
47 | $this->assertTrue($provider->supports(AbstractProvider::SUPPORT_IMAGE)); |
||
48 | $this->assertFalse($provider->supports('foo')); |
||
49 | } |
||
50 | } |
||
51 |