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 RequestTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @var Request |
||
13 | */ |
||
14 | private $request; |
||
15 | |||
16 | /** |
||
17 | * @var PreApproval |
||
18 | */ |
||
19 | private $preApproval; |
||
20 | |||
21 | protected function setUp() |
||
22 | { |
||
23 | $this->preApproval = $this->createMock(PreApproval::class); |
||
24 | $this->request = new Request($this->preApproval); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @test |
||
29 | */ |
||
30 | public function constructShouldConfigureTheAttributes() |
||
31 | { |
||
32 | $this->assertAttributeInstanceOf(PreApproval::class, 'preApproval', $this->request); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @test |
||
37 | */ |
||
38 | public function getPreApprovalShouldReturnConfiguredPreApproval() |
||
39 | { |
||
40 | $this->assertInstanceOf(PreApproval::class, $this->request->getPreApproval()); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | */ |
||
46 | public function getReferenceShouldReturnConfiguredReference() |
||
47 | { |
||
48 | $this->request->setReference('someRef'); |
||
49 | |||
50 | $this->assertEquals('someRef', $this->request->getReference()); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @test |
||
55 | */ |
||
56 | public function setReferenceShouldChangeTheAttribute() |
||
57 | { |
||
58 | $this->request->setReference('test'); |
||
59 | |||
60 | $this->assertAttributeEquals('test', 'reference', $this->request); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @test |
||
65 | */ |
||
66 | public function getRedirectToShouldReturnConfiguredRedirectTo() |
||
67 | { |
||
68 | $this->request->setRedirectTo('someRedirect'); |
||
69 | |||
70 | $this->assertEquals('someRedirect', $this->request->getRedirectTo()); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | */ |
||
76 | public function setRedirectToShouldChangeTheAttribute() |
||
77 | { |
||
78 | $this->request->setRedirectTo('otherRedirect'); |
||
79 | |||
80 | $this->assertAttributeEquals('otherRedirect', 'redirectTo', $this->request); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @test |
||
85 | */ |
||
86 | public function getReviewOnShouldReturnConfiguredReviewOn() |
||
87 | { |
||
88 | $this->request->setReviewOn('someReview'); |
||
89 | |||
90 | $this->assertEquals('someReview', $this->request->getReviewOn()); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @test |
||
95 | */ |
||
96 | public function setReviewOnToShouldChangeTheAttribute() |
||
97 | { |
||
98 | $this->request->setReviewOn('otherReview'); |
||
99 | |||
100 | $this->assertAttributeEquals('otherReview', 'reviewOn', $this->request); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @test |
||
105 | */ |
||
106 | public function getCustomerShouldReturnConfiguredCustomer() |
||
107 | { |
||
108 | $customer = $this->createMock(Customer::class); |
||
109 | $this->request->setCustomer($customer); |
||
110 | |||
111 | $this->assertSame($customer, $this->request->getCustomer()); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @test |
||
116 | */ |
||
117 | public function setCustomerToShouldChangeTheAttribute() |
||
118 | { |
||
119 | $customer = $this->createMock(Customer::class); |
||
120 | $this->request->setCustomer($customer); |
||
121 | |||
122 | $this->assertAttributeSame($customer, 'customer', $this->request); |
||
123 | } |
||
124 | } |
||
125 |