Fix bugs in Game and add some tests

This commit is contained in:
Austen McDonald
2016-08-01 06:57:20 +00:00
parent dd9775bfd7
commit a2fb425632
3 changed files with 148 additions and 13 deletions
+17 -13
View File
@@ -6,10 +6,15 @@ namespace LotGD\Core;
use Doctrine\ORM\EntityManagerInterface;
use Monolog\Logger;
use LotGD\Core\Models\Character;
use LotGD\Core\Models\ {
Character,
CharacterViewpoint,
Scene
};
use LotGD\Core\Exceptions\ {
ActionNotException,
CharacterNotFoundException,
InvalidConfigurationException,
SceneNotFoundException
};
@@ -165,14 +170,13 @@ class Game
*/
public function getViewpoint(): CharacterViewpoint
{
$v = $this->getEntityManager()->getRepository(CharacterViewpoint::class)->find([
'owner' => $this->getCharacter()
]);
$v = $this->getCharacter()->getCharacterViewpoint();
if ($v === null) {
// No viewpoint set up for this user. Run the hook to find the default
// scene.
$context = [
'g' => $this,
'character' => $this->getCharacter(),
'scene' => null
];
@@ -183,6 +187,7 @@ class Game
throw new InvalidConfigurationException("No subscriber to h/lotgd/core/default-scene returned a scene.");
}
$v = $this->setupViewpoint($s);
$v->save($this->getEntityManager());
}
return $v;
@@ -221,6 +226,7 @@ class Game
// $nextViewpoint, including the ability to redirect the user to
// a different scene, by setting $context['redirect'] to a new action.
$context = [
'g' => $this,
'viewpoint' => $nextViewpoint,
'redirect' => null
];
@@ -240,25 +246,23 @@ class Game
* Returns a viewpoint made from a Scene $s and the current user, complete
* with actions built from the scene's children and those modified/added by
* the hook 'h/lotgd/core/actions-for/[scene-template]'.
* @param Scene $s
* @param Sc[eng] $s
*/
private function setupViewpoint(Scene $s): CharacterViewpoint
{
$v = new CharacterViewpoint([
'owner' => $this->getCharacter()
]);
$v = new CharacterViewpoint();
$v->setOwner($this->getCharacter());
$v->changeFromScene($s);
$ag = new ActionGroup('lotgd/core/default', '', 'A');
$as = array_map(function ($c) { return new Action($c->getId()); }, $s->getChildren());
$as = array_map(function ($c) { return new Action($c->getId()); }, $s->getChildren()->toArray());
$context = [
'g' => $this,
'viewpoint' => $v,
'actions' => $as
'actions' => [$ag]
];
$this->getEventManager()->publish('h/lotgd/core/actions-for/' . $s->getTemplate(), $context);
$as = $context['actions'];
$ag->setActions($as);
$v->setActions($context['actions']);
return $v;
}
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests;
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use LotGD\Core\Bootstrap;
use LotGD\Core\Configuration;
use LotGD\Core\EventHandler;
use LotGD\Core\EventManager;
use LotGD\Core\Game;
use LotGD\Core\Models\Character;
use LotGD\Core\Models\CharacterViewpoint;
use LotGD\Core\Models\Scene;
use LotGD\Core\Exceptions\CharacterNotFoundException;
use LotGD\Core\Exceptions\InvalidConfigurationException;
use LotGD\Core\Tests\ModelTestCase;
class DefaultSceneProvider implements EventHandler
{
public static function handleEvent(string $event, array &$context)
{
switch ($event) {
case 'h/lotgd/core/default-scene':
if (!isset($context['character'])) {
throw new \Exception("Key 'character' was expected on event h/lotgd/core/default-scene.");
}
$context['scene'] = $context['g']->getEntityManager()->getRepository(Scene::class)->find(1);
}
}
}
class GameTest extends ModelTestCase
{
/** @var string default data set */
protected $dataset = "game";
private $g;
public function setUp()
{
parent::setUp();
$logger = new Logger('test');
$logger->pushHandler(new NullHandler());
$this->g = new Game(new Configuration(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . getenv('LOTGD_CONFIG')), $logger, $this->getEntityManager(), new EventManager($this->getEntityManager()));
}
public function testGetCharacterException()
{
$this->expectException(CharacterNotFoundException::class);
$this->g->getCharacter();
}
public function testSetGetCharacter()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find(1);
$this->g->setCharacter($c);
$this->assertEquals($c, $this->g->getCharacter());
}
public function testGetViewpointException()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find(1);
$this->g->setCharacter($c);
// There shouldnt 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(2);
$this->g->setCharacter($c);
$this->assertNotNull($this->g->getViewpoint());
}
public function testGetViewpointDefault()
{
$c = $this->getEntityManager()->getRepository(Character::class)->find(1);
$this->g->setCharacter($c);
$this->g->getEventManager()->subscribe('/h\/lotgd\/core\/default-scene/', DefaultSceneProvider::class, 'lotgd/core/tests');
$v = $this->g->getViewpoint();
$this->assertEquals('lotgd/tests/village', $v->getTemplate());
}
}
+37
View File
@@ -0,0 +1,37 @@
characters:
-
id: 1
name: "Char without a Scene"
displayName: "A"
-
id: 2
name: "Char with a Scene"
displayName: "B"
character_viewpoints:
-
owner_id: 2
title: "The Village"
description: "This is the village."
template: "lotgd/tests/village"
data: "a:0:{}"
attachments: "a:0:{}"
actions: "a:0:{}"
scenes:
-
id: 1
title: "The Village"
description: "This is the village."
template: "lotgd/tests/village"
parent:
-
id: 2
title: "The Forest"
description: "This is a very dangerous and dark forest"
template: "lotgd/tests/forest"
parent: 1
-
id: 3
title: "The Weaponry"
description: "This is the place where you can buy awesome weapons"
template: "lotgd/tests/weaponry"
parent: 1