|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 1/3/18 |
|
7
|
|
|
* Time: 3:54 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Service\RankingSystem; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
13
|
|
|
use Doctrine\Common\Collections\Collection; |
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
15
|
|
|
use Doctrine\ORM\AbstractQuery; |
|
16
|
|
|
use Doctrine\ORM\EntityManager; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
19
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
20
|
|
|
use Tfboe\FmLib\Entity\Helpers\AutomaticInstanceGeneration; |
|
21
|
|
|
use Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity; |
|
22
|
|
|
use Tfboe\FmLib\Entity\Helpers\TournamentHierarchyInterface; |
|
23
|
|
|
use Tfboe\FmLib\Entity\RankingSystemChangeInterface; |
|
24
|
|
|
use Tfboe\FmLib\Entity\RankingSystemInterface; |
|
25
|
|
|
use Tfboe\FmLib\Entity\RankingSystemListEntryInterface; |
|
26
|
|
|
use Tfboe\FmLib\Entity\RankingSystemListInterface; |
|
27
|
|
|
use Tfboe\FmLib\Exceptions\PreconditionFailedException; |
|
28
|
|
|
use Tfboe\FmLib\Helpers\Level; |
|
29
|
|
|
use Tfboe\FmLib\Service\ObjectCreatorServiceInterface; |
|
30
|
|
|
use Tfboe\FmLib\Service\RankingSystem\EntityComparerInterface; |
|
31
|
|
|
use Tfboe\FmLib\Service\RankingSystem\RankingSystemService; |
|
32
|
|
|
use Tfboe\FmLib\Service\RankingSystem\TimeServiceInterface; |
|
33
|
|
|
use Tfboe\FmLib\Tests\Entity\Competition; |
|
34
|
|
|
use Tfboe\FmLib\Tests\Entity\Game; |
|
35
|
|
|
use Tfboe\FmLib\Tests\Entity\Match; |
|
36
|
|
|
use Tfboe\FmLib\Tests\Entity\Phase; |
|
37
|
|
|
use Tfboe\FmLib\Tests\Entity\Player; |
|
38
|
|
|
use Tfboe\FmLib\Tests\Entity\RankingSystem; |
|
39
|
|
|
use Tfboe\FmLib\Tests\Entity\RankingSystemChange; |
|
40
|
|
|
use Tfboe\FmLib\Tests\Entity\RankingSystemList; |
|
41
|
|
|
use Tfboe\FmLib\Tests\Entity\RankingSystemListEntry; |
|
42
|
|
|
use Tfboe\FmLib\Tests\Entity\Tournament; |
|
43
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Class RankingSystemServiceTest |
|
48
|
|
|
* @packageTfboe\FmLib\Tests\Unit\Service\RankingSystemService |
|
49
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
50
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
51
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods) |
|
52
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveClassLength) |
|
53
|
|
|
*/ |
|
54
|
|
|
class RankingSystemServiceTest extends UnitTestCase |
|
55
|
|
|
{ |
|
56
|
|
|
//<editor-fold desc="Public Methods"> |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testConstruct() |
|
62
|
|
|
{ |
|
63
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
64
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
65
|
|
|
$entityComparer = $this->createMock(EntityComparerInterface::class); |
|
66
|
|
|
$objectCreator = $this->createMock(ObjectCreatorServiceInterface::class); |
|
67
|
|
|
$system = $this->getMockForAbstractClass(RankingSystemService::class, |
|
68
|
|
|
[$entityManager, $timeService, $entityComparer, $objectCreator]); |
|
69
|
|
|
self::assertInstanceOf(RankingSystemService::class, $system); |
|
70
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
71
|
|
|
self::assertEquals($entityManager, self::getProperty(get_class($system), 'entityManager')->getValue($system)); |
|
72
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
73
|
|
|
self::assertEquals($timeService, self::getProperty(get_class($system), 'timeService')->getValue($system)); |
|
74
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
75
|
|
|
self::assertEquals($entityComparer, self::getProperty(get_class($system), 'entityComparer')->getValue($system)); |
|
76
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
77
|
|
|
self::assertEquals($objectCreator, self::getProperty(get_class($system), 'objectCreatorService') |
|
78
|
|
|
->getValue($system)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getAverage |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testGetAverage() |
|
85
|
|
|
{ |
|
86
|
|
|
/** @var $service RankingSystemService */ |
|
87
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [], '', false); |
|
88
|
|
|
|
|
89
|
|
|
$entry1 = $this->createMock(RankingSystemListEntry::class); |
|
90
|
|
|
$entry1->method('getPoints')->willReturn(1.0); |
|
91
|
|
|
$entry2 = $this->createMock(RankingSystemListEntry::class); |
|
92
|
|
|
$entry2->method('getPoints')->willReturn(2.0); |
|
93
|
|
|
|
|
94
|
|
|
self::assertEquals(1.5, static::callProtectedMethod($service, 'getAverage', [[$entry1, $entry2]])); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
99
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
100
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Competition |
|
101
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Competition |
|
102
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Game |
|
103
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Game |
|
104
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\NameEntity |
|
105
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimeEntity |
|
106
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Match |
|
107
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Match |
|
108
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Phase |
|
109
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Phase |
|
110
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
111
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
112
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
113
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
114
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testGetEarliestInfluenceGameLevel() |
|
117
|
|
|
{ |
|
118
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
119
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
120
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
121
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
122
|
|
|
return $entity->getEndTime(); |
|
123
|
|
|
})->after('clearTimes'); |
|
124
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
125
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, |
|
126
|
|
|
[$this->createMock(EntityManagerInterface::class), |
|
127
|
|
|
$timeService, |
|
128
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
129
|
|
|
$this->createMock(ObjectCreatorServiceInterface::class)]); |
|
130
|
|
|
$service->method("getLevel")->willReturn(Level::GAME); |
|
131
|
|
|
/** @var RankingSystemService $service */ |
|
132
|
|
|
$tournament = new Tournament(); |
|
133
|
|
|
$competition = new Competition(); |
|
134
|
|
|
$competition->setName("TestCompetition")->setTournament($tournament); |
|
|
|
|
|
|
135
|
|
|
$phase = new Phase(); |
|
136
|
|
|
$phase->setPhaseNumber(1); |
|
137
|
|
|
$phase->setCompetition($competition); |
|
138
|
|
|
$match = new Match(); |
|
139
|
|
|
$match->setMatchNumber(1); |
|
140
|
|
|
$match->setPhase($phase); |
|
141
|
|
|
self::assertNull($service->getEarliestInfluence($ranking, $tournament)); |
|
142
|
|
|
|
|
143
|
|
|
$tournament->getRankingSystems()->set($ranking->getId(), $ranking); |
|
144
|
|
|
self::assertNull($service->getEarliestInfluence($ranking, $tournament)); |
|
145
|
|
|
|
|
146
|
|
|
$game = new Game(); |
|
147
|
|
|
$game->setGameNumber(1); |
|
148
|
|
|
$game->setMatch($match); |
|
149
|
|
|
$gameEndTime = new \DateTime("2017-06-01 00:00:00"); |
|
150
|
|
|
$game->setEndTime($gameEndTime); |
|
151
|
|
|
self::assertEquals($gameEndTime, $service->getEarliestInfluence($ranking, $tournament)); |
|
152
|
|
|
|
|
153
|
|
|
$game2 = new Game(); |
|
154
|
|
|
$game2->setGameNumber(2); |
|
155
|
|
|
$game2->setMatch($match); |
|
156
|
|
|
$game2EndTime = new \DateTime("2017-05-01 00:00:00"); |
|
157
|
|
|
$game2->setEndTime($game2EndTime); |
|
158
|
|
|
self::assertEquals($game2EndTime, $service->getEarliestInfluence($ranking, $tournament)); |
|
159
|
|
|
|
|
160
|
|
|
$game3 = new Game(); |
|
161
|
|
|
$game3->setGameNumber(3); |
|
162
|
|
|
$game3->setMatch($match); |
|
163
|
|
|
$game3EndTime = new \DateTime("2017-07-01 00:00:00"); |
|
164
|
|
|
$game3->setEndTime($game3EndTime); |
|
165
|
|
|
self::assertEquals($game2EndTime, $service->getEarliestInfluence($ranking, $tournament)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
170
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
171
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Competition |
|
172
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Competition |
|
173
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Game |
|
174
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Game |
|
175
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\NameEntity |
|
176
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimeEntity |
|
177
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Match |
|
178
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Match |
|
179
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Phase |
|
180
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Phase |
|
181
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
182
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
183
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
184
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
185
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
186
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
|
187
|
|
|
*/ |
|
188
|
|
|
public function testGetEarliestInfluenceGameLevelWithDifferentImpactLevels() |
|
189
|
|
|
{ |
|
190
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
191
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
192
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
193
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
194
|
|
|
return $entity->getEndTime(); |
|
195
|
|
|
})->after('clearTimes'); |
|
196
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
197
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, |
|
198
|
|
|
[$this->createMock(EntityManagerInterface::class), |
|
199
|
|
|
$timeService, |
|
200
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
201
|
|
|
$this->createMock(ObjectCreatorServiceInterface::class)]); |
|
202
|
|
|
$service->method("getLevel")->willReturn(Level::GAME); |
|
203
|
|
|
/** @var RankingSystemService $service */ |
|
204
|
|
|
$tournament = new Tournament(); |
|
205
|
|
|
$competition = new Competition(); |
|
206
|
|
|
$competition->setName("TestCompetition")->setTournament($tournament); |
|
|
|
|
|
|
207
|
|
|
$phase = new Phase(); |
|
208
|
|
|
$phase->setPhaseNumber(1); |
|
209
|
|
|
$phase->setCompetition($competition); |
|
210
|
|
|
$match = new Match(); |
|
211
|
|
|
$match->setMatchNumber(1); |
|
212
|
|
|
$match->setPhase($phase); |
|
213
|
|
|
$game = new Game(); |
|
214
|
|
|
$game->setGameNumber(1); |
|
215
|
|
|
$game->setMatch($match); |
|
216
|
|
|
$endTime1 = new \DateTime("2017-12-01 00:00:00"); |
|
217
|
|
|
$game->setEndTime($endTime1); |
|
218
|
|
|
$game->getRankingSystems()->set($ranking->getId(), $ranking); |
|
219
|
|
|
self::assertEquals($endTime1, $service->getEarliestInfluence($ranking, $tournament)); |
|
220
|
|
|
|
|
221
|
|
|
$game2 = new Game(); |
|
222
|
|
|
$game2->setGameNumber(2); |
|
223
|
|
|
$game2->setMatch($match); |
|
224
|
|
|
$endTime2 = new \DateTime("2017-11-01 00:00:00"); |
|
225
|
|
|
$game2->setEndTime($endTime2); |
|
226
|
|
|
self::assertEquals($endTime1, $service->getEarliestInfluence($ranking, $tournament)); |
|
227
|
|
|
|
|
228
|
|
|
$match->getRankingSystems()->set($ranking->getId(), $ranking); |
|
229
|
|
|
self::assertEquals($endTime2, $service->getEarliestInfluence($ranking, $tournament)); |
|
230
|
|
|
|
|
231
|
|
|
$match2 = new Match(); |
|
232
|
|
|
$match2->setMatchNumber(2); |
|
233
|
|
|
$match2->setPhase($phase); |
|
234
|
|
|
$game3 = new Game(); |
|
235
|
|
|
$game3->setGameNumber(1); |
|
236
|
|
|
$game3->setMatch($match2); |
|
237
|
|
|
$endTime3 = new \DateTime("2017-10-01 00:00:00"); |
|
238
|
|
|
$game3->setEndTime($endTime3); |
|
239
|
|
|
self::assertEquals($endTime2, $service->getEarliestInfluence($ranking, $tournament)); |
|
240
|
|
|
|
|
241
|
|
|
$phase->getRankingSystems()->set($ranking->getId(), $ranking); |
|
242
|
|
|
self::assertEquals($endTime3, $service->getEarliestInfluence($ranking, $tournament)); |
|
243
|
|
|
|
|
244
|
|
|
$phase2 = new Phase(); |
|
245
|
|
|
$phase2->setPhaseNumber(2); |
|
246
|
|
|
$phase2->setCompetition($competition); |
|
247
|
|
|
$match3 = new Match(); |
|
248
|
|
|
$match3->setMatchNumber(1); |
|
249
|
|
|
$match3->setPhase($phase2); |
|
250
|
|
|
$game4 = new Game(); |
|
251
|
|
|
$game4->setGameNumber(1); |
|
252
|
|
|
$game4->setMatch($match3); |
|
253
|
|
|
$endTime4 = new \DateTime("2017-09-01 00:00:00"); |
|
254
|
|
|
$game4->setEndTime($endTime4); |
|
255
|
|
|
self::assertEquals($endTime3, $service->getEarliestInfluence($ranking, $tournament)); |
|
256
|
|
|
|
|
257
|
|
|
$competition->getRankingSystems()->set($ranking->getId(), $ranking); |
|
258
|
|
|
self::assertEquals($endTime4, $service->getEarliestInfluence($ranking, $tournament)); |
|
259
|
|
|
|
|
260
|
|
|
$competition2 = new Competition(); |
|
261
|
|
|
$competition2->setName("TestCompetition2")->setTournament($tournament); |
|
|
|
|
|
|
262
|
|
|
$phase3 = new Phase(); |
|
263
|
|
|
$phase3->setPhaseNumber(1); |
|
264
|
|
|
$phase3->setCompetition($competition2); |
|
265
|
|
|
$match4 = new Match(); |
|
266
|
|
|
$match4->setMatchNumber(1); |
|
267
|
|
|
$match4->setPhase($phase3); |
|
268
|
|
|
$game5 = new Game(); |
|
269
|
|
|
$game5->setGameNumber(1); |
|
270
|
|
|
$game5->setMatch($match4); |
|
271
|
|
|
$endTime5 = new \DateTime("2017-01-01 00:00:00"); |
|
272
|
|
|
$game5->setEndTime($endTime5); |
|
273
|
|
|
self::assertEquals($endTime4, $service->getEarliestInfluence($ranking, $tournament)); |
|
274
|
|
|
|
|
275
|
|
|
$game6 = new Game(); |
|
276
|
|
|
$game6->setGameNumber(2); |
|
277
|
|
|
$game6->setMatch($match4); |
|
278
|
|
|
$endTime6 = new \DateTime("2017-10-01 00:00:00"); |
|
279
|
|
|
$game6->setEndTime($endTime6); |
|
280
|
|
|
$game6->getRankingSystems()->set($ranking->getId(), $ranking); |
|
281
|
|
|
self::assertEquals($endTime4, $service->getEarliestInfluence($ranking, $tournament)); |
|
282
|
|
|
|
|
283
|
|
|
$game7 = new Game(); |
|
284
|
|
|
$game7->setGameNumber(3); |
|
285
|
|
|
$game7->setMatch($match4); |
|
286
|
|
|
$endTime7 = new \DateTime("2017-08-01 00:00:00"); |
|
287
|
|
|
$game7->setEndTime($endTime7); |
|
288
|
|
|
$game7->getRankingSystems()->set($ranking->getId(), $ranking); |
|
289
|
|
|
self::assertEquals($endTime7, $service->getEarliestInfluence($ranking, $tournament)); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
294
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
295
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
296
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
297
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
298
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimeEntity |
|
299
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimestampableEntity |
|
300
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
301
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
302
|
|
|
*/ |
|
303
|
|
|
public function testGetEarliestInfluenceTournamentLevel() |
|
304
|
|
|
{ |
|
305
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
306
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
307
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
308
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
309
|
|
|
return $entity->getEndTime(); |
|
310
|
|
|
})->after('clearTimes'); |
|
311
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
312
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, |
|
313
|
|
|
[$this->createMock(EntityManagerInterface::class), |
|
314
|
|
|
$timeService, |
|
315
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
316
|
|
|
$this->createMock(ObjectCreatorServiceInterface::class)]); |
|
317
|
|
|
$service->method("getLevel")->willReturn(Level::TOURNAMENT); |
|
318
|
|
|
/** @var RankingSystemService $service */ |
|
319
|
|
|
$tournament = new Tournament(); |
|
320
|
|
|
$tournament->getRankingSystems()->set($ranking->getId(), $ranking); |
|
321
|
|
|
$endTime = new \DateTime("2017-03-01 00:00:00"); |
|
322
|
|
|
$tournament->setEndTime($endTime); |
|
323
|
|
|
self::assertEquals($endTime, $service->getEarliestInfluence($ranking, $tournament)); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getAverage |
|
328
|
|
|
*/ |
|
329
|
|
|
public function testGetEmptyAverage() |
|
330
|
|
|
{ |
|
331
|
|
|
/** @var $service RankingSystemService */ |
|
332
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [], '', false); |
|
333
|
|
|
|
|
334
|
|
|
self::assertEquals(0.0, static::callProtectedMethod($service, 'getAverage', [[]])); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
339
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
340
|
|
|
*/ |
|
341
|
|
|
public function testGetEntities() |
|
342
|
|
|
{ |
|
343
|
|
|
//create mock for input |
|
344
|
|
|
$ranking = $this->createMock(RankingSystem::class); |
|
345
|
|
|
|
|
346
|
|
|
//create service mock |
|
347
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, |
|
348
|
|
|
[$this->createMock(EntityManagerInterface::class), $this->createMock(TimeServiceInterface::class), |
|
349
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
350
|
|
|
$this->createMock(ObjectCreatorServiceInterface::class)]); |
|
351
|
|
|
|
|
352
|
|
|
//create mock for queryBuilder |
|
353
|
|
|
$entityList = ['e1', 'e2']; |
|
354
|
|
|
$query = $this->createMock(AbstractQuery::class); |
|
355
|
|
|
$query->expects(static::once())->method('getResult')->willReturn($entityList); |
|
356
|
|
|
//create query builder mock for getEntities |
|
357
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
|
358
|
|
|
$queryBuilder->expects(static::once())->method('getQuery')->willReturn($query); |
|
359
|
|
|
$service->expects(static::once())->method('getEntitiesQueryBuilder') |
|
360
|
|
|
->with($ranking, new \DateTime("2017-01-01"))->willReturn($queryBuilder); |
|
361
|
|
|
|
|
362
|
|
|
/** @var $service RankingSystemService */ |
|
363
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
364
|
|
|
self::assertEquals($entityList, static::getMethod(get_class($service), 'getEntities') |
|
365
|
|
|
->invokeArgs($service, [$ranking, new \DateTime("2017-01-01")])); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntityManager |
|
370
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
371
|
|
|
*/ |
|
372
|
|
|
public function testGetEntityManager() |
|
373
|
|
|
{ |
|
374
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
|
375
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [$entityManager, |
|
376
|
|
|
$this->createMock(TimeServiceInterface::class), $this->createMock(EntityComparerInterface::class), |
|
377
|
|
|
$this->createMock(ObjectCreatorServiceInterface::class)]); |
|
378
|
|
|
$em = static::callProtectedMethod($service, 'getEntityManager'); |
|
379
|
|
|
self::assertEquals($entityManager, $em); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntriesOfPlayers |
|
384
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateRankingSystemListEntry |
|
385
|
|
|
*/ |
|
386
|
|
|
public function testGetEntriesOfPlayers() |
|
387
|
|
|
{ |
|
388
|
|
|
|
|
389
|
|
|
/** @var $service RankingSystemService */ |
|
390
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [], '', false); |
|
391
|
|
|
|
|
392
|
|
|
$entry1 = $this->createMock(RankingSystemListEntry::class); |
|
393
|
|
|
$entry2 = $this->createMock(RankingSystemListEntry::class); |
|
394
|
|
|
$entry3 = $this->createMock(RankingSystemListEntry::class); |
|
395
|
|
|
|
|
396
|
|
|
$entries = new ArrayCollection([1 => $entry1, 2 => $entry2, 3 => $entry3]); |
|
397
|
|
|
$list = $this->createStub(RankingSystemList::class, ['getEntries' => $entries]); |
|
398
|
|
|
|
|
399
|
|
|
$player1 = $this->createStub(Player::class, ['getId' => 1]); |
|
400
|
|
|
$player3 = $this->createStub(Player::class, ['getId' => 3]); |
|
401
|
|
|
|
|
402
|
|
|
$returnedEntries = static::callProtectedMethod($service, 'getEntriesOfPlayers', |
|
403
|
|
|
[new ArrayCollection([$player1, $player3]), $list]); |
|
404
|
|
|
self::assertEquals([$entry1, $entry3], $returnedEntries); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateChange |
|
409
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
410
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange |
|
411
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
412
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty |
|
413
|
|
|
*/ |
|
414
|
|
|
public function testGetOrCreateChangeCreateNewOne() |
|
415
|
|
|
{ |
|
416
|
|
|
$persisted = null; |
|
417
|
|
|
|
|
418
|
|
|
[$entity, $ranking, $player] = $this->createEntities(); |
|
|
|
|
|
|
419
|
|
|
[$service, $entityManager] = $this->prepareCreateChange(); |
|
|
|
|
|
|
420
|
|
|
$entityManager->expects(self::once())->method('persist')->willReturnCallback( |
|
421
|
|
|
function (RankingSystemChangeInterface $change) use (&$persisted, $entity, $ranking, $player) { |
|
422
|
|
|
$persisted = $change; |
|
423
|
|
|
self::assertInstanceOf(RankingSystemChange::class, $change); |
|
424
|
|
|
self::assertEquals($entity, $change->getHierarchyEntity()); |
|
425
|
|
|
self::assertEquals($ranking, $change->getRankingSystem()); |
|
426
|
|
|
self::assertEquals($player, $change->getPlayer()); |
|
427
|
|
|
}); |
|
428
|
|
|
$service->method('getAdditionalFields')->willReturn(['additional' => 0.0]); |
|
429
|
|
|
|
|
430
|
|
|
$change = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
431
|
|
|
self::assertEquals($persisted, $change); |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateChange |
|
436
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
437
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange |
|
438
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
439
|
|
|
*/ |
|
440
|
|
|
public function testGetOrCreateChangeCreateTwice() |
|
441
|
|
|
{ |
|
442
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
|
443
|
|
|
$repository->expects(self::once())->method('findBy')->willReturn([]); |
|
444
|
|
|
|
|
445
|
|
|
[$entity, $ranking, $player] = $this->createEntities(); |
|
|
|
|
|
|
446
|
|
|
[$service, $entityManager] = $this->prepareCreateChange($repository); |
|
|
|
|
|
|
447
|
|
|
$entityManager->expects(self::once())->method('persist'); |
|
448
|
|
|
|
|
449
|
|
|
$change = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
450
|
|
|
$change2 = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
451
|
|
|
self::assertEquals($change, $change2); |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateChange |
|
456
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
457
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange |
|
458
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
459
|
|
|
*/ |
|
460
|
|
|
public function testGetOrCreateChangeFindInRepo() |
|
461
|
|
|
{ |
|
462
|
|
|
[$entity, $ranking, $player] = $this->createEntities(); |
|
|
|
|
|
|
463
|
|
|
$change = $this->createStub(RankingSystemChange::class, ['getRankingSystem' => $ranking, 'getPlayer' => $player]); |
|
464
|
|
|
|
|
465
|
|
|
$repository = $this->createStub(ObjectRepository::class, ['findBy' => [$change]]); |
|
466
|
|
|
|
|
467
|
|
|
|
|
468
|
|
|
[$service, $entityManager] = $this->prepareCreateChange($repository); |
|
|
|
|
|
|
469
|
|
|
$entityManager->expects(self::never())->method('persist'); |
|
470
|
|
|
|
|
471
|
|
|
$foundChange = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
472
|
|
|
self::assertEquals($change, $foundChange); |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateChange |
|
477
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
478
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
479
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange::init |
|
480
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange |
|
481
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemList |
|
482
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
483
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
484
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
485
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
486
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
487
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
488
|
|
|
*/ |
|
489
|
|
|
public function testGetOrCreateGetDeletedChange() |
|
490
|
|
|
{ |
|
491
|
|
|
[$entity, $ranking, $player] = $this->createEntities(); |
|
|
|
|
|
|
492
|
|
|
$change = $this->createStub(RankingSystemChange::class, |
|
493
|
|
|
['getRankingSystem' => $ranking, 'getPlayer' => $player, 'getHierarchyEntity' => $entity]); |
|
494
|
|
|
|
|
495
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([$change], null, ['persist', 'remove', 'getRepository']); |
|
496
|
|
|
$entityManager->expects(self::once())->method('persist'); |
|
497
|
|
|
$service = $this->prepareUpdateRankingFrom($ranking, $entityManager); |
|
|
|
|
|
|
498
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
499
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2017-02-28')); |
|
500
|
|
|
$repository = $this->createStub(ObjectRepository::class, ['findBy' => [$change]]); |
|
501
|
|
|
$entityManager->method('getRepository')->willReturn($repository); |
|
502
|
|
|
$foundChange = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
503
|
|
|
self::assertNotEquals($change, $foundChange); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateChange |
|
508
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
509
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange::init |
|
510
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange |
|
511
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemList |
|
512
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
513
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
514
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
515
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
516
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
517
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
518
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
519
|
|
|
*/ |
|
520
|
|
|
public function testGetOrCreateGetDeletedChangeTwice() |
|
521
|
|
|
{ |
|
522
|
|
|
[$entity, $ranking, $player] = $this->createEntities(); |
|
|
|
|
|
|
523
|
|
|
$change = $this->createStub(RankingSystemChange::class, |
|
524
|
|
|
['getRankingSystem' => $ranking, 'getPlayer' => $player, 'getHierarchyEntity' => $entity]); |
|
525
|
|
|
|
|
526
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([$change], null, ['persist', 'remove', 'getRepository']); |
|
527
|
|
|
$entityManager->expects(self::once())->method('persist'); |
|
528
|
|
|
$service = $this->prepareUpdateRankingFrom($ranking, $entityManager); |
|
|
|
|
|
|
529
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
530
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2017-02-28')); |
|
531
|
|
|
$repository = $this->createStub(ObjectRepository::class, ['findBy' => [$change]]); |
|
532
|
|
|
$entityManager->method('getRepository')->willReturn($repository); |
|
533
|
|
|
$foundChange = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
534
|
|
|
self::assertNotEquals($change, $foundChange); |
|
535
|
|
|
$foundChange2 = static::callProtectedMethod($service, 'getOrCreateChange', [$entity, $ranking, $player]); |
|
536
|
|
|
self::assertEquals($foundChange, $foundChange2); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateRankingSystemListEntry |
|
541
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::startPoints |
|
542
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
543
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemListEntry |
|
544
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
545
|
|
|
*/ |
|
546
|
|
|
public function testGetOrCreateRankingSystemListEntryExistingEntry() |
|
547
|
|
|
{ |
|
548
|
|
|
$player = $this->createStubWithId(Player::class, 1, 'getId'); |
|
549
|
|
|
$entries = new ArrayCollection([]); |
|
550
|
|
|
$list = $this->createStub(RankingSystemList::class, ['getEntries' => $entries]); |
|
551
|
|
|
$entry = $this->createStub(RankingSystemListEntry::class, |
|
552
|
|
|
['getPlayer' => $player, 'getRankingSystemList' => $list]); |
|
553
|
|
|
$entries->set(1, $entry); |
|
554
|
|
|
|
|
555
|
|
|
/** @var RankingSystemService $service */ |
|
556
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [], '', false); |
|
557
|
|
|
$foundEntry = static::callProtectedMethod($service, 'getOrCreateRankingSystemListEntry', [$list, $player]); |
|
558
|
|
|
self::assertEquals($entry, $foundEntry); |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateRankingSystemListEntry |
|
563
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::startPoints |
|
564
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
565
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemListEntry |
|
566
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
567
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty |
|
568
|
|
|
*/ |
|
569
|
|
|
public function testGetOrCreateRankingSystemListEntryNewEntry() |
|
570
|
|
|
{ |
|
571
|
|
|
$player = $this->createStubWithId(Player::class, 1, 'getId'); |
|
572
|
|
|
$entries = new ArrayCollection([]); |
|
573
|
|
|
$list = $this->createStub(RankingSystemList::class, ['getEntries' => $entries]); |
|
574
|
|
|
|
|
575
|
|
|
/** @var RankingSystemListEntryInterface $createdEntry */ |
|
576
|
|
|
$createdEntry = null; |
|
577
|
|
|
$entityManager = $this->createMock(EntityManager::class); |
|
578
|
|
|
$entityManager->expects(self::once())->method('persist')->willReturnCallback( |
|
579
|
|
|
function (RankingSystemListEntryInterface $entry) use (&$createdEntry, $player, $list) { |
|
580
|
|
|
$createdEntry = $entry; |
|
581
|
|
|
}); |
|
582
|
|
|
|
|
583
|
|
|
|
|
584
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [$entityManager, |
|
585
|
|
|
$this->createMock(TimeServiceInterface::class), $this->createMock(EntityComparerInterface::class), |
|
586
|
|
|
$this->getObjectCreator()]); |
|
587
|
|
|
$service->method('getAdditionalFields')->willReturn(['additional' => 0.0]); |
|
588
|
|
|
/** @var RankingSystemService $service */ |
|
589
|
|
|
|
|
590
|
|
|
$entry = static::callProtectedMethod($service, 'getOrCreateRankingSystemListEntry', [$list, $player]); |
|
591
|
|
|
self::assertEquals($createdEntry, $entry); |
|
592
|
|
|
self::assertInstanceOf(RankingSystemListEntry::class, $entry); |
|
593
|
|
|
self::assertEquals($player, $entry->getPlayer()); |
|
594
|
|
|
self::assertEquals($list, $entry->getRankingSystemList()); |
|
595
|
|
|
self::assertEquals(1, $entries->count()); |
|
596
|
|
|
self::assertEquals($entry, $entries[1]); |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/** |
|
600
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingForTournament |
|
601
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimeEntity |
|
602
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimestampableEntity |
|
603
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
604
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
605
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
606
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
607
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
608
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
609
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
610
|
|
|
*/ |
|
611
|
|
|
public function testUpdateRankingForTournamentOldEarliestIsEarlier() |
|
612
|
|
|
{ |
|
613
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
614
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
615
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
616
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
617
|
|
|
return $entity->getEndTime(); |
|
618
|
|
|
})->after('clearTimes'); |
|
619
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, |
|
620
|
|
|
[$this->createMock(EntityManagerInterface::class), |
|
621
|
|
|
$timeService], ['updateRankingFrom']); |
|
622
|
|
|
$service->method("getLevel")->willReturn(Level::TOURNAMENT); |
|
623
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
624
|
|
|
$tournament = new Tournament(); |
|
625
|
|
|
$endedAt = new \DateTime("2017-02-01 00:00:00"); |
|
626
|
|
|
$tournament->setUpdatedAt($endedAt); |
|
627
|
|
|
$tournament->getRankingSystems()->set($ranking->getId(), $ranking); |
|
628
|
|
|
$oldInfluence = new \DateTime("2017-01-01 00:00:00"); |
|
629
|
|
|
$service->expects(static::once()) |
|
630
|
|
|
->method('updateRankingFrom') |
|
631
|
|
|
->with($ranking, new \DateTime("2017-01-01 00:00:00")); |
|
632
|
|
|
|
|
633
|
|
|
/** @var RankingSystemService $service */ |
|
634
|
|
|
$service->updateRankingForTournament($ranking, $tournament, $oldInfluence); |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
/** |
|
638
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingForTournament |
|
639
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimestampableEntity |
|
640
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
641
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
642
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
643
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
644
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
645
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
646
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
647
|
|
|
*/ |
|
648
|
|
|
public function testUpdateRankingForTournamentOldEarliestIsNotNullAndTournamentNotRanked() |
|
649
|
|
|
{ |
|
650
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
651
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
652
|
|
|
$tournament = new Tournament(); |
|
653
|
|
|
$endedAt = new \DateTime("2017-01-01 00:00:00"); |
|
654
|
|
|
$tournament->setUpdatedAt($endedAt); |
|
655
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, [], ['updateRankingFrom']); |
|
656
|
|
|
$service->method("getLevel")->willReturn(Level::TOURNAMENT); |
|
657
|
|
|
$oldInfluence = new \DateTime("2017-02-01 00:00:00"); |
|
658
|
|
|
$service->expects(static::once()) |
|
659
|
|
|
->method('updateRankingFrom') |
|
660
|
|
|
->with($ranking, new \DateTime("2017-02-01 00:00:00")); |
|
661
|
|
|
|
|
662
|
|
|
/** @var RankingSystemService $service */ |
|
663
|
|
|
$service->updateRankingForTournament($ranking, $tournament, $oldInfluence); |
|
664
|
|
|
} |
|
665
|
|
|
|
|
666
|
|
|
/** |
|
667
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingForTournament |
|
668
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimeEntity |
|
669
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimestampableEntity |
|
670
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
671
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
672
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
673
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
674
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
675
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
676
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
677
|
|
|
*/ |
|
678
|
|
|
public function testUpdateRankingForTournamentOldEarliestIsNull() |
|
679
|
|
|
{ |
|
680
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
681
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
682
|
|
|
$tournament = new Tournament(); |
|
683
|
|
|
$endedAt = new \DateTime("2017-01-01 00:00:00"); |
|
684
|
|
|
$tournament->setEndTime($endedAt); |
|
685
|
|
|
$tournament->getRankingSystems()->set($ranking->getId(), $ranking); |
|
686
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
687
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
688
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
689
|
|
|
return $entity->getEndTime(); |
|
690
|
|
|
})->after('clearTimes'); |
|
691
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, |
|
692
|
|
|
[$this->createMock(EntityManagerInterface::class), |
|
693
|
|
|
$timeService], ['updateRankingFrom']); |
|
694
|
|
|
$service->method("getLevel")->willReturn(Level::TOURNAMENT); |
|
695
|
|
|
$service->expects(static::once()) |
|
696
|
|
|
->method('updateRankingFrom') |
|
697
|
|
|
->with($ranking, new \DateTime("2017-01-01 00:00:00")); |
|
698
|
|
|
|
|
699
|
|
|
/** @var RankingSystemService $service */ |
|
700
|
|
|
$service->updateRankingForTournament($ranking, $tournament, null); |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
/** |
|
704
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingForTournament |
|
705
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimestampableEntity |
|
706
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
707
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
708
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
709
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
710
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
711
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
712
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
713
|
|
|
*/ |
|
714
|
|
|
public function testUpdateRankingForTournamentOldEarliestIsNullAndTournamentNotRanked() |
|
715
|
|
|
{ |
|
716
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
717
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
718
|
|
|
$tournament = new Tournament(); |
|
719
|
|
|
$endedAt = new \DateTime("2017-01-01 00:00:00"); |
|
720
|
|
|
$tournament->setUpdatedAt($endedAt); |
|
721
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, |
|
722
|
|
|
[], ['updateRankingFrom']); |
|
723
|
|
|
$service->method("getLevel")->willReturn(Level::TOURNAMENT); |
|
724
|
|
|
$service->expects(self::never()) |
|
725
|
|
|
->method('updateRankingFrom'); |
|
726
|
|
|
|
|
727
|
|
|
/** @var RankingSystemService $service */ |
|
728
|
|
|
$service->updateRankingForTournament($ranking, $tournament, null); |
|
729
|
|
|
} |
|
730
|
|
|
|
|
731
|
|
|
//TODO split this up in multiple unit tests!!! |
|
732
|
|
|
|
|
733
|
|
|
/** |
|
734
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingForTournament |
|
735
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimeEntity |
|
736
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TimestampableEntity |
|
737
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
738
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
739
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
740
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestEntityInfluence |
|
741
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEarliestInfluence |
|
742
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
743
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
744
|
|
|
*/ |
|
745
|
|
|
public function testUpdateRankingForTournamentTournamentIsEarlier() |
|
746
|
|
|
{ |
|
747
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
748
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
749
|
|
|
$tournament = new Tournament(); |
|
750
|
|
|
$endedAt = new \DateTime("2017-01-01"); |
|
751
|
|
|
$tournament->setEndTime($endedAt); |
|
752
|
|
|
$tournament->getRankingSystems()->set($ranking->getId(), $ranking); |
|
753
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
754
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
755
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
756
|
|
|
return $entity->getEndTime(); |
|
757
|
|
|
})->after('clearTimes'); |
|
758
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, |
|
759
|
|
|
[$this->createMock(EntityManagerInterface::class), $timeService], ['updateRankingFrom']); |
|
760
|
|
|
$service->method("getLevel")->willReturn(Level::TOURNAMENT); |
|
761
|
|
|
$oldInfluence = new \DateTime("2017-02-01"); |
|
762
|
|
|
$service->expects(static::once()) |
|
763
|
|
|
->method('updateRankingFrom') |
|
764
|
|
|
->with($ranking, new \DateTime("2017-01-01")); |
|
765
|
|
|
|
|
766
|
|
|
/** @var RankingSystemService $service */ |
|
767
|
|
|
$service->updateRankingForTournament($ranking, $tournament, $oldInfluence); |
|
768
|
|
|
} |
|
769
|
|
|
|
|
770
|
|
|
/** |
|
771
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
772
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
773
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
774
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
775
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
776
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
777
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
778
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemListEntry |
|
779
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::cloneSubClassDataFrom |
|
780
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::initSubClassData |
|
781
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getOrCreateRankingSystemListEntry |
|
782
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::getProperty |
|
783
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\SubClassData::setProperty |
|
784
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemChange |
|
785
|
|
|
*/ |
|
786
|
|
|
public function testUpdateRankingFrom() |
|
787
|
|
|
{ |
|
788
|
|
|
//create mock for input |
|
789
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
790
|
|
|
$ranking->method('getGenerationInterval')->willReturn(-1); |
|
791
|
|
|
|
|
792
|
|
|
//create mocks for ranking lists |
|
793
|
|
|
$list1 = $this->createMock(RankingSystemList::class); |
|
794
|
|
|
$list1->method('isCurrent')->willReturn(false); |
|
795
|
|
|
$list1->method('getLastEntryTime')->willReturn(new \DateTime("2017-01-01")); |
|
796
|
|
|
$list1->method('getRankingSystem')->willReturn($ranking); |
|
797
|
|
|
|
|
798
|
|
|
$entry1 = $this->createEmptyEntry(); |
|
799
|
|
|
$entry2 = $this->createEmptyEntry(); |
|
800
|
|
|
$entry3 = $this->createEmptyEntry(); |
|
801
|
|
|
|
|
802
|
|
|
$list2 = $this->createMock(RankingSystemList::class); |
|
803
|
|
|
$list2->method('isCurrent')->willReturn(false); |
|
804
|
|
|
$list2->method('getLastEntryTime')->willReturn(new \DateTime("2017-02-01")); |
|
805
|
|
|
$list2->method('getEntries')->willReturn(new ArrayCollection([1 => $entry1, 3 => $entry3])); |
|
806
|
|
|
$list2->method('getRankingSystem')->willReturn($ranking); |
|
807
|
|
|
|
|
808
|
|
|
$list3 = $this->createMock(RankingSystemList::class); |
|
809
|
|
|
$list3->method('isCurrent')->willReturn(false); |
|
810
|
|
|
$list3->method('getLastEntryTime')->willReturn(new \DateTime("2017-03-01")); |
|
811
|
|
|
$list3->method('getEntries')->willReturn(new ArrayCollection([1 => $entry1, 2 => $entry2])); |
|
812
|
|
|
$list3->method('getRankingSystem')->willReturn($ranking); |
|
813
|
|
|
|
|
814
|
|
|
$list4 = $this->createMock(RankingSystemList::class); |
|
815
|
|
|
$list4->method('isCurrent')->willReturn(false); |
|
816
|
|
|
$list4->method('getLastEntryTime')->willReturn(new \DateTime("2017-04-01")); |
|
817
|
|
|
$list4->method('getEntries')->willReturn(new ArrayCollection()); |
|
818
|
|
|
$list4->method('getRankingSystem')->willReturn($ranking); |
|
819
|
|
|
|
|
820
|
|
|
$list5 = $this->createMock(RankingSystemList::class); |
|
821
|
|
|
$list5->method('isCurrent')->willReturn(true); |
|
822
|
|
|
$list5->method('getLastEntryTime')->willReturn(new \DateTime("2017-05-01")); |
|
823
|
|
|
$list5->method('getEntries')->willReturn(new ArrayCollection()); |
|
824
|
|
|
$list5->method('getRankingSystem')->willReturn($ranking); |
|
825
|
|
|
|
|
826
|
|
|
$lists = $this->createMock(Collection::class); |
|
827
|
|
|
$lists->expects(static::once())->method('toArray')->willReturn([$list1, $list2, $list3, $list4, $list5]); |
|
828
|
|
|
|
|
829
|
|
|
//finish mock for input |
|
830
|
|
|
$ranking->expects(static::once())->method('getLists')->willReturn($lists); |
|
831
|
|
|
|
|
832
|
|
|
//create time service, entity comparer and ranking service mock |
|
833
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
834
|
|
|
$timeService->expects(self::atLeastOnce())->method('clearTimes')->id('clearTimes'); |
|
835
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
836
|
|
|
return $entity->getEndTime(); |
|
837
|
|
|
})->after('clearTimes'); |
|
838
|
|
|
$entityComparer = $this->createMock(EntityComparerInterface::class); |
|
839
|
|
|
$entityComparer->method('compareEntities')->willReturnCallback( |
|
840
|
|
|
function (TournamentHierarchyInterface $entity1, TournamentHierarchyInterface $entity2) { |
|
841
|
|
|
return $entity1->getEndTime() <=> $entity2->getEndTime(); |
|
842
|
|
|
}); |
|
843
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([], |
|
844
|
|
|
'SELECT c FROM Tfboe\FmLib\Entity\RankingSystemChangeInterface c WHERE c.hierarchyEntity IN(:entities)', |
|
845
|
|
|
['persist', 'remove']); |
|
846
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, |
|
847
|
|
|
[$entityManager, |
|
848
|
|
|
$timeService, |
|
849
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
850
|
|
|
$this->getObjectCreator()]); |
|
851
|
|
|
|
|
852
|
|
|
//create entities mocks |
|
853
|
|
|
$entity1 = $this->createStubWithId(TournamentHierarchyEntity::class, "e1"); |
|
854
|
|
|
$entity1->method('getEndTime')->willReturn(new \DateTime("2017-03-01")); |
|
855
|
|
|
|
|
856
|
|
|
$entity2 = $this->createStubWithId(TournamentHierarchyEntity::class, "e2"); |
|
857
|
|
|
$entity2->method('getEndTime')->willReturn(new \DateTime("2017-02-01 00:00:01")); |
|
858
|
|
|
|
|
859
|
|
|
$entity3 = $this->createStubWithId(TournamentHierarchyEntity::class, "e3"); |
|
860
|
|
|
$entity3->method('getEndTime')->willReturn(new \DateTime("2017-05-02")); |
|
861
|
|
|
|
|
862
|
|
|
$entity4 = $this->createStubWithId(TournamentHierarchyEntity::class, "e4"); |
|
863
|
|
|
$entity4->method('getEndTime')->willReturn(new \DateTime("2017-03-02")); |
|
864
|
|
|
|
|
865
|
|
|
$parent = $this->createStubWithId(TournamentHierarchyEntity::class, "e4"); |
|
866
|
|
|
$parent->method('getEndTime')->willReturn(new \DateTime("2017-12-02")); |
|
867
|
|
|
$entity4->method('getParent')->willReturn($parent); |
|
868
|
|
|
|
|
869
|
|
|
//create query mock for getEntities |
|
870
|
|
|
$query = $this->createMock(AbstractQuery::class); |
|
871
|
|
|
$query->expects(static::once())->method('getResult')->willReturn([$entity1, $entity2, $entity3, $entity4]); |
|
872
|
|
|
//create query builder mock for getEntities |
|
873
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
|
874
|
|
|
$queryBuilder->expects(static::once())->method('getQuery')->willReturn($query); |
|
875
|
|
|
$service->expects(static::once())->method('getEntitiesQueryBuilder') |
|
876
|
|
|
->with($ranking, new \DateTime("2017-02-01"))->willReturn($queryBuilder); |
|
877
|
|
|
$changes = [ |
|
878
|
|
|
$this->createEmptyChange(), |
|
879
|
|
|
$this->createEmptyChange(), |
|
880
|
|
|
]; |
|
881
|
|
|
$service->method('getChanges')->willReturn($changes); |
|
882
|
|
|
$service->method('getAdditionalFields')->willReturn(['additional' => 0.0]); |
|
883
|
|
|
|
|
884
|
|
|
/** @var RankingSystemService $service */ |
|
885
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
886
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
887
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2017-02-28')); |
|
888
|
|
|
} |
|
889
|
|
|
|
|
890
|
|
|
/** |
|
891
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
892
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
893
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
894
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
895
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemList |
|
896
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
897
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
898
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
899
|
|
|
*/ |
|
900
|
|
|
public function testUpdateRankingFromCalledTwice() |
|
901
|
|
|
{ |
|
902
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
903
|
|
|
$service = $this->prepareUpdateRankingFrom($ranking); |
|
904
|
|
|
|
|
905
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
906
|
|
|
|
|
907
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
908
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2017-02-28')); |
|
|
|
|
|
|
909
|
|
|
|
|
910
|
|
|
$this->expectException(PreconditionFailedException::class); |
|
911
|
|
|
|
|
912
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
913
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2017-02-28')); |
|
|
|
|
|
|
914
|
|
|
} |
|
915
|
|
|
|
|
916
|
|
|
/** |
|
917
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
918
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
919
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
920
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
921
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemList |
|
922
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
923
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
924
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\UUIDEntity::getId |
|
925
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
926
|
|
|
*/ |
|
927
|
|
|
public function testUpdateRankingFromNoCurrent() |
|
928
|
|
|
{ |
|
929
|
|
|
//create mock for input |
|
930
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class, 'r1'); |
|
931
|
|
|
$ranking->method('getGenerationInterval')->willReturn(AutomaticInstanceGeneration::MONTHLY); |
|
932
|
|
|
|
|
933
|
|
|
//create mocks for ranking lists |
|
934
|
|
|
$list = $this->createMock(RankingSystemList::class); |
|
935
|
|
|
$list->method('isCurrent')->willReturn(false); |
|
936
|
|
|
$list->method('getLastEntryTime')->willReturn(new \DateTime("2017-12-01")); |
|
937
|
|
|
$list->method('getEntries')->willReturn(new ArrayCollection()); |
|
938
|
|
|
$list->method('getRankingSystem')->willReturn($ranking); |
|
939
|
|
|
|
|
940
|
|
|
$lists = $this->createMock(Collection::class); |
|
941
|
|
|
$lists->expects(static::once())->method('toArray')->willReturn([$list]); |
|
942
|
|
|
$lists->expects(static::once())->method('set')->with('new')->willReturnSelf(); |
|
943
|
|
|
|
|
944
|
|
|
//finish mock for input |
|
945
|
|
|
$ranking->expects(static::exactly(2))->method('getLists')->willReturn($lists); |
|
946
|
|
|
|
|
947
|
|
|
//create service mock |
|
948
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([], |
|
949
|
|
|
'SELECT c FROM Tfboe\FmLib\Entity\RankingSystemChangeInterface c WHERE c.hierarchyEntity IN(:entities)', |
|
950
|
|
|
['persist']); |
|
951
|
|
|
$entityManager->expects(static::once())->method('persist')->willReturnCallback( |
|
952
|
|
|
function (RankingSystemListInterface $entity) { |
|
953
|
|
|
self::assertInstanceOf(RankingSystemList::class, $entity); |
|
954
|
|
|
self::assertTrue($entity->isCurrent()); |
|
955
|
|
|
static::getProperty(get_class($entity), 'id')->setValue($entity, 'new'); |
|
956
|
|
|
}); |
|
957
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, [$entityManager, |
|
958
|
|
|
$this->createMock(TimeServiceInterface::class), $this->createMock(EntityComparerInterface::class), |
|
959
|
|
|
$this->getObjectCreator()]); |
|
960
|
|
|
|
|
961
|
|
|
//create query mock for getEntities |
|
962
|
|
|
$query = $this->createMock(AbstractQuery::class); |
|
963
|
|
|
$query->expects(static::once())->method('getResult')->willReturn([]); |
|
964
|
|
|
//create query builder mock for getEntities |
|
965
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
|
966
|
|
|
$queryBuilder->expects(static::once())->method('getQuery')->willReturn($query); |
|
967
|
|
|
$service->expects(static::once())->method('getEntitiesQueryBuilder') |
|
968
|
|
|
->with($ranking, new \DateTime("2017-12-01"))->willReturn($queryBuilder); |
|
969
|
|
|
|
|
970
|
|
|
/** @var RankingSystemService $service */ |
|
971
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
972
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
973
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
974
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
975
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
976
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2018-02-28')); |
|
977
|
|
|
} |
|
978
|
|
|
|
|
979
|
|
|
/** |
|
980
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
981
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
982
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
983
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
984
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemList |
|
985
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
986
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
987
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\UUIDEntity::getId |
|
988
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
989
|
|
|
*/ |
|
990
|
|
|
public function testUpdateRankingCreateMonthlyLists() |
|
991
|
|
|
{ |
|
992
|
|
|
//create mock for input |
|
993
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class, 'r1'); |
|
994
|
|
|
$ranking->method('getGenerationInterval')->willReturn(AutomaticInstanceGeneration::MONTHLY); |
|
995
|
|
|
|
|
996
|
|
|
//create mocks for ranking lists |
|
997
|
|
|
$list = $this->createMock(RankingSystemList::class); |
|
998
|
|
|
$list->method('isCurrent')->willReturn(true); |
|
999
|
|
|
$list->method('getLastEntryTime')->willReturn(new \DateTime("2017-12-01")); |
|
1000
|
|
|
$list->method('getEntries')->willReturn(new ArrayCollection()); |
|
1001
|
|
|
$list->method('getRankingSystem')->willReturn($ranking); |
|
1002
|
|
|
|
|
1003
|
|
|
$lists = $this->createMock(Collection::class); |
|
1004
|
|
|
$lists->expects(static::once())->method('toArray')->willReturn([$list]); |
|
1005
|
|
|
$lists->expects(static::once())->method('set')->with('new')->willReturnSelf(); |
|
1006
|
|
|
|
|
1007
|
|
|
//create entities mocks |
|
1008
|
|
|
$entity1 = $this->createStubWithId(TournamentHierarchyEntity::class, "e1"); |
|
1009
|
|
|
$entity1->method('getEndTime')->willReturn(new \DateTime("2018-03-01")); |
|
1010
|
|
|
|
|
1011
|
|
|
$entity2 = $this->createStubWithId(TournamentHierarchyEntity::class, "e2"); |
|
1012
|
|
|
$entity2->method('getEndTime')->willReturn(new \DateTime("2018-04-01 00:00:01")); |
|
1013
|
|
|
|
|
1014
|
|
|
//finish mock for input |
|
1015
|
|
|
$ranking->expects(static::exactly(2))->method('getLists')->willReturn($lists); |
|
1016
|
|
|
|
|
1017
|
|
|
//create service mock |
|
1018
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([], |
|
1019
|
|
|
'SELECT c FROM Tfboe\FmLib\Entity\RankingSystemChangeInterface c WHERE c.hierarchyEntity IN(:entities)', |
|
1020
|
|
|
['persist']); |
|
1021
|
|
|
$entityManager->expects(static::once())->method('persist')->willReturnCallback( |
|
1022
|
|
|
function (RankingSystemListInterface $entity) { |
|
1023
|
|
|
self::assertInstanceOf(RankingSystemList::class, $entity); |
|
1024
|
|
|
static::getProperty(get_class($entity), 'id')->setValue($entity, 'new'); |
|
1025
|
|
|
}); |
|
1026
|
|
|
$timeService = $this->createMock(TimeServiceInterface::class); |
|
1027
|
|
|
$timeService->method('getTime')->willReturnCallback(function (TournamentHierarchyInterface $entity) { |
|
1028
|
|
|
return $entity->getEndTime(); |
|
1029
|
|
|
}); |
|
1030
|
|
|
$service = $this->getMockWithMockedArguments(RankingSystemService::class, [$entityManager, |
|
1031
|
|
|
$timeService, $this->createMock(EntityComparerInterface::class), |
|
1032
|
|
|
$this->getObjectCreator()]); |
|
1033
|
|
|
|
|
1034
|
|
|
//create query mock for getEntities |
|
1035
|
|
|
$query = $this->createMock(AbstractQuery::class); |
|
1036
|
|
|
$query->expects(static::once())->method('getResult')->willReturn([$entity1, $entity2]); |
|
1037
|
|
|
//create query builder mock for getEntities |
|
1038
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
|
1039
|
|
|
$queryBuilder->expects(static::once())->method('getQuery')->willReturn($query); |
|
1040
|
|
|
$service->expects(static::once())->method('getEntitiesQueryBuilder') |
|
1041
|
|
|
->with($ranking, new \DateTime("2017-12-01"))->willReturn($queryBuilder); |
|
1042
|
|
|
|
|
1043
|
|
|
/** @var RankingSystemService $service */ |
|
1044
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
1045
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
1046
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
1047
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
1048
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
1049
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2018-02-28')); |
|
1050
|
|
|
} |
|
1051
|
|
|
|
|
1052
|
|
|
/** |
|
1053
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::updateRankingFrom |
|
1054
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::recomputeBasedOn |
|
1055
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::cloneInto |
|
1056
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getNextGenerationTime |
|
1057
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\RankingSystemList |
|
1058
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
1059
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::getEntities |
|
1060
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::deleteOldChanges |
|
1061
|
|
|
*/ |
|
1062
|
|
|
public function testUpdateRankingFromNoReusable() |
|
1063
|
|
|
{ |
|
1064
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class); |
|
1065
|
|
|
$service = $this->prepareUpdateRankingFrom($ranking); |
|
1066
|
|
|
|
|
1067
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
1068
|
|
|
|
|
1069
|
|
|
/** @var RankingSystemService $service */ |
|
1070
|
|
|
/** @var RankingSystemInterface $ranking */ |
|
1071
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
1072
|
|
|
$service->updateRankingFrom($ranking, new \DateTime('2017-02-28')); |
|
|
|
|
|
|
1073
|
|
|
} |
|
1074
|
|
|
//</editor-fold desc="Public Methods"> |
|
1075
|
|
|
|
|
1076
|
|
|
//<editor-fold desc="Private Methods"> |
|
1077
|
|
|
/** |
|
1078
|
|
|
* Creates an empty RankingSystemChange |
|
1079
|
|
|
* @return MockObject|RankingSystemChangeInterface |
|
1080
|
|
|
*/ |
|
1081
|
|
|
private function createEmptyChange(): MockObject |
|
1082
|
|
|
{ |
|
1083
|
|
|
$change = $this->getMockForAbstractClass(RankingSystemChange::class, [['additional']], '', true, true, true, |
|
1084
|
|
|
['getPlayer', 'getPointsChange']); |
|
1085
|
|
|
return $change; |
|
1086
|
|
|
} |
|
1087
|
|
|
|
|
1088
|
|
|
/** |
|
1089
|
|
|
* Creates an empty RankingSystemListEntry |
|
1090
|
|
|
* @return MockObject|RankingSystemListEntryInterface |
|
1091
|
|
|
*/ |
|
1092
|
|
|
private function createEmptyEntry(): MockObject |
|
1093
|
|
|
{ |
|
1094
|
|
|
$entry = $this->getMockForAbstractClass(RankingSystemListEntry::class, [['additional']], '', true, true, true, |
|
1095
|
|
|
['getPlayer', 'getPoints']); |
|
1096
|
|
|
return $entry; |
|
1097
|
|
|
} |
|
1098
|
|
|
|
|
1099
|
|
|
/** |
|
1100
|
|
|
* Creates different entities used for create change |
|
1101
|
|
|
* @return array, a hierarchy entity, a ranking system and a player |
|
|
|
|
|
|
1102
|
|
|
*/ |
|
1103
|
|
|
private function createEntities() |
|
1104
|
|
|
{ |
|
1105
|
|
|
$entity = $this->createStubWithId(TournamentHierarchyEntity::class, 'h1'); |
|
1106
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class, 'r1'); |
|
1107
|
|
|
$player = $this->createStubWithId(Player::class, 1, 'getId'); |
|
1108
|
|
|
return [$entity, $ranking, $player]; |
|
1109
|
|
|
} |
|
1110
|
|
|
|
|
1111
|
|
|
/** |
|
1112
|
|
|
* Prepares a ranking system service for creating a change |
|
1113
|
|
|
* @param ObjectRepository|null $repository the used repository, if not set a stub which finds the empty list is used |
|
1114
|
|
|
* @return array, the service entity and its corresponding entity manager |
|
|
|
|
|
|
1115
|
|
|
*/ |
|
1116
|
|
|
private function prepareCreateChange(?ObjectRepository $repository = null) |
|
1117
|
|
|
{ |
|
1118
|
|
|
if ($repository === null) { |
|
1119
|
|
|
$repository = $this->createStub(ObjectRepository::class, ['findBy' => []]); |
|
1120
|
|
|
} |
|
1121
|
|
|
$entityManager = $this->createStub(EntityManager::class, ['getRepository' => $repository]); |
|
1122
|
|
|
/** @var $service RankingSystemService */ |
|
1123
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, [ |
|
1124
|
|
|
$entityManager, $this->createMock(TimeServiceInterface::class), |
|
1125
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
1126
|
|
|
$this->getObjectCreator() |
|
1127
|
|
|
]); |
|
1128
|
|
|
return [$service, $entityManager]; |
|
1129
|
|
|
} |
|
1130
|
|
|
|
|
1131
|
|
|
/** |
|
1132
|
|
|
* prepares a new ranking system service for update ranking from |
|
1133
|
|
|
* @param MockObject $ranking the ranking entity |
|
1134
|
|
|
* @param EntityManagerInterface|null $entityManager the entity manager or null if a mock should be used |
|
1135
|
|
|
* @return RankingSystemService |
|
1136
|
|
|
*/ |
|
1137
|
|
|
private function prepareUpdateRankingFrom(MockObject $ranking, ?EntityManagerInterface $entityManager = null) |
|
1138
|
|
|
{ |
|
1139
|
|
|
if ($entityManager === null) { |
|
1140
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([]); |
|
1141
|
|
|
} |
|
1142
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemService::class, |
|
1143
|
|
|
[$entityManager, $this->createMock(TimeServiceInterface::class), |
|
1144
|
|
|
$this->createMock(EntityComparerInterface::class), |
|
1145
|
|
|
$this->getObjectCreator()]); |
|
1146
|
|
|
|
|
1147
|
|
|
//create mocks for current lists |
|
1148
|
|
|
$list = $this->createMock(RankingSystemList::class); |
|
1149
|
|
|
$list->method('isCurrent')->willReturn(true); |
|
1150
|
|
|
$list->method('getLastEntryTime')->willReturn(new \DateTime("2017-06-01")); |
|
1151
|
|
|
$list->method('getEntries')->willReturn(new ArrayCollection()); |
|
1152
|
|
|
|
|
1153
|
|
|
$lists = $this->createMock(Collection::class); |
|
1154
|
|
|
$lists->expects(static::once())->method('toArray')->willReturn([$list]); |
|
1155
|
|
|
|
|
1156
|
|
|
//finish mock for input |
|
1157
|
|
|
$ranking->method('getLists')->willReturn($lists); |
|
1158
|
|
|
|
|
1159
|
|
|
//create query mock for getEntities |
|
1160
|
|
|
$query = $this->createMock(AbstractQuery::class); |
|
1161
|
|
|
$query->expects(static::once())->method('getResult')->willReturn([]); |
|
1162
|
|
|
//create query builder mock for getEntities |
|
1163
|
|
|
$queryBuilder = $this->createMock(QueryBuilder::class); |
|
1164
|
|
|
$queryBuilder->expects(static::once())->method('getQuery')->willReturn($query); |
|
1165
|
|
|
$service->expects(static::once())->method('getEntitiesQueryBuilder') |
|
1166
|
|
|
->with($ranking, new \DateTime("2000-01-01"))->willReturn($queryBuilder); |
|
1167
|
|
|
/** @var $service RankingSystemService */ |
|
1168
|
|
|
|
|
1169
|
|
|
return $service; |
|
1170
|
|
|
} |
|
1171
|
|
|
//</editor-fold desc="Private Methods"> |
|
1172
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.