Refactor handleEvent() to pass in the Game always.
This commit is contained in:
+30
-19
@@ -3,12 +3,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Tests;
|
||||
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\EventManager;
|
||||
use LotGD\Core\Models\EventSubscription;
|
||||
use LotGD\Core\EventHandler;
|
||||
use LotGD\Core\Exceptions\WrongTypeException;
|
||||
use LotGD\Core\Exceptions\ClassNotFoundException;
|
||||
use LotGD\Core\Exceptions\SubscriptionNotFoundException;
|
||||
use LotGD\Core\Models\EventSubscription;
|
||||
use LotGD\Core\Tests\ModelTestCase;
|
||||
use LotGD\Core\Tests\FakeModule\Module as FakeModule;
|
||||
|
||||
@@ -19,7 +20,7 @@ class EventManagerTestInvalidSubscriber
|
||||
|
||||
class EventManagerTestSubscriber implements EventHandler
|
||||
{
|
||||
public static function handleEvent(string $event, array &$context) {}
|
||||
public static function handleEvent(Game $g, string $event, array &$context) {}
|
||||
}
|
||||
|
||||
class EventManagerTest extends ModelTestCase
|
||||
@@ -27,9 +28,19 @@ class EventManagerTest extends ModelTestCase
|
||||
/** @var string default data set */
|
||||
protected $dataset = "eventManager";
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->g = $this->getMockBuilder(Game::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->g->method('getEntityManager')->willReturn($this->getEntityManager());
|
||||
}
|
||||
|
||||
public function testSubscribeNoClass()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(ClassNotFoundException::class);
|
||||
$em->subscribe("/test.event/", 'LotGD\Core\Tests\NoClassHere', 'lotgd/tests');
|
||||
@@ -37,7 +48,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testSubscribeInvalidClass()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(WrongTypeException::class);
|
||||
$em->subscribe("/test.event/", 'LotGD\Core\Tests\EventManagerTestInvalidSubscriber', 'lotgd/tests');
|
||||
@@ -45,7 +56,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testSubscribeInvalidRegexp()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(WrongTypeException::class);
|
||||
$em->subscribe("/test.event", FakeModule::class, 'lotgd/tests');
|
||||
@@ -53,28 +64,28 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testGetSubscriptions()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$pattern = "/test\\.foo.*/";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
$pattern = "/test\\.foo.*/";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$sub = EventSubscription::create([
|
||||
$sub = EventSubscription::create([
|
||||
'pattern' => $pattern,
|
||||
'class' => $class,
|
||||
'library' => $library
|
||||
]);
|
||||
]);
|
||||
|
||||
$subscriptions = $em->getSubscriptions();
|
||||
$this->assertContainsOnlyInstancesOf(EventSubscription::class, $subscriptions);
|
||||
$subscriptions = $em->getSubscriptions();
|
||||
$this->assertContainsOnlyInstancesOf(EventSubscription::class, $subscriptions);
|
||||
|
||||
// This is a little fragile, but assertContains() doesn't seem to work.
|
||||
$this->assertEquals($sub, $subscriptions[0]);
|
||||
// This is a little fragile, but assertContains() doesn't seem to work.
|
||||
$this->assertEquals($sub, $subscriptions[0]);
|
||||
}
|
||||
|
||||
public function testSubscribeSuccess()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$pattern = "/test.event/";
|
||||
$class = FakeModule::class;
|
||||
@@ -97,7 +108,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testUnsubscribeSuccess()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$em->unsubscribe("/test\\.foo.*/", FakeModule::class, 'lotgd/tests');
|
||||
|
||||
@@ -107,7 +118,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testUnsubscribeNotFound()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(SubscriptionNotFoundException::class);
|
||||
$em->unsubscribe("/notfound/", FakeModule::class, 'lotgd/tests');
|
||||
@@ -115,7 +126,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testPublish()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$event = 'test.foo.something_here';
|
||||
$context = array('foo' => 'bar');
|
||||
|
||||
Reference in New Issue
Block a user