Passed
Branch master (182172)
by Matthew
04:03 queued 01:23
created

RoundTimeValidatorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 54.55 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 48
loc 88
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace PhpDraft\Test;
3
4
use PHPUnit\Framework\TestCase;
5
use \PhpDraft\Domain\Entities\RoundTime;
6
use PhpDraft\Domain\Validators\RoundTimeValidator;
7
use PhpDraft\Domain\Models\RoundTimeCreateModel;
8
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
}