|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 1/3/18 |
|
7
|
|
|
* Time: 3:53 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Service; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
13
|
|
|
use Tfboe\FmLib\Entity\LastRecalculationInterface; |
|
14
|
|
|
use Tfboe\FmLib\Service\DynamicServiceLoadingService; |
|
15
|
|
|
use Tfboe\FmLib\Service\DynamicServiceLoadingServiceInterface; |
|
16
|
|
|
use Tfboe\FmLib\Service\RankingSystem\RankingSystemInterface; |
|
17
|
|
|
use Tfboe\FmLib\Service\RankingSystemService; |
|
18
|
|
|
use Tfboe\FmLib\Tests\Entity\Competition; |
|
19
|
|
|
use Tfboe\FmLib\Tests\Entity\Game; |
|
20
|
|
|
use Tfboe\FmLib\Tests\Entity\Match; |
|
21
|
|
|
use Tfboe\FmLib\Tests\Entity\Phase; |
|
22
|
|
|
use Tfboe\FmLib\Tests\Entity\RankingSystem; |
|
23
|
|
|
use Tfboe\FmLib\Tests\Entity\Tournament; |
|
24
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class EloRankingTest |
|
29
|
|
|
* @packageTfboe\FmLib\Tests\Unit\Service |
|
30
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
31
|
|
|
*/ |
|
32
|
|
|
class RankingSystemServiceTest extends UnitTestCase |
|
33
|
|
|
{ |
|
34
|
|
|
//<editor-fold desc="Public Methods"> |
|
35
|
|
|
/** |
|
36
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::adaptOpenSyncFromValues |
|
37
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystemService::__construct |
|
38
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
39
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
40
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystemService::getRankingSystems |
|
41
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystemService::getRankingSystemsEarliestInfluences |
|
42
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
43
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
44
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testAdaptOpenSyncFromValues() |
|
47
|
|
|
{ |
|
48
|
|
|
$serviceLoader = $this->createMock(DynamicServiceLoadingService::class); |
|
49
|
|
|
$serviceLoader->expects(self::exactly(2)) |
|
50
|
|
|
->method("loadRankingSystemService") |
|
51
|
|
View Code Duplication |
->willReturnCallback(function ($earliestInfluence) { |
|
|
|
|
|
|
52
|
|
|
$mock = $this->createMock(\Tfboe\FmLib\Service\RankingSystem\RankingSystemService::class); |
|
53
|
|
|
$mock->method("getEarliestInfluence")->willReturn(new \DateTime($earliestInfluence)); |
|
|
|
|
|
|
54
|
|
|
return $mock; |
|
55
|
|
|
}); |
|
56
|
|
|
/** @var DynamicServiceLoadingService $serviceLoader */ |
|
57
|
|
|
/** @noinspection PhpParamsInspection */ |
|
58
|
|
|
$service = new RankingSystemService($serviceLoader, |
|
|
|
|
|
|
59
|
|
|
$this->getMockForAbstractClass(EntityManagerInterface::class)); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$tournament = new Tournament(); |
|
63
|
|
|
$ranking = $this->createStubWithId(RankingSystem::class, 'r1'); |
|
64
|
|
|
$ranking->method('getServiceName')->willReturn("2017-01-01"); |
|
|
|
|
|
|
65
|
|
|
$ranking->method('getOpenSyncFrom')->willReturn(new \DateTime("2017-01-01 15:00:00")); |
|
|
|
|
|
|
66
|
|
|
$ranking->expects(self::once())->method('setOpenSyncFrom')->with(new \DateTime("2017-01-01")); |
|
67
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking */ |
|
68
|
|
|
$tournament->getRankingSystems()->set($ranking->getId(), $ranking); |
|
69
|
|
|
|
|
70
|
|
|
$ranking2 = $this->createStubWithId(RankingSystem::class, 'r2'); |
|
71
|
|
|
$ranking2->method('getServiceName')->willReturn("2017-02-01"); |
|
|
|
|
|
|
72
|
|
|
$ranking2->method('getOpenSyncFrom')->willReturn(new \DateTime("2017-01-30 15:00:00")); |
|
|
|
|
|
|
73
|
|
|
$ranking2->expects(self::once())->method('setOpenSyncFrom')->with(new \DateTime("2017-01-30")); |
|
74
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking2 */ |
|
75
|
|
|
$tournament->getRankingSystems()->set($ranking2->getId(), $ranking2); |
|
76
|
|
|
|
|
77
|
|
|
$ranking3 = $this->createStubWithId(RankingSystem::class, 'r3'); |
|
78
|
|
|
$ranking3->method('getOpenSyncFrom')->willReturn(null); |
|
|
|
|
|
|
79
|
|
|
$ranking3->expects(self::once())->method('setOpenSyncFrom')->with(new \DateTime("2017-03-01")); |
|
80
|
|
|
$ranking4 = $this->createStubWithId(RankingSystem::class, 'r4'); |
|
81
|
|
|
$ranking4->method('getOpenSyncFrom')->willReturn(new \DateTime("2017-04-01")); |
|
|
|
|
|
|
82
|
|
|
$ranking4->expects(self::never())->method('setOpenSyncFrom'); |
|
83
|
|
|
|
|
84
|
|
|
$service->adaptOpenSyncFromValues($tournament, [ |
|
85
|
|
|
'r1' => ["rankingSystem" => $ranking, "earliestInfluence" => new \DateTime("2017-01-02")], |
|
86
|
|
|
'r2' => ["rankingSystem" => $ranking2, "earliestInfluence" => new \DateTime("2017-01-30")], |
|
87
|
|
|
'r3' => ["rankingSystem" => $ranking3, "earliestInfluence" => new \DateTime("2017-03-01")], |
|
88
|
|
|
'r4' => ["rankingSystem" => $ranking4, "earliestInfluence" => new \DateTime("2017-06-01")], |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::applyRankingSystems |
|
94
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::getRankingSystems |
|
95
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
96
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
97
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystemService::__construct |
|
98
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
99
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
100
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testApplyRankingSystems() |
|
103
|
|
|
{ |
|
104
|
|
|
$tournament = new Tournament(); |
|
105
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking2 */ |
|
106
|
|
|
$ranking2 = $this->createStubWithId(RankingSystem::class, 's2'); |
|
107
|
|
|
$tournament->getRankingSystems()->set($ranking2->getId(), $ranking2); |
|
108
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking3 */ |
|
109
|
|
|
$ranking3 = $this->createStubWithId(RankingSystem::class, 's3'); |
|
110
|
|
|
|
|
111
|
|
|
$tournament->getRankingSystems()->set($ranking3->getId(), $ranking3); |
|
112
|
|
|
|
|
113
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking4 */ |
|
114
|
|
|
$ranking4 = $this->createStubWithId(RankingSystem::class, 's4'); |
|
115
|
|
|
|
|
116
|
|
|
$oldInfluences = [ |
|
117
|
|
|
$ranking2->getId() => ["rankingSystem" => $ranking2, "earliestInfluence" => new \DateTime("2017-02-01")], |
|
118
|
|
|
$ranking4->getId() => ["rankingSystem" => $ranking4, "earliestInfluence" => new \DateTime("2017-04-01")] |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
$serviceLoader = $this->createMock(DynamicServiceLoadingService::class); |
|
122
|
|
|
$mock = $this->createMock(\Tfboe\FmLib\Service\RankingSystem\RankingSystemService::class); |
|
123
|
|
|
$mock->expects(self::exactly(3))->method("updateRankingForTournament")->withConsecutive( |
|
124
|
|
|
[$ranking2, $tournament, self::equalTo(new \DateTime("2017-02-01"))], |
|
125
|
|
|
[$ranking4, $tournament, self::equalTo(new \DateTime("2017-04-01"))], |
|
126
|
|
|
[$ranking3, $tournament, null] |
|
127
|
|
|
); |
|
128
|
|
|
$serviceLoader->expects(self::exactly(3)) |
|
129
|
|
|
->method("loadRankingSystemService") |
|
130
|
|
|
->willReturn($mock); |
|
131
|
|
|
|
|
132
|
|
|
/** @var DynamicServiceLoadingService $serviceLoader */ |
|
133
|
|
|
/** @noinspection PhpParamsInspection */ |
|
134
|
|
|
$service = new RankingSystemService($serviceLoader, |
|
|
|
|
|
|
135
|
|
|
$this->getMockForAbstractClass(EntityManagerInterface::class)); |
|
|
|
|
|
|
136
|
|
|
$service->applyRankingSystems($tournament, $oldInfluences); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::__construct |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testConstruct() |
|
143
|
|
|
{ |
|
144
|
|
|
$dsls = $this->getMockForAbstractClass(DynamicServiceLoadingServiceInterface::class); |
|
145
|
|
|
$entityManager = $this->getMockForAbstractClass(EntityManagerInterface::class); |
|
146
|
|
|
/** @var DynamicServiceLoadingServiceInterface $dsls */ |
|
147
|
|
|
/** @var EntityManagerInterface $entityManager */ |
|
148
|
|
|
$system = new RankingSystemService($dsls, $entityManager); |
|
|
|
|
|
|
149
|
|
|
self::assertInstanceOf(RankingSystemService::class, $system); |
|
150
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
151
|
|
|
self::assertEquals($entityManager, self::getProperty(get_class($system), 'entityManager')->getValue($system)); |
|
152
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
153
|
|
|
self::assertEquals($dsls, self::getProperty(get_class($system), 'dsls')->getValue($system)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::getRankingSystemsEarliestInfluences |
|
158
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::getRankingSystems |
|
159
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Competition |
|
160
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Competition |
|
161
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Game |
|
162
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Game |
|
163
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Match |
|
164
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Match |
|
165
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Phase |
|
166
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Phase |
|
167
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
168
|
|
|
* @uses \Tfboe\FmLib\Entity\Traits\Tournament |
|
169
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\NameEntity |
|
170
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystemService::__construct |
|
171
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystem\RankingSystemService::__construct |
|
172
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct |
|
173
|
|
|
* @uses \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems |
|
174
|
|
|
*/ |
|
175
|
|
|
public function testGetRankingSystemsEarliestInfluences() |
|
176
|
|
|
{ |
|
177
|
|
|
$serviceLoader = $this->createMock(DynamicServiceLoadingService::class); |
|
178
|
|
|
$serviceLoader->expects(self::exactly(3)) |
|
179
|
|
|
->method("loadRankingSystemService") |
|
180
|
|
View Code Duplication |
->willReturnCallback(function ($earliestInfluence) { |
|
|
|
|
|
|
181
|
|
|
$mock = $this->createMock(\Tfboe\FmLib\Service\RankingSystem\RankingSystemService::class); |
|
182
|
|
|
$mock->method("getEarliestInfluence")->willReturn(new \DateTime($earliestInfluence)); |
|
|
|
|
|
|
183
|
|
|
return $mock; |
|
184
|
|
|
}); |
|
185
|
|
|
/** @var DynamicServiceLoadingService $serviceLoader */ |
|
186
|
|
|
/** @noinspection PhpParamsInspection */ |
|
187
|
|
|
$service = new RankingSystemService($serviceLoader, |
|
|
|
|
|
|
188
|
|
|
$this->getMockForAbstractClass(EntityManagerInterface::class)); |
|
|
|
|
|
|
189
|
|
|
$tournament = new Tournament(); |
|
190
|
|
|
$ranking2 = $this->createStubWithId(RankingSystem::class, 'r2'); |
|
191
|
|
|
$ranking2->method('getServiceName')->willReturn("2017-04-01"); |
|
|
|
|
|
|
192
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking2 */ |
|
193
|
|
|
$tournament->getRankingSystems()->set($ranking2->getId(), $ranking2); |
|
194
|
|
|
|
|
195
|
|
|
$competition = new Competition(); |
|
196
|
|
|
$competition->setName("TestCompetition")->setTournament($tournament); |
|
|
|
|
|
|
197
|
|
|
$phase = new Phase(); |
|
198
|
|
|
$phase->setPhaseNumber(1); |
|
199
|
|
|
$phase->setCompetition($competition); |
|
200
|
|
|
$ranking3 = $this->createStubWithId(RankingSystem::class, 'r3'); |
|
201
|
|
|
$ranking3->method('getServiceName')->willReturn("2017-02-01"); |
|
|
|
|
|
|
202
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking3 */ |
|
203
|
|
|
|
|
204
|
|
|
$phase->getRankingSystems()->set($ranking3->getId(), $ranking3); |
|
205
|
|
|
|
|
206
|
|
|
$match = new Match(); |
|
207
|
|
|
$match->setMatchNumber(1); |
|
208
|
|
|
$match->setPhase($phase); |
|
209
|
|
|
$game = new Game(); |
|
210
|
|
|
$game->setGameNumber(1); |
|
211
|
|
|
$game->setMatch($match); |
|
212
|
|
|
$ranking4 = $this->createStubWithId(RankingSystem::class, 'r4'); |
|
213
|
|
|
$ranking4->method('getServiceName')->willReturn("2017-03-01"); |
|
|
|
|
|
|
214
|
|
|
/** @var \Tfboe\FmLib\Entity\RankingSystemInterface $ranking4 */ |
|
215
|
|
|
$game->getRankingSystems()->set($ranking4->getId(), $ranking4); |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
self::assertEquals( |
|
219
|
|
|
[ |
|
220
|
|
|
$ranking2->getId() => ["rankingSystem" => $ranking2, "earliestInfluence" => new \DateTime("2017-04-01")], |
|
221
|
|
|
$ranking3->getId() => ["rankingSystem" => $ranking3, "earliestInfluence" => new \DateTime("2017-02-01")], |
|
222
|
|
|
$ranking4->getId() => ["rankingSystem" => $ranking4, "earliestInfluence" => new \DateTime("2017-03-01")], |
|
223
|
|
|
], |
|
224
|
|
|
$service->getRankingSystemsEarliestInfluences($tournament)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @covers \Tfboe\FmLib\Service\RankingSystemService::recalculateRankingSystems |
|
229
|
|
|
* @uses \Tfboe\FmLib\Service\RankingSystemService::__construct |
|
230
|
|
|
*/ |
|
231
|
|
|
public function testRecalculateRankingSystems() |
|
232
|
|
|
{ |
|
233
|
|
|
$rs1 = $this->createMock(\Tfboe\FmLib\Entity\RankingSystemInterface::class); |
|
234
|
|
|
$rs1->expects(self::once())->method('getServiceName')->willReturn('service'); |
|
235
|
|
|
$rs1->expects(self::exactly(3))->method('getOpenSyncFrom')->willReturn(new \DateTime("2017-02-01")); |
|
236
|
|
|
$rs1->expects(self::once())->method('setOpenSyncFrom')->with(null); |
|
237
|
|
|
$rs1->expects(self::once())->method('setOpenSyncFromInProcess')->with(null); |
|
238
|
|
|
$rs2 = $this->createMock(\Tfboe\FmLib\Entity\RankingSystemInterface::class); |
|
239
|
|
|
$rs2->expects(self::once())->method('getServiceName')->willReturn('service'); |
|
240
|
|
|
$rs2->expects(self::exactly(3))->method('getOpenSyncFrom')->willReturn(new \DateTime("2017-05-01")); |
|
241
|
|
|
$rs2->expects(self::once())->method('setOpenSyncFrom')->with(null); |
|
242
|
|
|
$rs2->expects(self::once())->method('setOpenSyncFromInProcess')->with(null); |
|
243
|
|
|
$slash = '\\'; |
|
244
|
|
|
$first = 'SELECT s'; |
|
245
|
|
|
$second = ' FROM Tfboe'; |
|
246
|
|
|
$third = 'FmLib'; |
|
247
|
|
|
$rest = 'RankingSystemInterface s WHERE s.openSyncFrom IS NOT NULL OR s.openSyncFromInProcess IS NOT NULL'; |
|
248
|
|
|
$entityManager = $this->getEntityManagerMockForQuery([$rs1, $rs2], |
|
249
|
|
|
$first . $second . $slash . $third . $slash . 'Entity' . $slash . $rest, ['flush', 'clear', 'transactional', |
|
250
|
|
|
'find']); |
|
251
|
|
|
$entityManager->method('transactional')->willReturnCallback(function ($f) use ($entityManager) { |
|
|
|
|
|
|
252
|
|
|
return $f($entityManager); |
|
253
|
|
|
}); |
|
254
|
|
|
$lastRecalculation = $this->createMock(LastRecalculationInterface::class); |
|
255
|
|
|
$entityManager->method('find')->willReturn($lastRecalculation); |
|
|
|
|
|
|
256
|
|
|
$dsls = $this->getMockForAbstractClass(DynamicServiceLoadingServiceInterface::class); |
|
257
|
|
|
$service = $this->getMockForAbstractClass(RankingSystemInterface::class); |
|
258
|
|
|
$service->expects(self::exactly(2))->method('updateRankingFrom') |
|
259
|
|
|
->withConsecutive([$rs1, new \DateTime("2017-02-01")], [$rs2, new \DateTime("2017-05-01")]); |
|
260
|
|
|
$dsls->expects(self::exactly(2))->method('loadRankingSystemService')->with('service')->willReturn($service); |
|
261
|
|
|
/** @var DynamicServiceLoadingServiceInterface $dsls */ |
|
262
|
|
|
/** @var EntityManagerInterface $entityManager */ |
|
263
|
|
|
$system = new RankingSystemService($dsls, $entityManager); |
|
|
|
|
|
|
264
|
|
|
$system->recalculateRankingSystems(); |
|
265
|
|
|
} |
|
266
|
|
|
//</editor-fold desc="Public Methods"> |
|
267
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.