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 namespace VojtaSvoboda\Reservations\Tests\Models; |
||
| 14 | class ReservationTest extends PluginTestCase |
||
| 15 | { |
||
| 16 | private $defaultStatus; |
||
| 17 | |||
| 18 | View Code Duplication | public function setUp() |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Get tested model. |
||
| 32 | * |
||
| 33 | * @return Reservation |
||
| 34 | */ |
||
| 35 | public function getModel() |
||
| 36 | { |
||
| 37 | return App::make(Reservation::class); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testBeforeCreate() |
||
| 53 | |||
| 54 | public function testIsDateAvailableFailing() |
||
| 65 | |||
| 66 | public function testIsDateAvailablePassed() |
||
| 78 | |||
| 79 | public function testIsDateAvailableForCancelled() |
||
| 98 | |||
| 99 | public function testIsCancelled() |
||
| 107 | |||
| 108 | public function testGetHash() |
||
| 109 | { |
||
| 110 | $model = $this->getModel(); |
||
| 111 | |||
| 112 | $firstHash = $model->getUniqueHash(); |
||
| 113 | $secondHash = $model->getUniqueHash(); |
||
| 114 | |||
| 115 | $this->assertNotEquals($firstHash, $secondHash); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testGetEmptyHash() |
||
| 124 | |||
| 125 | public function testGetNumber() |
||
| 126 | { |
||
| 127 | $model = $this->getModel(); |
||
| 128 | |||
| 129 | $firstNumber = $model->getUniqueNumber(); |
||
| 130 | $secondNumber = $model->getUniqueNumber(); |
||
| 131 | |||
| 132 | $this->assertNotEquals($firstNumber, $secondNumber); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testGetEmptyNumber() |
||
| 141 | |||
| 142 | public function testScopeNotCancelled() |
||
| 143 | { |
||
| 144 | $model = $this->getModel(); |
||
| 145 | |||
| 146 | // create reservation |
||
| 147 | $reservation = $model->create($this->getTestingReservationData()); |
||
| 148 | $reservations = $model->notCancelled()->get(); |
||
| 149 | $this->assertNotEmpty($reservations); |
||
| 150 | |||
| 151 | // change reservation to cancelled |
||
| 152 | $reservation->status = Status::where('ident', 'cancelled')->first(); |
||
| 153 | $reservation->save(); |
||
| 154 | $reservations = $model->notCancelled()->get(); |
||
| 155 | $this->assertEmpty($reservations); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get testing reservation data. |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | View Code Duplication | private function getTestingReservationData() |
|
| 164 | { |
||
| 165 | return [ |
||
| 166 | 'date' => Carbon::createFromFormat('Y-m-d H:i', '2016-08-18 20:00'), |
||
| 167 | 'email' => '[email protected]', |
||
| 168 | 'phone' => '777111222', |
||
| 169 | 'street' => 'ABCDE', |
||
| 170 | 'name' => 'Vojta Svoboda', |
||
| 171 | 'message' => 'Hello.', |
||
| 172 | 'status' => $this->getDefaultStatus(), |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get default status object. |
||
| 178 | * |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | private function getDefaultStatus() |
||
| 190 | } |
||
| 191 |