1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* 2018-01-09: Note first time trying to implement "good" unit tests according to "The Art of Unit Testing" |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Service\RankingSystem; |
8
|
|
|
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
10
|
|
|
use Tfboe\FmLib\Entity\Helpers\TournamentHierarchyInterface; |
11
|
|
|
use Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier; |
12
|
|
|
use Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService; |
13
|
|
|
use Tfboe\FmLib\Service\RankingSystem\TimeServiceInterface; |
14
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class EntityComparerByTimeStartTimeAndLocalIdentifierTest |
18
|
|
|
* @packageTfboe\FmLib\Tests\Unit\Service\RankingSystemListService |
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
20
|
|
|
*/ |
21
|
|
|
class EntityComparerByTimeStartTimeAndLocalIdentifierTest extends UnitTestCase |
22
|
|
|
{ |
23
|
|
|
//<editor-fold desc="Public Methods"> |
24
|
|
|
/** |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function localIdentifierProvider() |
28
|
|
|
{ |
29
|
|
|
return array_merge($this->localIdentifierProviderWithoutZero(), [[8, 8, 0]]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function localIdentifierProviderWithoutZero() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
[5, 7, -1], |
39
|
|
|
["15", "5", 1] |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @dataProvider timePairProvider |
45
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::compareEntities |
46
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
47
|
|
|
* ::compareEntityTimes |
48
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::__construct |
49
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService::getTime |
50
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
51
|
|
|
* ::compareLocalIdentifiersWithinTournament |
52
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::getPredecessors |
53
|
|
|
* @param \DateTime $time1 |
54
|
|
|
* @param \DateTime $time2 |
55
|
|
|
* @param int $expectedResult |
56
|
|
|
*/ |
57
|
|
|
public function testCompareEntitiesDifferentEntityTimesGrandparentLevel(\DateTime $time1, \DateTime $time2, |
58
|
|
|
int $expectedResult) |
59
|
|
|
{ |
60
|
|
|
$service = $this->createComparer(); |
61
|
|
|
$commonEndTime = new \DateTime("2017-01-01"); |
62
|
|
|
$grandParent1 = $this->createTreeStructureEntity('gp1', ['getEndTime' => $time1]); |
63
|
|
|
$grandParent2 = $this->createTreeStructureEntity('gp2', ['getEndTime' => $time2]); |
64
|
|
|
$parent1 = $this->createTreeStructureEntity('p1', ['getEndTime' => $commonEndTime, 'getParent' => $grandParent1]); |
65
|
|
|
$parent2 = $this->createTreeStructureEntity('p2', ['getEndTime' => $commonEndTime, 'getParent' => $grandParent2]); |
66
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getEndTime' => $commonEndTime, 'getParent' => $parent1]); |
67
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getEndTime' => $commonEndTime, 'getParent' => $parent2]); |
68
|
|
|
|
69
|
|
|
/** @var TournamentHierarchyInterface $entity1 */ |
70
|
|
|
/** @var TournamentHierarchyInterface $entity2 */ |
71
|
|
|
$result = $service->compareEntities($entity1, $entity2); |
|
|
|
|
72
|
|
|
self::assertEquals($expectedResult, $result); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider timePairProvider |
77
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::compareEntities |
78
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
79
|
|
|
* ::compareEntityTimes |
80
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::__construct |
81
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService::getTime |
82
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
83
|
|
|
* ::compareLocalIdentifiersWithinTournament |
84
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::getPredecessors |
85
|
|
|
* @param \DateTime $time1 |
86
|
|
|
* @param \DateTime $time2 |
87
|
|
|
* @param int $expectedResult |
88
|
|
|
*/ |
89
|
|
|
public function testCompareEntitiesDifferentEntityTimesOneLevel(\DateTime $time1, \DateTime $time2, |
90
|
|
|
int $expectedResult) |
91
|
|
|
{ |
92
|
|
|
$service = $this->createComparer(); |
93
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getEndTime' => $time1]); |
94
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getEndTime' => $time2]); |
95
|
|
|
|
96
|
|
|
/** @var TournamentHierarchyInterface $entity1 */ |
97
|
|
|
/** @var TournamentHierarchyInterface $entity2 */ |
98
|
|
|
$result = $service->compareEntities($entity1, $entity2); |
|
|
|
|
99
|
|
|
self::assertEquals($expectedResult, $result); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @dataProvider localIdentifierProvider |
104
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::compareEntities |
105
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::__construct |
106
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
107
|
|
|
* ::compareEntityTimes |
108
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
109
|
|
|
* ::compareLocalIdentifiersWithinTournament |
110
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::getPredecessors |
111
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService::getTime |
112
|
|
|
* @param mixed $localIdentifier1 the local identifier of entity1 |
113
|
|
|
* @param mixed $localIdentifier2 the local identifier of entity2 |
114
|
|
|
* @param int $expectedResult |
115
|
|
|
*/ |
116
|
|
|
public function testCompareEntitiesSameEntityTimesDifferentLocalIdentifiers($localIdentifier1, $localIdentifier2, |
117
|
|
|
int $expectedResult) |
118
|
|
|
{ |
119
|
|
|
$service = $this->createComparer(); |
120
|
|
|
$commonEndTime = new \DateTime("2017-01-01"); |
121
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', |
122
|
|
|
['getEndTime' => $commonEndTime, 'getLocalIdentifier' => $localIdentifier1]); |
123
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', |
124
|
|
|
['getEndTime' => $commonEndTime, 'getLocalIdentifier' => $localIdentifier2]); |
125
|
|
|
|
126
|
|
|
/** @var TournamentHierarchyInterface $entity1 */ |
127
|
|
|
/** @var TournamentHierarchyInterface $entity2 */ |
128
|
|
|
$result = $service->compareEntities($entity1, $entity2); |
|
|
|
|
129
|
|
|
self::assertEquals($expectedResult, $result); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ //ReflectionException |
133
|
|
|
/** |
134
|
|
|
* @dataProvider timePairProvider |
135
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
136
|
|
|
* ::compareEntityTimes |
137
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::__construct |
138
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService::getTime |
139
|
|
|
* @param \DateTime $time1 the start time of the first entity |
140
|
|
|
* @param \DateTime $time2 the start time of the second entity |
141
|
|
|
* @param int $expectedResult the expected result of the method |
142
|
|
|
*/ |
143
|
|
|
public function testCompareEntityTimesWithStartTimes(\DateTime $time1, \DateTime $time2, int $expectedResult) |
144
|
|
|
{ |
145
|
|
|
$service = $this->createComparer(); |
146
|
|
|
$commonEndTime = new \DateTime("2017-12-01"); |
147
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getEndTime' => $commonEndTime, 'getStartTime' => $time1]); |
148
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getEndTime' => $commonEndTime, 'getStartTime' => $time2]); |
149
|
|
|
|
150
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
151
|
|
|
$result = self::getMethod(get_class($service), 'compareEntityTimes')->invokeArgs($service, [$entity1, $entity2]); |
152
|
|
|
self::assertEquals($expectedResult, $result); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ //ReflectionException |
156
|
|
|
/** |
157
|
|
|
* @dataProvider timePairProvider |
158
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
159
|
|
|
* ::compareEntityTimes |
160
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::__construct |
161
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RecursiveEndStartTimeService::getTime |
162
|
|
|
* @param \DateTime $time1 the end time of the first entity |
163
|
|
|
* @param \DateTime $time2 the end time of the second entity |
164
|
|
|
* @param int $expectedResult the expected result of the method |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function testCompareEntityTimesWithoutStartTimes(\DateTime $time1, \DateTime $time2, int $expectedResult) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$service = $this->createComparer(); |
169
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getEndTime' => $time1]); |
170
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getEndTime' => $time2]); |
171
|
|
|
|
172
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
173
|
|
|
$result = self::getMethod(get_class($service), 'compareEntityTimes')->invokeArgs($service, [$entity1, $entity2]); |
174
|
|
|
self::assertEquals($expectedResult, $result); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ //ReflectionException |
178
|
|
|
/** |
179
|
|
|
* @dataProvider localIdentifierProviderWithoutZero |
180
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
181
|
|
|
* ::compareLocalIdentifiersWithinTournament |
182
|
|
|
* @param mixed $localIdentifier1 the local identifier of grand parent 1 |
183
|
|
|
* @param mixed $localIdentifier2 the local identifier of grand parent 2 |
184
|
|
|
* @param int $expectedResult the expected result of the method |
185
|
|
|
*/ |
186
|
|
|
public function testCompareLocalIdentifiersWithinTournamentGrandParentsLevel($localIdentifier1, $localIdentifier2, |
187
|
|
|
int $expectedResult) |
188
|
|
|
{ |
189
|
|
|
$service = $this->createComparer(); |
190
|
|
|
$grandParent1 = $this->createTreeStructureEntity('gp1', ['getLocalIdentifier' => $localIdentifier1]); |
191
|
|
|
$grandParent2 = $this->createTreeStructureEntity('gp2', ['getLocalIdentifier' => $localIdentifier2]); |
192
|
|
|
$parent1 = $this->createTreeStructureEntity('p1', ['getLocalIdentifier' => 1, 'getParent' => $grandParent1]); |
193
|
|
|
$parent2 = $this->createTreeStructureEntity('p2', ['getLocalIdentifier' => 5, 'getParent' => $grandParent2]); |
194
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getLocalIdentifier' => "4", 'getParent' => $parent1]); |
195
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getLocalIdentifier' => "2", 'getParent' => $parent2]); |
196
|
|
|
|
197
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
198
|
|
|
$result = self::getMethod(get_class($service), 'compareLocalIdentifiersWithinTournament') |
199
|
|
|
->invokeArgs($service, [$entity1, $entity2]); |
200
|
|
|
self::assertEquals($expectedResult, $result); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ //ReflectionException |
204
|
|
|
/** |
205
|
|
|
* @dataProvider localIdentifierProvider |
206
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
207
|
|
|
* ::compareLocalIdentifiersWithinTournament |
208
|
|
|
* @param mixed $localIdentifier1 the local identifier of entity1 |
209
|
|
|
* @param mixed $localIdentifier2 the local identifier of entity2 |
210
|
|
|
* @param int $expectedResult the expected result of the method |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function testCompareLocalIdentifiersWithinTournamentOneLevel($localIdentifier1, $localIdentifier2, |
|
|
|
|
213
|
|
|
int $expectedResult) |
214
|
|
|
{ |
215
|
|
|
$service = $this->createComparer(); |
216
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getLocalIdentifier' => $localIdentifier1]); |
217
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getLocalIdentifier' => $localIdentifier2]); |
218
|
|
|
|
219
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
220
|
|
|
$result = self::getMethod(get_class($service), 'compareLocalIdentifiersWithinTournament') |
221
|
|
|
->invokeArgs($service, [$entity1, $entity2]); |
222
|
|
|
self::assertEquals($expectedResult, $result); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** @noinspection PhpDocMissingThrowsInspection */ //ReflectionException |
226
|
|
|
/** |
227
|
|
|
* @dataProvider localIdentifierProviderWithoutZero |
228
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier |
229
|
|
|
* ::compareLocalIdentifiersWithinTournament |
230
|
|
|
* @param mixed $localIdentifier1 the local identifier of parent 1 |
231
|
|
|
* @param mixed $localIdentifier2 the local identifier of parent 2 |
232
|
|
|
* @param int $expectedResult the expected result of the method |
233
|
|
|
*/ |
234
|
|
|
public function testCompareLocalIdentifiersWithinTournamentParentsLevelWithCommonGrandParent($localIdentifier1, |
235
|
|
|
$localIdentifier2, |
236
|
|
|
int $expectedResult) |
237
|
|
|
{ |
238
|
|
|
$service = $this->createComparer(); |
239
|
|
|
$grandParent = $this->createTreeStructureEntity('gp1', ['getLocalIdentifier' => 1]); |
240
|
|
|
$parent1 = $this->createTreeStructureEntity('p1', |
241
|
|
|
['getLocalIdentifier' => $localIdentifier1, 'getParent' => $grandParent]); |
242
|
|
|
$parent2 = $this->createTreeStructureEntity('p2', |
243
|
|
|
['getLocalIdentifier' => $localIdentifier2, 'getParent' => $grandParent]); |
244
|
|
|
$entity1 = $this->createTreeStructureEntity('e1', ['getLocalIdentifier' => "3", 'getParent' => $parent1]); |
245
|
|
|
$entity2 = $this->createTreeStructureEntity('e2', ['getLocalIdentifier' => "5", 'getParent' => $parent2]); |
246
|
|
|
|
247
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
248
|
|
|
$result = self::getMethod(get_class($service), 'compareLocalIdentifiersWithinTournament') |
249
|
|
|
->invokeArgs($service, [$entity1, $entity2]); |
250
|
|
|
self::assertEquals($expectedResult, $result); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\EntityComparerByTimeStartTimeAndLocalIdentifier::__construct |
255
|
|
|
*/ |
256
|
|
|
public function testConstruct() |
257
|
|
|
{ |
258
|
|
|
$entity = $this->createComparer(); |
259
|
|
|
self::assertInstanceOf(EntityComparerByTimeStartTimeAndLocalIdentifier::class, $entity); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
public function timePairProvider() |
266
|
|
|
{ |
267
|
|
|
return [ |
268
|
|
|
[new \DateTime("2017-05-01"), new \DateTime("2017-06-01"), -1], |
269
|
|
|
[new \DateTime("2017-06-01"), new \DateTime("2017-06-01"), 0], |
270
|
|
|
[new \DateTime("2017-06-01"), new \DateTime("2017-05-30"), 1] |
271
|
|
|
]; |
272
|
|
|
} |
273
|
|
|
//</editor-fold desc="Public Methods"> |
274
|
|
|
|
275
|
|
|
//<editor-fold desc="Private Methods"> |
276
|
|
|
/** |
277
|
|
|
* Creates a default comparer with the given timeService or with the default time service if omitted |
278
|
|
|
* @param TimeServiceInterface|null $timeService the time service to use |
279
|
|
|
* @return EntityComparerByTimeStartTimeAndLocalIdentifier the comparer |
280
|
|
|
*/ |
281
|
|
|
private function createComparer(TimeServiceInterface $timeService = null) |
282
|
|
|
{ |
283
|
|
|
if ($timeService === null) { |
284
|
|
|
$timeService = new RecursiveEndStartTimeService(); |
285
|
|
|
} |
286
|
|
|
return new EntityComparerByTimeStartTimeAndLocalIdentifier($timeService); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Creates a tree structure entity with the given id and optionally additional method results |
291
|
|
|
* @param string $entityId the id of the entity |
292
|
|
|
* @param array $otherMethods the additional method results |
293
|
|
|
* @return MockObject the stub |
294
|
|
|
*/ |
295
|
|
|
private function createTreeStructureEntity(string $entityId, array $otherMethods = []): MockObject |
296
|
|
|
{ |
297
|
|
|
return $this->createStub(TournamentHierarchyInterface::class, array_merge($otherMethods, ['getId' => $entityId])); |
298
|
|
|
} |
299
|
|
|
//</editor-fold desc="Private Methods"> |
300
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: