Change module configs to take lotgd-namespace and lotgd-subscriptions
This commit is contained in:
@@ -30,11 +30,11 @@ class ModuleManager
|
||||
{
|
||||
$name = $package->getName();
|
||||
$extra = $package->getExtra();
|
||||
if (!empty($extra['subscriptions'])) {
|
||||
if (!empty($extra['lotgd-subscriptions'])) {
|
||||
$subscriptions = array();
|
||||
|
||||
// Minimal scrub to the subscriptions list.
|
||||
foreach ($extra['subscriptions'] as $s) {
|
||||
foreach ($extra['lotgd-subscriptions'] as $s) {
|
||||
if (!is_string($s))
|
||||
{
|
||||
$this->g->getLogger()->error("Module {$name} has invalid event subscription: {$s}.");
|
||||
@@ -71,12 +71,13 @@ class ModuleManager
|
||||
|
||||
$name = $package->getName();
|
||||
|
||||
$class = $package->getExtra()['class'];
|
||||
if (!isset($package->getExtra()['class']) ||
|
||||
!is_string($class))
|
||||
if (!isset($package->getExtra()['lotgd-namespace']) ||
|
||||
!is_string($package->getExtra()['lotgd-namespace']))
|
||||
{
|
||||
throw new KeyNotFoundException("Module {$name} is missing a valid 'class' entry in its extra field.");
|
||||
throw new KeyNotFoundException("Module {$name} is missing a valid 'lotgd-namespace' entry in its extra field.");
|
||||
}
|
||||
|
||||
$class = $package->getExtra()['lotgd-namespace'] . 'Module';
|
||||
try {
|
||||
$klass = new \ReflectionClass($class);
|
||||
} catch (\LogicException $e) {
|
||||
@@ -119,7 +120,7 @@ class ModuleManager
|
||||
// TODO: handle error cases here.
|
||||
$m->delete($this->g->getEntityManager());
|
||||
|
||||
$class = $package->getExtra()['class'];
|
||||
$class = $package->getExtra()['lotgd-namespace'] . 'Module';
|
||||
|
||||
// Unsubscribe the module's events.
|
||||
$subscriptions = ModuleManager::getPackageSubscriptions($package);
|
||||
@@ -172,7 +173,7 @@ class ModuleManager
|
||||
$currentSubscriptions = $this->g->getEventManager()->getSubscriptions();
|
||||
foreach ($packages as $p) {
|
||||
$name = $p->getName();
|
||||
$class = $p->getExtra()['class'];
|
||||
$class = $p->getExtra()['lotgd-namespace'] . 'Module';
|
||||
|
||||
$expectedSubscriptions = ModuleManager::getPackageSubscriptions($p);
|
||||
$currentSubscriptionsForThisPackage = array_filter($currentSubscriptions, function($s) use ($name) {
|
||||
|
||||
@@ -10,6 +10,7 @@ use LotGD\Core\Exceptions\WrongTypeException;
|
||||
use LotGD\Core\Exceptions\ClassNotFoundException;
|
||||
use LotGD\Core\Exceptions\SubscriptionNotFoundException;
|
||||
use LotGD\Core\Tests\ModelTestCase;
|
||||
use LotGD\Core\Tests\FakeModule\Module as FakeModule;
|
||||
|
||||
class EventManagerTestInvalidSubscriber
|
||||
{
|
||||
@@ -21,14 +22,6 @@ class EventManagerTestSubscriber implements EventHandler
|
||||
public static function handleEvent(string $event, array $context) {}
|
||||
}
|
||||
|
||||
class EventManagerTestInstalledSubscriber implements EventHandler
|
||||
{
|
||||
public static function handleEvent(string $event, array $context) {
|
||||
$context['foo'] = 'baz';
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
||||
class EventManagerTest extends ModelTestCase
|
||||
{
|
||||
/** @var string default data set */
|
||||
@@ -55,7 +48,7 @@ class EventManagerTest extends ModelTestCase
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
|
||||
$this->expectException(WrongTypeException::class);
|
||||
$em->subscribe("/test.event", 'LotGD\Core\Tests\EventManagerTestSubscriber', 'lotgd/tests');
|
||||
$em->subscribe("/test.event", FakeModule::class, 'lotgd/tests');
|
||||
}
|
||||
|
||||
public function testGetSubscriptions()
|
||||
@@ -63,7 +56,7 @@ class EventManagerTest extends ModelTestCase
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
|
||||
$pattern = "/test\\.foo.*/";
|
||||
$class = 'LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber';
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$sub = EventSubscription::create([
|
||||
@@ -84,7 +77,7 @@ class EventManagerTest extends ModelTestCase
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
|
||||
$pattern = "/test.event/";
|
||||
$class = 'LotGD\Core\Tests\EventManagerTestSubscriber';
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$em->subscribe($pattern, $class, $library);
|
||||
@@ -106,7 +99,7 @@ class EventManagerTest extends ModelTestCase
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
|
||||
$em->unsubscribe("/test\\.foo.*/", 'LotGD\Core\Tests\EventManagerTestInstalledSubscriber', 'lotgd/tests');
|
||||
$em->unsubscribe("/test\\.foo.*/", FakeModule::class, 'lotgd/tests');
|
||||
|
||||
$subscriptions = $em->getSubscriptions();
|
||||
$this->assertEmpty($subscriptions);
|
||||
@@ -117,7 +110,7 @@ class EventManagerTest extends ModelTestCase
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
|
||||
$this->expectException(SubscriptionNotFoundException::class);
|
||||
$em->unsubscribe("/notfound/", 'LotGD\Core\Tests\EventManagerTestInstalledSubscriber', 'lotgd/tests');
|
||||
$em->unsubscribe("/notfound/", FakeModule::class, 'lotgd/tests');
|
||||
}
|
||||
|
||||
public function testPublish()
|
||||
|
||||
@@ -2,9 +2,14 @@
|
||||
|
||||
namespace LotGD\Core\Tests\FakeModule;
|
||||
|
||||
use LotGD\Core\Module;
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\Module as ModuleBase;
|
||||
|
||||
class FakeModule extends Module {
|
||||
class Module implements ModuleBase {
|
||||
public static function handleEvent(string $event, array $context) {
|
||||
$context['foo'] = 'baz';
|
||||
return $context;
|
||||
}
|
||||
public static function onRegister(Game $g) {}
|
||||
public static function onUnregister(Game $g) {}
|
||||
}
|
||||
|
||||
+24
-30
@@ -16,13 +16,7 @@ use LotGD\Core\Module;
|
||||
use LotGD\Core\Exceptions\ModuleAlreadyExistsException;
|
||||
use LotGD\Core\Exceptions\ModuleDoesNotExistException;
|
||||
use LotGD\Core\Tests\ModelTestCase;
|
||||
|
||||
class ModuleManagerTestModule implements Module
|
||||
{
|
||||
public static function handleEvent(string $event, array $context) {}
|
||||
public static function onRegister(Game $g) {}
|
||||
public static function onUnregister(Game $g) {}
|
||||
}
|
||||
use LotGD\Core\Tests\FakeModule\Module as FakeModule;
|
||||
|
||||
class ModuleManagerTest extends ModelTestCase
|
||||
{
|
||||
@@ -74,7 +68,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
{
|
||||
$package = $this->getMockForAbstractClass(PackageInterface::class);
|
||||
$package->method('getExtra')->willReturn(array(
|
||||
'class' => ModuleManagerTestModule::class
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\'
|
||||
));
|
||||
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
@@ -95,14 +89,14 @@ class ModuleManagerTest extends ModelTestCase
|
||||
'/pattern1/',
|
||||
'/pattern2/'
|
||||
);
|
||||
$class = ModuleManagerTestModule::class;
|
||||
$class = FakeModule::class;
|
||||
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$package = $this->getMockForAbstractClass(PackageInterface::class);
|
||||
$package->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
@@ -128,7 +122,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
{
|
||||
$package = $this->getMockForAbstractClass(PackageInterface::class);
|
||||
$package->method('getExtra')->willReturn(array(
|
||||
'class' => ModuleManagerTestModule::class
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
));
|
||||
|
||||
$library = 'lotgd/tests2';
|
||||
@@ -156,14 +150,14 @@ class ModuleManagerTest extends ModelTestCase
|
||||
'/pattern1/',
|
||||
'/pattern2/',
|
||||
);
|
||||
$class = ModuleManagerTestModule::class;
|
||||
$class = FakeModule::class;
|
||||
|
||||
$library = 'lotgd/tests2';
|
||||
|
||||
$package = $this->getMockForAbstractClass(PackageInterface::class);
|
||||
$package->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
@@ -193,7 +187,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
public function testValidateSuccess()
|
||||
{
|
||||
$pattern = '/test\\.foo.*/';
|
||||
$class = "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
$subscriptions = array(
|
||||
$pattern,
|
||||
@@ -203,8 +197,8 @@ class ModuleManagerTest extends ModelTestCase
|
||||
->getMock();
|
||||
$p1->method('getName')->willReturn($library);
|
||||
$p1->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$packages = array($p1);
|
||||
@@ -239,7 +233,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
public function testValidateFailMissingSubscription()
|
||||
{
|
||||
$pattern = '/test\\.foo.*/';
|
||||
$class = "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
$subscriptions = array(
|
||||
$pattern,
|
||||
@@ -250,8 +244,8 @@ class ModuleManagerTest extends ModelTestCase
|
||||
->getMock();
|
||||
$p1->method('getName')->willReturn($library);
|
||||
$p1->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$packages = array($p1);
|
||||
@@ -286,7 +280,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
public function testValidateFailMoreInstalledModules()
|
||||
{
|
||||
$pattern = '/test\\.foo.*/';
|
||||
$class = "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$packages = array();
|
||||
@@ -321,7 +315,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
public function testValidateFailMoreExpectedModules()
|
||||
{
|
||||
$pattern = '/test\\.foo.*/';
|
||||
$class = "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$subscriptions = array(
|
||||
@@ -332,16 +326,16 @@ class ModuleManagerTest extends ModelTestCase
|
||||
->getMock();
|
||||
$p1->method('getName')->willReturn($library);
|
||||
$p1->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$p2 = $this->getMockBuilder(PackageInterface::class)
|
||||
->getMock();
|
||||
$p2->method('getName')->willReturn('lotgd/tests-another');
|
||||
$p2->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$packages = array($p1, $p2);
|
||||
@@ -376,7 +370,7 @@ class ModuleManagerTest extends ModelTestCase
|
||||
public function testValidateWarningUnconfiguredSubscriptions()
|
||||
{
|
||||
$pattern = '/test\\.foo.*/';
|
||||
$class = "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$subscriptions = array(
|
||||
@@ -387,8 +381,8 @@ class ModuleManagerTest extends ModelTestCase
|
||||
->getMock();
|
||||
$p1->method('getName')->willReturn($library);
|
||||
$p1->method('getExtra')->willReturn(array(
|
||||
'class' => $class,
|
||||
'subscriptions' => $subscriptions
|
||||
'lotgd-namespace' => 'LotGD\\Core\\Tests\\FakeModule\\',
|
||||
'lotgd-subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$packages = array($p1);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
event_subscriptions:
|
||||
-
|
||||
pattern: "/test\\.foo.*/"
|
||||
class: "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber"
|
||||
class: "LotGD\\Core\\Tests\\FakeModule\\Module"
|
||||
library: "lotgd/tests"
|
||||
|
||||
Reference in New Issue
Block a user