Added EventContextData containers.

This commit is contained in:
Vassyli
2017-04-07 10:47:43 +02:00
parent 214b1de95f
commit e6e9e6e102
12 changed files with 187 additions and 60 deletions
+10 -4
View File
@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace LotGD\Core\Tests;
use LotGD\Core\Events\EventContext;
use LotGD\Core\Events\EventContextData;
use LotGD\Core\Game;
use LotGD\Core\EventManager;
use LotGD\Core\EventHandler;
@@ -20,7 +22,9 @@ class EventManagerTestInvalidSubscriber
class EventManagerTestSubscriber implements EventHandler
{
public static function handleEvent(Game $g, string $event, array &$context) {}
public static function handleEvent(Game $g, EventContext $context): EventContext {
return $context;
}
}
class EventManagerTest extends CoreModelTestCase
@@ -129,9 +133,11 @@ class EventManagerTest extends CoreModelTestCase
$em = new EventManager($this->g);
$event = 'test.foo.something_here';
$context = array('foo' => 'bar');
$contextData = EventContextData::create(["foo" => "bar"]);
$em->publish($event, $context);
$this->assertEquals($context['foo'], 'baz');
// The event is expected to change foo from bar to baz.
$contextDataModified = $em->publish($event, $contextData);
$this->assertNotSame($contextData, $contextDataModified);
$this->assertEquals("baz", $contextDataModified->get("foo"));
}
}
+4 -2
View File
@@ -3,12 +3,14 @@
namespace LotGD\Core\Tests\FakeModule;
use LotGD\Core\Game;
use LotGD\Core\Events\EventContext;
use LotGD\Core\Module as ModuleInterface;
use LotGD\Core\Models\Module as ModuleModel;
class Module implements ModuleInterface {
public static function handleEvent(Game $g, string $event, array &$context) {
$context['foo'] = 'baz';
public static function handleEvent(Game $g, EventContext $context): EventContext
{
$context->setDataField("foo", "baz");
return $context;
}
public static function onRegister(Game $g, ModuleModel $module) {}
+23 -26
View File
@@ -8,26 +8,16 @@ use Doctrine\ORM\EntityManager;
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use LotGD\Core\Action;
use LotGD\Core\ActionGroup;
use LotGD\Core\Bootstrap;
use LotGD\Core\Configuration;
use LotGD\Core\ComposerManager;
use LotGD\Core\DiceBag;
use LotGD\Core\EventHandler;
use LotGD\Core\EventManager;
use LotGD\Core\Game;
use LotGD\Core\TimeKeeper;
use LotGD\Core\ModuleManager;
use LotGD\Core\Models\Character;
use LotGD\Core\Models\Viewpoint;
use LotGD\Core\Models\Scene;
use LotGD\Core\Exceptions\ {
ActionNotFoundException,
CharacterNotFoundException,
InvalidConfigurationException
use LotGD\Core\{
Action, ActionGroup, Bootstrap, Configuration, ComposerManager, DiceBag, EventHandler, EventManager, Events\NewViewpoint, Game, TimeKeeper, ModuleManager
};
use LotGD\Core\Tests\CoreModelTestCase;
use LotGD\Core\Models\{
Character, Viewpoint, Scene
};
use LotGD\Core\Exceptions\ {
ActionNotFoundException, CharacterNotFoundException, InvalidConfigurationException
};
use LotGD\Core\Events\EventContext;
class DefaultSceneProvider implements EventHandler
{
@@ -35,17 +25,22 @@ class DefaultSceneProvider implements EventHandler
public static $attachments = ['actions'];
public static $data = ['data'];
public static function handleEvent(Game $g, string $event, array &$context)
public static function handleEvent(Game $g, EventContext $context): EventContext
{
switch ($event) {
switch ($context->getEvent()) {
case 'h/lotgd/core/default-scene':
if (!isset($context['character'])) {
throw new \Exception("Key 'character' was expected on event h/lotgd/core/default-scene.");
if (!$context->hasDataType(NewViewpoint::class)) {
throw new \Exception(sprintf(
"Context was expected to be %s, %s instead.",
NewViewpoint::class,
get_class($context->getData())
));
}
$context['scene'] = $g->getEntityManager()->getRepository(Scene::class)->find(1);
$context->setDataField("scene", $g->getEntityManager()->getRepository(Scene::class)->find(1));
break;
case 'h/lotgd/core/navigate-to/lotgd/tests/village':
$v = $context['viewpoint'];
$v = $context->getDataField('viewpoint');
self::$actionGroups = [new ActionGroup('default', 'Title', 0)];
self::$actionGroups[0]->setActions([
@@ -58,6 +53,8 @@ class DefaultSceneProvider implements EventHandler
$v->setData(self::$data);
break;
}
return $context;
}
}
@@ -124,7 +121,7 @@ class GameTest extends CoreModelTestCase
$c = $this->getEntityManager()->getRepository(Character::class)->find(1);
$this->g->setCharacter($c);
// There shouldnt be any listeners to provide a default scene.
// There should'nt be any listeners to provide a default scene.
$this->expectException(InvalidConfigurationException::class);
$this->g->getViewpoint();
}