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 ChargeResponseTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * @var ChargeResponse |
||
13 | */ |
||
14 | private $chargeResponse; |
||
15 | |||
16 | /** |
||
17 | * @var DateTime |
||
18 | */ |
||
19 | private $date; |
||
20 | |||
21 | public function setUp() |
||
22 | { |
||
23 | $this->date = new DateTime('2015-01-01'); |
||
24 | $this->chargeResponse = new ChargeResponse('someTransactionCode', $this->date); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @test |
||
29 | */ |
||
30 | public function constructShouldConfigureTheAttributes() |
||
31 | { |
||
32 | $this->assertAttributeEquals('someTransactionCode', 'transactionCode', $this->chargeResponse); |
||
33 | $this->assertAttributeSame($this->date, 'date', $this->chargeResponse); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @test |
||
38 | * @depends constructShouldConfigureTheAttributes |
||
39 | */ |
||
40 | public function getTransactionCodeShouldReturnTheConfiguredTransactionCode() |
||
41 | { |
||
42 | $this->assertEquals('someTransactionCode', $this->chargeResponse->getTransactionCode()); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @test |
||
47 | * @depends constructShouldConfigureTheAttributes |
||
48 | */ |
||
49 | public function getDateShouldReturnTheConfiredDate() |
||
50 | { |
||
51 | $this->assertSame($this->date, $this->chargeResponse->getDate()); |
||
52 | } |
||
53 | } |
||
54 |