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 CredentialsTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @var Environment |
||
13 | */ |
||
14 | private $environment; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | protected function setUp() |
||
20 | { |
||
21 | $this->environment = $this->getMockForAbstractClass(Environment::class); |
||
22 | |||
23 | $this->environment->expects($this->any()) |
||
24 | ->method('getHost') |
||
25 | ->willReturn('test.com'); |
||
26 | |||
27 | $this->environment->expects($this->any()) |
||
28 | ->method('getWsHost') |
||
29 | ->willReturn('ws.test.com'); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @test |
||
34 | */ |
||
35 | public function constructShouldConfigureTheAttributes() |
||
36 | { |
||
37 | $credentials = new Credentials('[email protected]', 'testing', $this->environment); |
||
38 | |||
39 | $this->assertAttributeEquals('[email protected]', 'email', $credentials); |
||
40 | $this->assertAttributeEquals('testing', 'token', $credentials); |
||
41 | $this->assertAttributeSame($this->environment, 'environment', $credentials); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @test |
||
46 | */ |
||
47 | public function constructShouldTruncateEmailAndToken() |
||
48 | { |
||
49 | $credentials = new Credentials(str_repeat('a', 80), str_repeat('a', 40), $this->environment); |
||
50 | |||
51 | $this->assertAttributeEquals(str_repeat('a', 60), 'email', $credentials); |
||
52 | $this->assertAttributeEquals(str_repeat('a', 32), 'token', $credentials); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @test |
||
57 | */ |
||
58 | public function constructShouldUseProductionAsDefaultEnvironment() |
||
59 | { |
||
60 | $credentials = new Credentials('[email protected]', 'testing'); |
||
61 | |||
62 | $this->assertAttributeInstanceOf(Production::class, 'environment', $credentials); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @test |
||
67 | */ |
||
68 | public function getUrlShouldGetTheUrlFromTheEnvironment() |
||
69 | { |
||
70 | $credentials = new Credentials( |
||
71 | '[email protected]', |
||
72 | 'testing', |
||
73 | $this->environment |
||
74 | ); |
||
75 | |||
76 | $this->assertEquals('https://test.com/test', $credentials->getUrl('/test')); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @test |
||
81 | */ |
||
82 | public function getWsUrlShouldGetTheWsUrlFromTheEnvironmentAppendingEmailAndTokenAsGetParams() |
||
83 | { |
||
84 | $credentials = new Credentials( |
||
85 | '[email protected]', |
||
86 | 'testing', |
||
87 | $this->environment |
||
88 | ); |
||
89 | |||
90 | $this->assertEquals( |
||
91 | 'https://ws.test.com/test?page=1&email=contato%40phpsc.com.br&token=testing', |
||
92 | $credentials->getWsUrl('/test', ['page' => '1']) |
||
93 | ); |
||
94 | } |
||
95 | } |
||
96 |