Adding validate method to ModuleManager with one successful test. More to follow.
This commit is contained in:
+15
-2
@@ -11,6 +11,7 @@ class Game
|
||||
{
|
||||
private $entityManager;
|
||||
private $eventManager;
|
||||
private $composerManager;
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $entityManager,
|
||||
@@ -20,6 +21,18 @@ class Game
|
||||
$this->eventManager = $eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the game's composer manager.
|
||||
* @return ComposerManager The game's composer manager.
|
||||
*/
|
||||
public function getComposerManager(): ComposerManager
|
||||
{
|
||||
if ($this->composerManager === null) {
|
||||
$this->composerManager = new ComposerManager($this);
|
||||
}
|
||||
return $this->composerManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the game's entity manager.
|
||||
* @return EntityManagerInterface The game's database entity manager.
|
||||
@@ -37,7 +50,7 @@ class Game
|
||||
{
|
||||
return $this->eventManager;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the game's dice bag.
|
||||
* @return DiceBag
|
||||
@@ -46,7 +59,7 @@ class Game
|
||||
{
|
||||
return $this->diceBag;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the active character for this game run
|
||||
* @return Character
|
||||
|
||||
@@ -120,4 +120,64 @@ class ModuleManager
|
||||
public function getModules(): array {
|
||||
return $this->g->getEntityManager()->getRepository(Module::class)->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that all modules are installed correctly. Currently checks for
|
||||
* all the proper event subscriptions.
|
||||
* @return array of strings describing issues. An empty array is returned
|
||||
* on successful validation.
|
||||
*/
|
||||
public function validate(): array
|
||||
{
|
||||
$result = array();
|
||||
|
||||
$modules = $this->getModules();
|
||||
$packages = $this->g->getComposerManager()->getModulePackages();
|
||||
|
||||
// Quick validation for the count of the modules.
|
||||
$diff = count($packages) - count($modules);
|
||||
if ($diff < 0) {
|
||||
$d = -$diff;
|
||||
array_push($result, "Error: Found {$d} more installed modules than there are configured with Composer.");
|
||||
}
|
||||
if ($diff > 0) {
|
||||
array_push($result, "Error: Found {$diff} more modules configured with Composer than installed.");
|
||||
}
|
||||
|
||||
// Validate event subscriptions.
|
||||
// TODO: Replace this n^2 algorithm to valiate event subscriptions with something faster. :)
|
||||
$currentSubscriptions = $this->g->getEventManager()->getSubscriptions();
|
||||
foreach ($packages as $p) {
|
||||
$name = $p->getName();
|
||||
|
||||
$expectedSubscriptions = ModuleManager::getPackageSubscriptions($p);
|
||||
$currentSubscriptionsForThisPackage = array_filter($currentSubscriptions, function($s) use ($name) {
|
||||
return $s->getLibrary() === $name;
|
||||
});
|
||||
|
||||
if (count($currentSubscriptionsForThisPackage) > count($expectedSubscriptions)) {
|
||||
array_push($result, "Warning: There are event subscriptions for module ${name} not present in the configuration for that module.");
|
||||
}
|
||||
foreach ($expectedSubscriptions as $e) {
|
||||
$found = false;
|
||||
foreach ($currentSubscriptionsForThisPackage as $c) {
|
||||
// Count the subscriptions for this
|
||||
if ($c->getPattern() === $e['pattern'] &&
|
||||
$c->getClass() === $e['class'] &&
|
||||
$c->getLibrary() === $p->getName())
|
||||
{
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$pattern = $e['pattern'];
|
||||
$class = $e['class'];
|
||||
array_push($result, "Error: Couldn't find subscription ({$pattern} => ${class}) for module {$name}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ declare(strict_types=1);
|
||||
namespace LotGD\Core\Tests;
|
||||
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\ComposerManager;
|
||||
use LotGD\Core\EventHandler;
|
||||
use LotGD\Core\EventManager;
|
||||
use LotGD\Core\EventSubscription;
|
||||
use LotGD\Core\ModuleManager;
|
||||
use LotGD\Core\Models\Module;
|
||||
use LotGD\Core\Exceptions\ModuleAlreadyExistsException;
|
||||
@@ -274,4 +276,52 @@ class ModuleManagerTest extends ModelTestCase
|
||||
$this->assertGreaterThanOrEqual(-5, $timeDiff);
|
||||
$this->assertEquals($library, $modules[1]->getLibrary());
|
||||
}
|
||||
|
||||
public function testValidateSuccess()
|
||||
{
|
||||
$pattern = '/test\\.foo.*/';
|
||||
$class = "LotGD\\Core\\Tests\\EventManagerTestInstalledSubscriber";
|
||||
$library = 'lotgd/tests';
|
||||
$subscriptions = array(
|
||||
array(
|
||||
'pattern' => $pattern,
|
||||
'class' => $class
|
||||
),
|
||||
);
|
||||
|
||||
$p1 = $this->getMockBuilder(PackageInterface::class)
|
||||
->getMock();
|
||||
$p1->method('getName')->willReturn($library);
|
||||
$p1->method('getExtra')->willReturn(array(
|
||||
'subscriptions' => $subscriptions
|
||||
));
|
||||
|
||||
$packages = array($p1);
|
||||
|
||||
$composerManager = $this->getMockBuilder(ComposerManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getModulePackages'))
|
||||
->getMock();
|
||||
$composerManager->method('getModulePackages')->willReturn($packages);
|
||||
|
||||
$s1 = $this->getMockBuilder(EventSubscription::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getPattern', 'getClass', 'getLibrary'))
|
||||
->getMock();
|
||||
$s1->method('getPattern')->willReturn($pattern);
|
||||
$s1->method('getClass')->willReturn($class);
|
||||
$s1->method('getLibrary')->willReturn($library);
|
||||
$installedSubscriptions = array($s1);
|
||||
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$eventManager->method('getSubscriptions')->willReturn($installedSubscriptions);
|
||||
|
||||
$this->game->method('getComposerManager')->willReturn($composerManager);
|
||||
$this->game->method('getEventManager')->willReturn($eventManager);
|
||||
|
||||
$r = $this->mm->validate();
|
||||
$this->assertEmpty($r);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user