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\Common\Collections\AbstractLazyCollection; |
13
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use Tfboe\FmLib\Entity\CompetitionInterface; |
16
|
|
|
use Tfboe\FmLib\Entity\GameInterface; |
17
|
|
|
use Tfboe\FmLib\Entity\PhaseInterface; |
18
|
|
|
use Tfboe\FmLib\Entity\TournamentInterface; |
19
|
|
|
use Tfboe\FmLib\Service\LoadingService; |
20
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class LoadingServiceTest |
25
|
|
|
* @package Tfboe\FmLib\Tests\Unit\Service |
26
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
27
|
|
|
*/ |
28
|
|
|
class LoadingServiceTest extends UnitTestCase |
29
|
|
|
{ |
30
|
|
|
//<editor-fold desc="Public Methods"> |
31
|
|
|
/** |
32
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::__construct |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function testConstruct() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
/** @var EntityManagerInterface $entityManager */ |
37
|
|
|
$entityManager = $this->getMockForAbstractClass(EntityManagerInterface::class); |
38
|
|
|
$service = new LoadingService($entityManager); |
39
|
|
|
self::assertInstanceOf(LoadingService::class, $service); |
40
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
41
|
|
|
self::assertEquals($entityManager, self::getProperty(get_class($service), 'em')->getValue($service)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadEntities |
46
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap |
47
|
|
|
* @uses \Tfboe\FmLib\Service\LoadingService::__construct |
48
|
|
|
*/ |
49
|
|
|
public function testLoadEntitiesAlreadyLoaded() |
50
|
|
|
{ |
51
|
|
|
$service = new LoadingService($this->getEntityManagerMockForQueries([])); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$tournament = $this->createMock(TournamentInterface::class); |
54
|
|
|
$tournament->method('getEntityId')->willReturn('t'); |
55
|
|
|
$initializedCollection = new ArrayCollection(); |
56
|
|
|
$initializedCollection->__isInitialized__ = true; |
|
|
|
|
57
|
|
|
$tournament->method('getCompetitions')->willReturn($initializedCollection); |
58
|
|
|
$service->loadEntities([$tournament], [TournamentInterface::class => [["competitions"]]]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadEntities |
63
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadProperties |
64
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap |
65
|
|
|
* @uses \Tfboe\FmLib\Service\LoadingService::__construct |
66
|
|
|
*/ |
67
|
|
|
public function testLoadEntitiesDefaultPropertiesGame() |
68
|
|
|
{ |
69
|
|
|
$service = new LoadingService($this->getEntityManagerMockForQuery([], |
|
|
|
|
70
|
|
|
'SELECT t1, t2, t3 FROM Tfboe\FmLib\Entity\GameInterface t1 LEFT JOIN t1.playersA t2 LEFT JOIN ' |
71
|
|
|
. 't1.playersB t3 WHERE t1.id IN(\'g\')' |
72
|
|
|
)); |
73
|
|
|
$game = $this->createMock(GameInterface::class); |
74
|
|
|
$game->method('getEntityId')->willReturn('g'); |
75
|
|
|
|
76
|
|
|
$uninitializedLazyCollection = $this->createMock(AbstractLazyCollection::class); |
77
|
|
|
$uninitializedLazyCollection->method('isInitialized')->willReturn(false); |
78
|
|
|
|
79
|
|
|
$game->expects(self::exactly(2))->method('getPlayersA')->willReturnOnConsecutiveCalls($uninitializedLazyCollection, |
80
|
|
|
new ArrayCollection()); |
81
|
|
|
$game->expects(self::once())->method('getPlayersB')->willReturn(new ArrayCollection()); |
82
|
|
|
$service->loadEntities([$game]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadEntities |
87
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadProperties |
88
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap |
89
|
|
|
* @uses \Tfboe\FmLib\Service\LoadingService::__construct |
90
|
|
|
*/ |
91
|
|
|
public function testLoadEntitiesMultipleLevels() |
92
|
|
|
{ |
93
|
|
|
$tournament = $this->createMock(TournamentInterface::class); |
94
|
|
|
$tournament->method('getEntityId')->willReturn('t'); |
95
|
|
|
|
96
|
|
|
$competition1 = $this->createMock(CompetitionInterface::class); |
97
|
|
|
$competition1->method('getEntityId')->willReturn('c1'); |
98
|
|
|
|
99
|
|
|
$competition2 = $this->createMock(CompetitionInterface::class); |
100
|
|
|
$competition2->method('getEntityId')->willReturn('c1'); |
101
|
|
|
|
102
|
|
|
$uninitializedLazyCollection = $this->createMock(AbstractLazyCollection::class); |
103
|
|
|
$uninitializedLazyCollection->method('isInitialized')->willReturn(false); |
104
|
|
|
|
105
|
|
|
$tournament->method('getCompetitions')->willReturnOnConsecutiveCalls( |
106
|
|
|
$uninitializedLazyCollection, |
107
|
|
|
new ArrayCollection([$competition1, $competition2]) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$phase1 = $this->createMock(PhaseInterface::class); |
111
|
|
|
$phase1->method('getEntityId')->willReturn('p1'); |
112
|
|
|
|
113
|
|
|
$competition1->method('getPhases')->willReturn(new ArrayCollection([$phase1])); |
114
|
|
|
|
115
|
|
|
$phase2 = $this->createMock(PhaseInterface::class); |
116
|
|
|
$phase2->method('getEntityId')->willReturn('p2'); |
117
|
|
|
|
118
|
|
|
$competition2->method('getPhases')->willReturn(new ArrayCollection([$phase2])); |
119
|
|
|
|
120
|
|
|
$entityManager = $this->getEntityManagerMockForQueries([[], []], [ |
121
|
|
|
'SELECT t1, t2 FROM Tfboe\FmLib\Entity\TournamentInterface t1 LEFT JOIN t1.competitions t2 WHERE t1.id IN(\'t\')', |
122
|
|
|
'SELECT t1, t2 FROM Tfboe\FmLib\Entity\CompetitionInterface t1 LEFT JOIN t1.phases t2 WHERE t1.id IN(\'c1\')' |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
$service = new LoadingService($entityManager); |
|
|
|
|
126
|
|
|
$service->loadEntities([$tournament], [ |
127
|
|
|
TournamentInterface::class => [["competitions"]], |
128
|
|
|
CompetitionInterface::class => [["phases"]] |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadEntities |
134
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadProperties |
135
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap |
136
|
|
|
* @uses \Tfboe\FmLib\Service\LoadingService::__construct |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function testLoadEntitiesNullProperty() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$service = new LoadingService($this->getEntityManagerMockForQuery([], |
|
|
|
|
141
|
|
|
'SELECT t1, t2 FROM Tfboe\FmLib\Entity\TournamentInterface t1 LEFT JOIN t1.competitions t2 WHERE t1.id IN(\'t\')' |
142
|
|
|
)); |
143
|
|
|
$tournament = $this->createMock(TournamentInterface::class); |
144
|
|
|
$tournament->method('getEntityId')->willReturn('t'); |
145
|
|
|
$tournament->method('getCompetitions')->willReturn(null); |
146
|
|
|
$service->loadEntities([$tournament], [TournamentInterface::class => [["competitions"]]]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadEntities |
151
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::loadProperties |
152
|
|
|
* @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap |
153
|
|
|
* @uses \Tfboe\FmLib\Service\LoadingService::__construct |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function testLoadEntitiesSimpleProperty() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$service = new LoadingService($this->getEntityManagerMockForQuery([], |
|
|
|
|
158
|
|
|
'SELECT t1, t2 FROM Tfboe\FmLib\Entity\CompetitionInterface t1 LEFT JOIN t1.tournament t2 WHERE t1.id IN(\'c\')' |
159
|
|
|
)); |
160
|
|
|
$competition = $this->createMock(CompetitionInterface::class); |
161
|
|
|
$competition->method('getEntityId')->willReturn('c'); |
162
|
|
|
$service->loadEntities([$competition], [CompetitionInterface::class => [["tournament"]]]); |
163
|
|
|
} |
164
|
|
|
//</editor-fold desc="Public Methods"> |
165
|
|
|
} |
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.