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 |
||
| 20 | class UtilCommandTest extends BaseCommandTestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @test |
||
| 24 | */ |
||
| 25 | public function processCoinReturnsCorrectResponseForNoOptions() |
||
| 26 | { |
||
| 27 | $this->initCommandWithPayload(UtilCommand::class, [ |
||
| 28 | 'user' => 'U54321', |
||
| 29 | 'text' => 'util:coin', |
||
| 30 | ]); |
||
| 31 | $result = $this->command->process(); |
||
| 32 | static::assertEquals('*Botty says:* _A complicated decision ..._', $result); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @test |
||
| 37 | */ |
||
| 38 | public function processCoinReturnsCorrectResponseForOneOption() |
||
| 39 | { |
||
| 40 | $this->initCommandWithPayload(UtilCommand::class, [ |
||
| 41 | 'user' => 'U54321', |
||
| 42 | 'text' => 'util:coin a', |
||
| 43 | ]); |
||
| 44 | $result = $this->command->process(); |
||
| 45 | static::assertEquals('*Botty says:* _A complicated decision ..._', $result); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @test |
||
| 50 | */ |
||
| 51 | public function processCoinReturnsCorrectResponseForTwoOptions() |
||
| 52 | { |
||
| 53 | $this->initCommandWithPayload(UtilCommand::class, [ |
||
| 54 | 'user' => 'U54321', |
||
| 55 | 'text' => 'util:coin a, b', |
||
| 56 | ]); |
||
| 57 | $result = $this->command->process(); |
||
| 58 | static::assertContains($result, ['*Botty says:* _a_', '*Botty says:* _b_']); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @test |
||
| 63 | */ |
||
| 64 | public function processCoinReturnsCorrectResponseForThreeOptions() |
||
| 65 | { |
||
| 66 | $this->initCommandWithPayload(UtilCommand::class, [ |
||
| 67 | 'user' => 'U54321', |
||
| 68 | 'text' => 'util:coin a, b, c', |
||
| 69 | ]); |
||
| 70 | $result = $this->command->process(); |
||
| 71 | static::assertContains($result, ['*Botty says:* _a_', '*Botty says:* _b_', '*Botty says:* _c_']); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @test |
||
| 76 | */ |
||
| 77 | public function processCoinReturnsCorrectResponseForTwoIdenticalOptions() |
||
| 78 | { |
||
| 79 | $this->initCommandWithPayload(UtilCommand::class, [ |
||
| 80 | 'user' => 'U54321', |
||
| 81 | 'text' => 'util:coin a, a', |
||
| 82 | ]); |
||
| 83 | $result = $this->command->process(); |
||
| 84 | static::assertEquals('*Botty says:* _it is undecidable ..._', $result); |
||
| 85 | } |
||
| 86 | } |
||
| 87 |