-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGameTest.php
357 lines (286 loc) · 12.6 KB
/
GameTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests;
use Doctrine\ORM\EntityManager;
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use LotGD\Core\{Action,
ActionGroup,
Bootstrap,
Configuration,
ComposerManager,
DiceBag,
EventHandler,
EventManager,
Events\NewViewpointData,
Game,
GameBuilder,
Tests\SceneTemplates\ParameterTestSceneTemplate,
Tests\SceneTemplates\ForestSceneTemplate,
Tests\SceneTemplates\VillageSceneTemplate,
TimeKeeper,
ModuleManager};
use LotGD\Core\Models\{
Character, Viewpoint, Scene
};
use LotGD\Core\Exceptions\ {
ActionNotFoundException, CharacterNotFoundException, InvalidConfigurationException
};
use LotGD\Core\Events\EventContext;
use vierbergenlars\SemVer\version;
class DefaultSceneProvider implements EventHandler
{
public static $actionGroups;
public static $attachments = ['actions'];
public static $data = ['data'];
public static function handleEvent(Game $g, EventContext $context): EventContext
{
switch ($context->getEvent()) {
case 'h/lotgd/core/default-scene':
if (!$context->hasDataType(NewViewpointData::class)) {
throw new \Exception(sprintf(
"Context was expected to be %s, %s instead.",
NewViewpointData::class,
get_class($context->getData())
));
}
$context->setDataField("scene", $g->getEntityManager()->getRepository(Scene::class)
->find("30000000-0000-0000-0000-000000000001"));
break;
case "h/lotgd/core/navigate-to/".VillageSceneTemplate::getNavigationEvent();
$v = $context->getDataField('viewpoint');
self::$actionGroups = [new ActionGroup('default', 'Title', 0)];
self::$actionGroups[0]->setActions([
new Action("30000000-0000-0000-0000-000000000002"), // This is a real sceneId in game.yml
new Action("30000000-0000-0000-0000-000000000101"),
]);
$v->setActionGroups(self::$actionGroups);
$v->setAttachments(self::$attachments);
$v->setData(self::$data);
break;
case "h/lotgd/core/navigate-to/".ParameterTestSceneTemplate::getNavigationEvent():
/* @var Viewpoint $v //*/
$v = $context->getDataField('viewpoint');
/* @var array //*/
$p = $context->getDataField('parameters');
if ($p["foo"] === "baz") {
$v->setDescription("Parameter is baz.");
} else {
$v->setDescription("Parameter is NOT baz.");
}
break;
}
return $context;
}
}
class GameTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "game";
public $g;
public function setUp(): void
{
parent::setUp();
$logger = new Logger('test');
$logger->pushHandler(new NullHandler());
$this->g = (new GameBuilder())
->withConfiguration(new Configuration(getenv('LOTGD_TESTS_CONFIG_PATH')))
->withLogger($logger)
->withEntityManager($this->getEntityManager())
->withCwd(implode(DIRECTORY_SEPARATOR, [__DIR__, '..']))
->create();
}
public function testBasicInjection()
{
$r = $this->g->getTimeKeeper();
$this->assertInstanceOf(TimeKeeper::class, $r);
$r = $this->g->getEventManager();
$this->assertInstanceOf(EventManager::class, $r);
$r = $this->g->getEntityManager();
$this->assertInstanceOf(EntityManager::class, $r);
$r = $this->g->getComposerManager();
$this->assertInstanceOf(ComposerManager::class, $r);
$r = $this->g->getModuleManager();
$this->assertInstanceOf(ModuleManager::class, $r);
$r = $this->g->getLogger();
$this->assertInstanceOf(Logger::class, $r);
$r = $this->g->getConfiguration();
$this->assertInstanceOf(Configuration::class, $r);
$r = $this->g->getDiceBag();
$this->assertInstanceOf(DiceBag::class, $r);
}
public function testGetCharacterException()
{
$this->expectException(CharacterNotFoundException::class);
$this->g->getCharacter();
}
public function testSetGetCharacter()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000001");
$this->g->setCharacter($c);
$this->assertEquals($c, $this->g->getCharacter());
}
public function testGetViewpointException()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000001");
$this->g->setCharacter($c);
// There should'nt be any listeners to provide a default scene.
$this->expectException(InvalidConfigurationException::class);
$this->g->getViewpoint();
}
public function testGetViewpointStored()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000002");
$this->g->setCharacter($c);
$this->assertNotNull($this->g->getViewpoint());
}
public function testGetViewpointDefault()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000001");
$this->g->setCharacter($c);
$this->g->getEventManager()->subscribe('/h\/lotgd\/core\/default-scene/', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->g->getEventManager()->subscribe('/h\/lotgd\/core\/navigate-to\/.*/', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->getEntityManager()->flush();
$v = $this->g->getViewpoint();
// Run it twice to make sure no additional DB operations happen.
/** @var Viewpoint $v */
$v = $this->g->getViewpoint();
$this->assertSame(VillageSceneTemplate::class, $v->getTemplate()->getClass());
// Validate the changes made by the hook.
$this->assertSame(DefaultSceneProvider::$actionGroups, $v->getActionGroups());
$this->assertSame(DefaultSceneProvider::$attachments, $v->getAttachments());
$this->assertSame(DefaultSceneProvider::$data, $v->getData());
$this->g->getEventManager()->unsubscribe('/h\/lotgd\/core\/navigate-to\/.*/', DefaultSceneProvider::class, 'lotgd/core/tests');
}
public function testTakeActionNonExistant()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000001");
$this->g->setCharacter($c);
// For now, I cant seem to serialize a proper ActionGroup to store in
// the yaml for this test suite, so build one naturally :)
$v = $this->g->getViewpoint();
$this->expectException(ActionNotFoundException::class);
$this->g->takeAction('non-existent');
}
public function testTakeActionNavigate()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000003");
$this->g->setCharacter($c);
// For now, I cant seem to serialize a proper ActionGroup to store in
// the yaml for this test suite, so build one naturally :)
$v = $this->g->getViewpoint();
$a = $v->getActionGroups()[0]->getActions()[0];
$this->assertNotNull($a);
$s = $this->getEntityManager()->find(Scene::class, $a->getDestinationSceneId());
$this->assertNotNull($s);
$this->g->takeAction($a->getId());
$v = $this->g->getViewpoint();
$this->assertSame($s->getTemplate(), $v->getTemplate());
}
public function testIfActionParametersAreRelayedToEvent()
{
/* @var $c Character */
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000002");
$this->g->setCharacter($c);
// subscribe event
$this->g->getEventManager()->subscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->getEntityManager()->flush();
$action = new Action("30000000-0000-0000-0000-000000000007", null, ["foo" => "baz"]);
$actionId = $action->getId();
$ag = new ActionGroup("group1", "Group 1", 5);
$ag->addAction($action);
$v = $c->getViewpoint();
$v->setDescription("Test");
$v->setActionGroups([$ag]);
$c->setViewpoint($v);
$this->g->takeAction($actionId);
$v = $c->getViewpoint();
$this->assertSame("Parameter is baz.", $v->getDescription());
// unsubscribe event
$this->g->getEventManager()->unsubscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
}
public function testIfActionParametersTakePriorityToOtherParameters()
{
/* @var $c Character */
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000002");
$this->g->setCharacter($c);
// subscribe event
$this->g->getEventManager()->subscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->getEntityManager()->flush();
$action = new Action("30000000-0000-0000-0000-000000000007", null, ["foo" => "baz"]);
$actionId = $action->getId();
$ag = new ActionGroup("group1", "Group 1", 5);
$ag->addAction($action);
$v = $c->getViewpoint();
$v->setDescription("Test");
$v->setActionGroups([$ag]);
$c->setViewpoint($v);
$this->g->takeAction($actionId, ["foo" => "nobaz"]);
$v = $c->getViewpoint();
$this->assertSame("Parameter is baz.", $v->getDescription());
// unsubscribe event
$this->g->getEventManager()->unsubscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
}
public function testIfActionsAreAddedAsExpected()
{
$viewpointToArray = function(Viewpoint $v) {
$returnTree = [];
foreach ($v->getActionGroups() as $actionGroup) {
$returnTree[$actionGroup->getId()] = [];
foreach ($actionGroup->getActions() as $action) {
$returnTree[$actionGroup->getId()][] = $action->getDestinationSceneId();
}
}
return [$v->getTitle(), $returnTree];
};
$sortedValues = function(array $array) {
$values = array_values($array);
sort($values);
return $values;
};
$c = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000003");
$this->g->setCharacter($c);
$v0 = $this->g->getViewpoint();
$this->g->takeAction($v0->getActionGroups()[0]->getActions()[2]->getId());
$v1 = $this->g->getViewpoint();
$this->assertSame([
"Parent Scene",
[
ActionGroup::DefaultGroup => ["30000000-0000-0000-0000-000000000001"],
"lotgd/tests/none/child1" => ["30000000-0000-0000-0000-000000000005"],
"lotgd/tests/none/child2" => ["30000000-0000-0000-0000-000000000006"],
ActionGroup::HiddenGroup => [],
]
], $viewpointToArray($v1));
$this->g->takeAction($v1->getActionGroups()[1]->getActions()[0]->getId());
$v2 = $this->g->getviewpoint();
$this->assertSame([
"Child Scene 1",
[
ActionGroup::DefaultGroup => [
"30000000-0000-0000-0000-000000000006",
"30000000-0000-0000-0000-000000000004"
],
ActionGroup::HiddenGroup => [],
]
], $viewpointToArray($v2));
$this->g->takeAction($v1->getActionGroups()[0]->getActions()[0]->getId());
$v3 = $this->g->getviewpoint();
$this->assertSame([
"Child Scene 2",
[
ActionGroup::DefaultGroup => ["30000000-0000-0000-0000-000000000004"],
ActionGroup::HiddenGroup => [],
]
], $viewpointToArray($v3));
}
public function testCorrectVersioning()
{
try {
$version = new version(Game::getVersion());
$this->assertTrue(true);
} catch(\RuntimeException $e) {
$this->fail(sprintf("Version failed SemVer check with error: %s", $e->getMessage()));
}
}
}