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 CancellationResponseTest extends \PHPUnit_Framework_TestCase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var CancellationResponse |
||
| 13 | */ |
||
| 14 | private $cancellationResponse; |
||
| 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->cancellationResponse = new CancellationResponse('someStatus', $this->date); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @test |
||
| 29 | */ |
||
| 30 | public function constructShouldConfigureTheAttributes() |
||
| 31 | { |
||
| 32 | $this->assertAttributeEquals('someStatus', 'status', $this->cancellationResponse); |
||
| 33 | $this->assertAttributeSame($this->date, 'date', $this->cancellationResponse); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @test |
||
| 38 | * @depends constructShouldConfigureTheAttributes |
||
| 39 | */ |
||
| 40 | public function getStatusShouldReturnTheConfiguredStatus() |
||
| 41 | { |
||
| 42 | $this->assertEquals('someStatus', $this->cancellationResponse->getStatus()); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @test |
||
| 47 | * @depends constructShouldConfigureTheAttributes |
||
| 48 | */ |
||
| 49 | public function getDateShouldReturnTheConfiredDate() |
||
| 50 | { |
||
| 51 | $this->assertSame($this->date, $this->cancellationResponse->getDate()); |
||
| 52 | } |
||
| 53 | } |
||
| 54 |