Completed
Branch trunk (78000f)
by SuperNova.WS
25:55 queued 09:00
created

VectorTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 239
Duplicated Lines 6.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 16
loc 239
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

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
3
use Vector\Vector;
4
5
/**
6
 * Class VectorTest
7
 *
8
 * @coversDefaultClass Vector\Vector
9
 */
10
class VectorTest extends PHPUnit_Framework_TestCase {
11
12
  /**
13
   * @var Vector $object
14
   */
15
  protected $object;
16
  /**
17
   * @var stdClass $config
18
   */
19
  protected $config;
20
21
  public function setUp() {
22
    parent::setUp();
23
    $this->object = new Vector();
24
25
    $this->object->type = PT_PLANET;
26
27
    $this->config = new stdClass();
28
    $this->config->game_maxGalaxy = 2;
29
    $this->config->game_maxSystem = 4;
30
    $this->config->game_maxPlanet = 6;
31
    $this->config->uni_galaxy_distance = 20000;
32
33
    Vector::_staticInit($this->config);
34
  }
35
36
  public function tearDown() {
37
    unset($this->object);
38
    parent::tearDown();
39
  }
40
41
  /**
42
   * @covers ::_staticInit
43
   */
44
  public function test_staticInit() {
45
    $className = get_class($this->object);
46
    $this->assertAttributeEquals(true, '_isStaticInit', $className);
47
    $this->assertAttributeEquals(2, 'knownGalaxies', $className);
48
    $this->assertAttributeEquals(4, 'knownSystems', $className);
49
    $this->assertAttributeEquals(6, 'knownPlanets', $className);
50
    $this->assertAttributeEquals(20000, 'galaxyDistance', $className);
51
52
    $this->assertAttributeEquals(PT_PLANET, 'type', $this->object);
53
  }
54
55
  /**
56
   * @covers ::readFromVector
57
   */
58
  public function testReadFromVector() {
59
    $vector = new Vector(1, 3, 5, PT_MOON);
60
    $this->object->readFromVector($vector);
61
62
    $this->assertAttributeEquals(1, 'galaxy', $this->object);
63
    $this->assertAttributeEquals(3, 'system', $this->object);
64
    $this->assertAttributeEquals(5, 'planet', $this->object);
65
    $this->assertAttributeEquals(PT_MOON, 'type', $this->object);
66
  }
67
68
  /**
69
   * @covers ::getParamInt
70
   */
71
  // TODO
72
  public function testGetParamInt() {
73
  }
74
75
  /**
76
   * @covers ::readFromParamFleets
77
   */
78
  // TODO
79
  public function testReadFromParamFleets() {
80
  }
81
82
  public function data__construct() {
83
    $vector = new Vector(4, 5, 6, PT_PLANET);
84
85
    return array(
86
      array(1, 2, 3, PT_MOON, 1, 2, 3, PT_MOON),
87
      array(Vector::READ_VECTOR, $vector, 3, PT_MOON, 4, 5, 6, PT_PLANET),
88
      // TODO
89
//      array(Vector::READ_PARAMS_FLEET, array('galaxy' => 90, 'system' => 91, 'planet' => 92, 'planet_type' => 93,), 99, PT_MOON, 4, 5, 6, PT_PLANET),
90
    );
91
  }
92
93
  /**
94
   * @dataProvider data__construct
95
   *
96
   * @covers ::__construct
97
   */
98
  public function test__construct($galaxy, $system, $planet, $type, $g, $s, $p, $t) {
99
    $this->object = new Vector($galaxy, $system, $planet, $type);
100
101
    $this->assertAttributeEquals($g, 'galaxy', $this->object);
102
    $this->assertAttributeEquals($s, 'system', $this->object);
103
    $this->assertAttributeEquals($p, 'planet', $this->object);
104
    $this->assertAttributeEquals($t, 'type', $this->object);
105
  }
106
107
  public function dataDistance() {
108
    return array(
109
      // Checking that $returnZero works on same planet
110
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 6, PT_PLANET), true, 0),
111
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 6, PT_PLANET), false, 5),
112
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 6, PT_MOON), true, 5),
113
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 6, PT_MOON), false, 5),
114
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 6, PT_DEBRIS), true, 5),
115
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 6, PT_DEBRIS), false, 5),
116
117
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 7, PT_PLANET), true, 1005),
118
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 7, PT_PLANET), false, 1005),
119
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 7, PT_MOON), true, 1005),
120
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 7, PT_MOON), false, 1005),
121
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 7, PT_DEBRIS), true, 1005),
122
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 7, PT_DEBRIS), false, 1005),
123
124
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 5, PT_PLANET), true, 1005),
125
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 5, PT_PLANET), false, 1005),
126
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 5, PT_MOON), true, 1005),
127
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 5, PT_MOON), false, 1005),
128
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 5, PT_DEBRIS), true, 1005),
129
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 5, PT_DEBRIS), false, 1005),
130
131
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 8, PT_PLANET), true, 1010),
132
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 8, PT_PLANET), false, 1010),
133
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 8, PT_MOON), true, 1010),
134
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 8, PT_MOON), false, 1010),
135
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 8, PT_DEBRIS), true, 1010),
136
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 4, 8, PT_DEBRIS), false, 1010),
137
138
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 5, 6, PT_PLANET), true, 2795),
139
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 5, 6, PT_PLANET), false, 2795),
140
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 5, 6, PT_MOON), true, 2795),
141
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 5, 6, PT_MOON), false, 2795),
142
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 5, 6, PT_DEBRIS), true, 2795),
143
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 5, 6, PT_DEBRIS), false, 2795),
144
145
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 3, 6, PT_PLANET), true, 2795),
146
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 3, 6, PT_PLANET), false, 2795),
147
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 3, 6, PT_MOON), true, 2795),
148
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 3, 6, PT_MOON), false, 2795),
149
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 3, 6, PT_DEBRIS), true, 2795),
150
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 3, 6, PT_DEBRIS), false, 2795),
151
152
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 2, 6, PT_PLANET), true, 2890),
153
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 2, 6, PT_PLANET), false, 2890),
154
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 2, 6, PT_MOON), true, 2890),
155
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 2, 6, PT_MOON), false, 2890),
156
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 2, 6, PT_DEBRIS), true, 2890),
157
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(2, 2, 6, PT_DEBRIS), false, 2890),
158
159
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(1, 4, 6, PT_PLANET), true, 20000),
160
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(1, 4, 6, PT_PLANET), false, 20000),
161
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(1, 4, 6, PT_MOON), true, 20000),
162
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(1, 4, 6, PT_MOON), false, 20000),
163
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(1, 4, 6, PT_DEBRIS), true, 20000),
164
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(1, 4, 6, PT_DEBRIS), false, 20000),
165
166
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(3, 4, 6, PT_PLANET), true, 20000),
167
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(3, 4, 6, PT_PLANET), false, 20000),
168
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(3, 4, 6, PT_MOON), true, 20000),
169
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(3, 4, 6, PT_MOON), false, 20000),
170
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(3, 4, 6, PT_DEBRIS), true, 20000),
171
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(3, 4, 6, PT_DEBRIS), false, 20000),
172
173
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(4, 4, 6, PT_PLANET), true, 40000),
174
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(4, 4, 6, PT_PLANET), false, 40000),
175
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(4, 4, 6, PT_MOON), true, 40000),
176
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(4, 4, 6, PT_MOON), false, 40000),
177
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(4, 4, 6, PT_DEBRIS), true, 40000),
178
      array(new Vector(2, 4, 6, PT_PLANET), new Vector(4, 4, 6, PT_DEBRIS), false, 40000),
