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 RoundTimeValidatorTest extends TestCase { |
||
| 10 | function setUp() { |
||
| 11 | $this->app = require dirname(__FILE__).'/../../../../api/config/_app.php'; |
||
| 12 | $this->roundTimeCreateModel = new RoundTimeCreateModel(); |
||
| 13 | $this->sut = new RoundTimeValidator($this->app); |
||
| 14 | } |
||
| 15 | |||
| 16 | public function testPassesValidationIfTimersAreDisabled() { |
||
| 17 | $this->roundTimeCreateModel->isRoundTimesEnabled = false; |
||
| 18 | |||
| 19 | $result = $this->sut->AreRoundTimesValid($this->roundTimeCreateModel); |
||
| 20 | |||
| 21 | $this->assertTrue($result->success); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function testDraftIdAndSecondsArePresent() { |
||
| 25 | $this->roundTimeCreateModel->isRoundTimesEnabled = true; |
||
| 26 | |||
| 27 | $roundTime1 = new RoundTime(); |
||
| 28 | $roundTime1->draft_id = 1; |
||
| 29 | $roundTime1->is_static_time = false; |
||
| 30 | $roundTime1->draft_round = 1; |
||
| 31 | |||
| 32 | $roundTime2 = new RoundTime(); |
||
| 33 | $roundTime2->round_time_seconds = 7; |
||
| 34 | $roundTime2->is_static_time = false; |
||
| 35 | $roundTime2->draft_round = 1; |
||
| 36 | |||
| 37 | $this->roundTimeCreateModel->roundTimes[] = $roundTime1; |
||
| 38 | $this->roundTimeCreateModel->roundTimes[] = $roundTime2; |
||
| 39 | |||
| 40 | $result = $this->sut->AreRoundTimesValid($this->roundTimeCreateModel); |
||
| 41 | |||
| 42 | $this->assertFalse($result->success); |
||
| 43 | $this->assertContains("Round time #2 has one or more missing fields.", $result->errors); |
||
| 44 | $this->assertContains("Round time #1 has one or more missing fields.", $result->errors); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testRoundTimeSecondsArePositiveNumbers() { |
||
| 48 | $this->roundTimeCreateModel->isRoundTimesEnabled = true; |
||
| 49 | |||
| 50 | $roundTime1 = new RoundTime(); |
||
| 51 | $roundTime1->draft_id = 1; |
||
| 52 | $roundTime1->round_time_seconds = -5; |
||
| 53 | $roundTime1->is_static_time = false; |
||
| 54 | $roundTime1->draft_round = 1; |
||
| 55 | |||
| 56 | $roundTime2 = new RoundTime(); |
||
| 57 | $roundTime2->draft_id = 1; |
||
| 58 | $roundTime2->round_time_seconds = "q"; |
||
| 59 | $roundTime2->is_static_time = false; |
||
| 60 | $roundTime2->draft_round = 1; |
||
| 61 | |||
| 62 | $this->roundTimeCreateModel->roundTimes[] = $roundTime1; |
||
| 63 | $this->roundTimeCreateModel->roundTimes[] = $roundTime2; |
||
| 64 | |||
| 65 | $result = $this->sut->AreRoundTimesValid($this->roundTimeCreateModel); |
||
| 66 | |||
| 67 | $this->assertFalse($result->success); |
||
| 68 | $this->assertContains("Round time #1 must have 1 or more seconds specified.", $result->errors); |
||
| 69 | $this->assertContains("Round time #2 must have 1 or more seconds specified.", $result->errors); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function testEnsureRoundNumberIsCorrect() { |
||
| 73 | $this->roundTimeCreateModel->isRoundTimesEnabled = true; |
||
| 74 | |||
| 75 | $roundTime1 = new RoundTime(); |
||
| 76 | $roundTime1->draft_id = 1; |
||
| 77 | $roundTime1->round_time_seconds = -5; |
||
| 78 | $roundTime1->is_static_time = false; |
||
| 79 | $roundTime1->draft_round = 0; |
||
| 80 | |||
| 81 | $roundTime2 = new RoundTime(); |
||
| 82 | $roundTime2->draft_id = 1; |
||
| 83 | $roundTime2->round_time_seconds = "q"; |
||
| 84 | $roundTime2->is_static_time = false; |
||
| 85 | $roundTime2->draft_round = 31; |
||
| 86 | |||
| 87 | $this->roundTimeCreateModel->roundTimes[] = $roundTime1; |
||
| 88 | $this->roundTimeCreateModel->roundTimes[] = $roundTime2; |
||
| 89 | |||
| 90 | $result = $this->sut->AreRoundTimesValid($this->roundTimeCreateModel); |
||
| 91 | |||
| 92 | $this->assertFalse($result->success); |
||
| 93 | $this->assertContains("Round time #1 cannot have a round less than 1 or greater than 30.", $result->errors); |
||
| 94 | $this->assertContains("Round time #2 cannot have a round less than 1 or greater than 30.", $result->errors); |
||
| 95 | } |
||
| 96 | } |