|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
use App\Adventure\Utils\GameSetupManager; |
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Doctrine\Persistence\ObjectManager; |
|
7
|
|
|
use Doctrine\Persistence\ObjectRepository; |
|
8
|
|
|
use App\Entity\Game\Location as LocationEntity; |
|
9
|
|
|
use App\Entity\Game\Item as ItemEntity; |
|
10
|
|
|
use App\Entity\Game\Action; |
|
11
|
|
|
use App\Entity\Game\Response; |
|
12
|
|
|
use App\Entity\Game\Connection; |
|
13
|
|
|
use App\Adventure\Game; |
|
14
|
|
|
use App\Adventure\Location; |
|
15
|
|
|
use App\Adventure\Item; |
|
16
|
|
|
use App\Adventure\InputActions\UseAction; |
|
17
|
|
|
use App\Adventure\InputActions\ExamineAction; |
|
18
|
|
|
use App\Adventure\ActionResponses\SwapLocationResponse; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Unit tests for GameSetupManager class. |
|
22
|
|
|
*/ |
|
23
|
|
|
class GameSetupManagerTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Test case for setupGameFromDatabase method. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testSetupGameFromDatabase(): void |
|
29
|
|
|
{ |
|
30
|
|
|
// Mock the manager |
|
31
|
|
|
$objectManager = $this->createMock(ObjectManager::class); |
|
32
|
|
|
|
|
33
|
|
|
// Set up the mock to return sample data from repositories |
|
34
|
|
|
$objectManager->method('getRepository')->willReturnMap([ |
|
35
|
|
|
[LocationEntity::class, $this->getLocationRepositoryMock()], |
|
36
|
|
|
[ItemEntity::class, $this->getItemRepositoryMock()], |
|
37
|
|
|
[Action::class, $this->getActionRepositoryMock()], |
|
38
|
|
|
[Response::class, $this->getResponseRepositoryMock()], |
|
39
|
|
|
[Connection::class, $this->getConnectionRepositoryMock()], |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
// Create an instance of GameSetupManager with the mock manager |
|
43
|
|
|
$gameSetupManager = new GameSetupManager($objectManager); |
|
44
|
|
|
|
|
45
|
|
|
// Configure game object |
|
46
|
|
|
$game = $gameSetupManager->setupGameFromDatabase(); |
|
47
|
|
|
|
|
48
|
|
|
// Assert that the returned object is a Game instance |
|
49
|
|
|
$this->assertInstanceOf(Game::class, $game); |
|
50
|
|
|
|
|
51
|
|
|
// Perform additional assertion to check if the game object is configured as expected |
|
52
|
|
|
$forrest = $game->getCurrentLocation(); |
|
53
|
|
|
$this->assertInstanceOf(Location::class, $forrest); |
|
54
|
|
|
$this->assertSame('Forrest', $forrest->getName()); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue($forrest->hasConnection('north')); // To the Cave |
|
57
|
|
|
$this->assertFalse($forrest->hasConnection('south')); // No connection |
|
58
|
|
|
$this->assertInstanceOf(Location::class, $forrest->getConnectedLocation('north')); |
|
59
|
|
|
$this->assertNull($forrest->getConnectedLocation('south')); |
|
60
|
|
|
|
|
61
|
|
|
$cave = $forrest->getConnectedLocation('north'); |
|
62
|
|
|
$this->assertSame('Cave', $cave->getName()); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertTrue($cave->hasItem('Axe')); |
|
65
|
|
|
$this->assertFalse($cave->hasItem('Sword')); |
|
66
|
|
|
$this->assertNull($cave->getItem('Sword')); |
|
67
|
|
|
|
|
68
|
|
|
/** @var Item */ |
|
69
|
|
|
$axe = $cave->getItem('Axe'); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame('Axe', $axe->getName()); |
|
72
|
|
|
$this->assertTrue($axe->hasAction('take')); |
|
73
|
|
|
|
|
74
|
|
|
$examineAxe = $axe->getAction('examine'); |
|
75
|
|
|
$this->assertInstanceOf(ExamineAction::class, $examineAxe); |
|
76
|
|
|
$this->assertNull($examineAxe->getRequiredLocation()); |
|
77
|
|
|
$this->assertNull($examineAxe->getLocationResponse()); |
|
78
|
|
|
|
|
79
|
|
|
$useAxe = $axe->getAction('use'); |
|
80
|
|
|
$this->assertInstanceOf(UseAction::class, $useAxe); |
|
81
|
|
|
$this->assertSame($forrest, $useAxe->getRequiredLocation()); |
|
82
|
|
|
$this->assertInstanceOf(SwapLocationResponse::class, $useAxe->getLocationResponse()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return ObjectRepository The mocked Location repository. |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getLocationRepositoryMock(): ObjectRepository |
|
89
|
|
|
{ |
|
90
|
|
|
$location1 = new LocationEntity(); |
|
91
|
|
|
$location1->setId(1); |
|
92
|
|
|
$location1->setName('Forrest') |
|
93
|
|
|
->setDescription('A dense forest.') |
|
94
|
|
|
->setDetails('This forest is filled with tall trees and lush vegetation.'); |
|
95
|
|
|
|
|
96
|
|
|
$location2 = new LocationEntity(); |
|
97
|
|
|
$location2->setId(2); |
|
98
|
|
|
$location2->setName('Cave') |
|
99
|
|
|
->setDescription('A dark cave.') |
|
100
|
|
|
->setDetails('The cave is damp and cold.'); |
|
101
|
|
|
|
|
102
|
|
|
$location3 = new LocationEntity(); |
|
103
|
|
|
$location3->setId(3); |
|
104
|
|
|
$location3->setName('Lake') |
|
105
|
|
|
->setDescription('A serene lake.') |
|
106
|
|
|
->setDetails('A calm lake reflecting the beauty of its surroundings.'); |
|
107
|
|
|
|
|
108
|
|
|
$locationRepository = $this->createMock(ObjectRepository::class); |
|
109
|
|
|
$locationRepository->expects($this->once()) |
|
110
|
|
|
->method('findAll') |
|
111
|
|
|
->willReturn([ |
|
112
|
|
|
$location1, |
|
113
|
|
|
$location2, |
|
114
|
|
|
$location3, |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
return $locationRepository; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return ObjectRepository The mocked ItemEntity repository. |
|
122
|
|
|
*/ |
|
123
|
|
|
private function getItemRepositoryMock(): ObjectRepository |
|
124
|
|
|
{ |
|
125
|
|
|
$item1 = new ItemEntity(); |
|
126
|
|
|
$item1 |
|
127
|
|
|
->setId(1) |
|
128
|
|
|
->setName('Sword') |
|
129
|
|
|
->setDescription('A powerful sword.') |
|
130
|
|
|
->setLocationId(1); |
|
131
|
|
|
|
|
132
|
|
|
$item2 = new ItemEntity(); |
|
133
|
|
|
$item2 |
|
134
|
|
|
->setId(2) |
|
135
|
|
|
->setName('Axe') |
|
136
|
|
|
->setDescription('A heavy axe.') |
|
137
|
|
|
->setLocationId(2); |
|
138
|
|
|
|
|
139
|
|
|
$itemRepository = $this->createMock(ObjectRepository::class); |
|
140
|
|
|
$itemRepository->expects($this->once()) |
|
141
|
|
|
->method('findAll') |
|
142
|
|
|
->willReturn([ |
|
143
|
|
|
$item1, |
|
144
|
|
|
$item2, |
|
145
|
|
|
]); |
|
146
|
|
|
|
|
147
|
|
|
return $itemRepository; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return ObjectRepository The mocked Action repository. |
|
152
|
|
|
*/ |
|
153
|
|
|
private function getActionRepositoryMock(): ObjectRepository |
|
154
|
|
|
{ |
|
155
|
|
|
$action1 = new Action(); |
|
156
|
|
|
$action1 |
|
157
|
|
|
->setId(1) |
|
158
|
|
|
->setType('take') |
|
159
|
|
|
->setItemId(2) |
|
160
|
|
|
->setDescription('You take the axe.') |
|
161
|
|
|
->setRequiredLocationId(null); |
|
162
|
|
|
|
|
163
|
|
|
$action2 = new Action(); |
|
164
|
|
|
$action2 |
|
165
|
|
|
->setId(2) |
|
166
|
|
|
->setType('examine') |
|
167
|
|
|
->setItemId(2) |
|
168
|
|
|
->setDescription('You examine the axe.') |
|
169
|
|
|
->setRequiredLocationId(null); |
|
170
|
|
|
|
|
171
|
|
|
$action3 = new Action(); |
|
172
|
|
|
$action3 |
|
173
|
|
|
->setId(3) |
|
174
|
|
|
->setType('use') |
|
175
|
|
|
->setItemId(2) |
|
176
|
|
|
->setDescription('You use the axe.') |
|
177
|
|
|
->setRequiredLocationId(1); |
|
178
|
|
|
|
|
179
|
|
|
$actionRepository = $this->createMock(ObjectRepository::class); |
|
180
|
|
|
$actionRepository->expects($this->once()) |
|
181
|
|
|
->method('findAll') |
|
182
|
|
|
->willReturn( |
|
183
|
|
|
[ |
|
184
|
|
|
$action1, |
|
185
|
|
|
$action2, |
|
186
|
|
|
$action3, |
|
187
|
|
|
] |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
return $actionRepository; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return ObjectRepository The mocked Response repository. |
|
195
|
|
|
*/ |
|
196
|
|
|
private function getResponseRepositoryMock(): ObjectRepository |
|
197
|
|
|
{ |
|
198
|
|
|
$response = new Response(); |
|
199
|
|
|
$response |
|
200
|
|
|
->setId(1) |
|
201
|
|
|
->setActionId(3) |
|
202
|
|
|
->setType('swap') |
|
203
|
|
|
->setLocationId(3); |
|
204
|
|
|
|
|
205
|
|
|
$responseRepository = $this->createMock(ObjectRepository::class); |
|
206
|
|
|
$responseRepository->expects($this->once()) |
|
207
|
|
|
->method('findAll') |
|
208
|
|
|
->willReturn( |
|
209
|
|
|
[ |
|
210
|
|
|
$response, |
|
211
|
|
|
] |
|
212
|
|
|
); |
|
213
|
|
|
|
|
214
|
|
|
return $responseRepository; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return ObjectRepository The mocked Connection repository. |
|
219
|
|
|
*/ |
|
220
|
|
|
private function getConnectionRepositoryMock(): ObjectRepository |
|
221
|
|
|
{ |
|
222
|
|
|
$connection1 = new Connection(); |
|
223
|
|
|
$connection1 |
|
224
|
|
|
->setId(1) |
|
225
|
|
|
->setFromLocationId(1) |
|
226
|
|
|
->setToLocationId(2) |
|
227
|
|
|
->setDirection('north'); |
|
228
|
|
|
|
|
229
|
|
|
$connection2 = new Connection(); |
|
230
|
|
|
$connection2 |
|
231
|
|
|
->setId(2) |
|
232
|
|
|
->setFromLocationId(2) |
|
233
|
|
|
->setToLocationId(1) |
|
234
|
|
|
->setDirection('south'); |
|
235
|
|
|
|
|
236
|
|
|
$connectionRepository = $this->createMock(ObjectRepository::class); |
|
237
|
|
|
$connectionRepository->expects($this->once()) |
|
238
|
|
|
->method('findAll') |
|
239
|
|
|
->willReturn( |
|
240
|
|
|
[ |
|
241
|
|
|
$connection1, |
|
242
|
|
|
$connection2, |
|
243
|
|
|
] |
|
244
|
|
|
); |
|
245
|
|
|
|
|
246
|
|
|
return $connectionRepository; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|