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

TradeValidatorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 168
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 18
loc 168
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\Draft;
6
use PhpDraft\Domain\Entities\Manager;
7
use PhpDraft\Domain\Entities\Trade;
8
use PhpDraft\Domain\Entities\TradeAsset;
9
use PhpDraft\Domain\Entities\Pick;
10
use PhpDraft\Domain\Validators\TradeValidator;
11
12
class TradeValidatorTest extends TestCase {
13
  function setUp() {
14
    $this->app = require dirname(__FILE__).'/../../../../api/config/_app.php';
15
    $this->draft = new Draft();
16
    $this->trade = new Trade();
17
    $this->trade->manager1 = new Manager();
18
    $this->trade->manager2 = new Manager();
19
    $this->sut = new TradeValidator($this->app);
20
  }
21
22
  public function testIsInvalidManagerIndicatedForAssetRetrieval() {
23
    $this->draft->draft_id = 1;
24
25
    $this->trade->manager1->draft_id = 2;
26
27
    $result = $this->sut->IsManagerValidForAssetRetrieval($this->draft, $this->trade->manager1);
28
29
    $this->assertFalse($result->success);
30
  }
31
32
  public function testIsValidManagerIndicatedForAssetRetrieval() {
33
    $this->draft->draft_id = 1;
34
35
    $this->trade->manager1->draft_id = 1;
36
37
    $result = $this->sut->IsManagerValidForAssetRetrieval($this->draft, $this->trade->manager1);
38
39
    $this->assertTrue($result->success);
40
  }
41
42
  public function testValidationOfTradeRoundValues() {
43
    $this->draft->draft_rounds = 10;
44
45
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
46
47
    $this->assertFalse($result->success);
48
    $this->assertContains("Invalid value for trade round.", $result->errors);
49
50
    $this->trade->trade_round = -1;
51
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
52
53
    $this->assertFalse($result->success);
54
    $this->assertContains("Invalid value for trade round.", $result->errors);
55
56
    $this->trade->trade_round = 11;
57
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
58
59
    $this->assertFalse($result->success);
60
    $this->assertContains("Invalid value for trade round.", $result->errors);
61
62
    $this->trade->trade_round = 9;
63
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
64
65
    $this->assertNotContains("Invalid value for trade round.", $result->errors);
66
  }
67
68
  public function testValidationOfTradeAssetOwnership() {
69
    $this->draft->draft_id = 1;
70
71
    $tradeAsset1 = new TradeAsset();
72
    $tradeAsset1->player = new Pick();
73
    $tradeAsset1->player->player_id = 1;
74
    $tradeAsset1->player->draft_id = 1;
75
76
    $tradeAsset2 = new TradeAsset();
77
    $tradeAsset2->player = new Pick();
78
    $tradeAsset2->player->player_id = 2;
79
    $tradeAsset2->player->draft_id = 1;
80
81
    $tradeAsset1->oldmanager_id = 3;
82
    $tradeAsset2->oldmanager_id = 2;
83
84
    $this->trade->manager1_id = 1;
85
    $this->trade->manager2_id = 2;
86
87
    $this->trade->trade_assets[] = $tradeAsset1;
88
    $this->trade->trade_assets[] = $tradeAsset2;
89
90
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
91
92
    $this->assertFalse($result->success);
93
    $this->assertContains("Asset #1 does not belong to either manager.", $result->errors);
94
    //Added since the addition of an AND condition instead of OR in the validator:
95
    $this->assertNotContains("Asset #2 does not belong to either manager.", $result->errors);
96
97
    $tradeAsset1->oldmanager_id = 1;
98
    $tradeAsset2->oldmanager_id = 4;
99
100
    $this->trade->trade_assets = array();
101
    $this->trade->trade_assets[] = $tradeAsset1;
102
    $this->trade->trade_assets[] = $tradeAsset2;
103
104
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
105
106
    $this->assertFalse($result->success);
107
    $this->assertContains("Asset #2 does not belong to either manager.", $result->errors);
108
109
    $tradeAsset1->player->draft_id = 2;
110
111
    $this->trade->trade_assets = array();
112
    $this->trade->trade_assets[] = $tradeAsset1;
113
    $this->trade->trade_assets[] = $tradeAsset2;
114
115
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
116
117
    $this->assertFalse($result->success);
118
    $this->assertContains("Asset #1 does not belong to draft #1", $result->errors);
119
120
    $tradeAsset1->player->draft_id = 1;
121
    $tradeAsset2->player->draft_id = 38;
122
123
    $this->trade->trade_assets = array();
124
    $this->trade->trade_assets[] = $tradeAsset1;
125
    $this->trade->trade_assets[] = $tradeAsset2;
126
127
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
128
129
    $this->assertFalse($result->success);
130
    $this->assertContains("Asset #2 does not belong to draft #1", $result->errors);
131
  }
132
133
  public function testValidationOfAssetCounts() {
134
    $this->draft->draft_id = 1;
135
136
    $tradeAsset1 = new TradeAsset();
137
    $tradeAsset1->player = new Pick();
138
    $tradeAsset1->player->player_id = 2;
139
    $tradeAsset1->player->draft_id = 1;
140
141
    $tradeAsset2 = new TradeAsset();
142
    $tradeAsset2->player = new Pick();
143
    $tradeAsset2->player->player_id = 2;
144
    $tradeAsset2->player->draft_id = 1;
145
146
    $tradeAsset1->oldmanager_id = 3;
147
    $tradeAsset2->oldmanager_id = 2;
148
149
    $this->trade->manager1_id = 1;
150
    $this->trade->manager2_id = 2;
151
152
    $this->trade->trade_assets[] = $tradeAsset1;
153
    $this->trade->trade_assets[] = $tradeAsset2;
154
155
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
156
157
    $this->assertFalse($result->success);
158
    $this->assertContains("One or more of the trade assets are duplicate.", $result->errors);
159
  }
160
161
  public function testValidationOfManagersBelongingToTheDraft() {
162
    $this->draft->draft_id = 1;
163
    $this->trade->manager1->draft_id = 2;
164
    $this->trade->manager2->draft_id = 1;
165
166
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
167
168
    $this->assertFalse($result->success);
169
    $this->assertContains("Manager 1 does not belong to draft #1", $result->errors);
170
171
    $this->trade->manager1->draft_id = 1;
172
    $this->trade->manager2->draft_id = 3;
173
174
    $result = $this->sut->IsTradeValid($this->draft, $this->trade);
175
176
    $this->assertFalse($result->success);
177
    $this->assertContains("Manager 2 does not belong to draft #1", $result->errors);
178
  }
179
}