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