179
    );
180
  }
181
182
  /**
183
   * @dataProvider dataDistance
184
   *
185
   * @covers ::distance
186
   *
187
   * @param Vector $vector1
188
   * @param Vector $vector2
189
   * @param bool   $returnZero
190
   * @param int    $distance
191
   */
192
  public function testDistance($vector1, $vector2, $returnZero, $distance) {
193
    $this->object->readFromVector($vector1);
194
    $this->assertEquals($distance, $this->object->distance($vector2, $returnZero));
195
  }
196
197
  public function dataConvertToVector() {
198
    return array(
199
      array(array('galaxy' => 1, 'system' => 3, 'planet' => 5, 'planet_type' => PT_MOON), '', 1, 3, 5, PT_MOON, 20000),
200
      array(array('galaxy' => 1, 'system' => 3, 'planet' => 5, 'type' => PT_MOON), '', 1, 3, 5, PT_MOON, 20000),
201
      array(array('pr_galaxy' => 1, 'pr_system' => 3, 'pr_planet' => 5, 'pr_planet_type' => PT_MOON), 'pr_', 1, 3, 5, PT_MOON, 40000),
202
      array(array('pr_galaxy' => 1, 'pr_system' => 3, 'pr_planet' => 5, 'pr_type' => PT_MOON), 'pr_', 1, 3, 5, PT_MOON, 40000),
203
204
      array(array('galaxy' => 2, 'system' => 3, 'planet' => 5, 'type' => PT_MOON), '', 2, 3, 5, PT_MOON, 2795),
205
      array(array('galaxy' => 2, 'system' => 4, 'planet' => 5, 'type' => PT_MOON), '', 2, 4, 5, PT_MOON, 1005),
206
      array(array('galaxy' => 2, 'system' => 4, 'planet' => 6, 'type' => PT_MOON), '', 2, 4, 6, PT_MOON, 5),
207
      array(array('galaxy' => 2, 'system' => 4, 'planet' => 6, 'type' => PT_PLANET), '', 2, 4, 6, PT_PLANET, 5),
208
    );
209
  }
210
211
  /**
212
   * @dataProvider dataConvertToVector
213
   *
214
   * @covers ::convertToVector
215
   *
216
   * @param array  $coordinates
217
   * @param string $prefix
218
   * @param        $g
219
   * @param        $s
220
   * @param        $p
221
   * @param        $t
222
   * @param        $d
223
   */
224
  public function testConvertToVector($coordinates, $prefix, $g, $s, $p, $t, $d) {
225
    $this->object = Vector::convertToVector($coordinates, $prefix);
226
227
    $this->assertAttributeEquals($g, 'galaxy', $this->object);
228
    $this->assertAttributeEquals($s, 'system', $this->object);
229
    $this->assertAttributeEquals($p, 'planet', $this->object);
230
    $this->assertAttributeEquals($t, 'type', $this->object);
231
  }
232
233
  /**
234
   * @dataProvider dataConvertToVector
235
   *
236
   * @covers ::distanceFromCoordinates
237
   *
238
   * @param Vector $vector
239
   * @param int    $distance
240
   */
241
  public function testDistanceFromCoordinates($coordinates, $prefix, $g, $s, $p, $t, $d) {
242
    $this->object->galaxy = 2;
243
    $this->object->system = 4;
244
    $this->object->planet = 6;
245
    $this->assertEquals($d, $this->object->distanceFromCoordinates($coordinates));
246
  }
247
248
}
249