Completed
Push — master ( fb8886...624979 )
by Matthew
9s
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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testIsInvalidManagerIndicatedForAssetRetrieval() 9 9 1
A testIsValidManagerIndicatedForAssetRetrieval() 9 9 1
B testValidationOfTradeRoundValues() 0 25 1
A testValidationOfTradeAssetOwnership() 0 64 1
B testValidationOfAssetCounts() 0 27 1
A testValidationOfManagersBelongingToTheDraft() 0 18 1

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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
    $this->app = require dirname(__FILE__).'/../../../../api/config/_app.php';
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
    $this->draft = new Draft();
0 ignored issues
show
Bug introduced by
The property draft does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
    $this->trade = new Trade();
0 ignored issues
show
Bug introduced by
The property trade does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
    $this->trade->manager1 = new Manager();
18
    $this->trade->manager2 = new Manager();
19
    $this->sut = new TradeValidator($this->app);
0 ignored issues
show
Bug introduced by
The property sut does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
  }
21
22 View Code Duplication
  public function testIsInvalidManagerIndicatedForAssetRetrieval() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
  public function testIsValidManagerIndicatedForAssetRetrieval() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \PhpDraft\Domain\Entities\Pick() of type object<PhpDraft\Domain\Entities\Pick> is incompatible with the declared type object<PhpDraft\Domain\Entities\Player> of property $player.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \PhpDraft\Domain\Entities\Pick() of type object<PhpDraft\Domain\Entities\Pick> is incompatible with the declared type object<PhpDraft\Domain\Entities\Player> of property $player.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
}