Adds new commands to manage modules and corresponding tests.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Module;
|
||||
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
use LotGD\Core\Console\Command\BaseCommand;
|
||||
use LotGD\Core\Models\Module;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
||||
class ModuleBaseCommand extends BaseCommand
|
||||
{
|
||||
protected ?string $namespace = "module";
|
||||
|
||||
/**
|
||||
* @return InputArgument
|
||||
*/
|
||||
protected function getModuleNameArgumentDefinition(): InputArgument
|
||||
{
|
||||
return new InputArgument(
|
||||
name: "moduleName",
|
||||
mode: InputArgument::REQUIRED,
|
||||
description: "Name of the module, in vendor/package format",
|
||||
);
|
||||
}
|
||||
|
||||
protected function getModuleRepository(): ObjectRepository
|
||||
{
|
||||
return $this->game->getEntityManager()->getRepository(Module::class);
|
||||
}
|
||||
|
||||
protected function getModuleModel(InputInterface $input): ?Module
|
||||
{
|
||||
return $this->game->getModuleManager()->getModule($input->getArgument("moduleName"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Module;
|
||||
|
||||
use LotGD\Core\Events\EventContextData;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class ModuleConfigListCommand extends ModuleBaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName($this->namespaced("config:list"))
|
||||
->setDescription('List available configuration option for a module')
|
||||
->setDefinition([
|
||||
$this->getModuleNameArgumentDefinition(),
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$module = $this->getModuleModel($input);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module was not found.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Create hook
|
||||
$context = EventContextData::create([
|
||||
"module" => $module,
|
||||
"io" => $io,
|
||||
"settings" => [],
|
||||
]);
|
||||
$newContext = $this->game->getEventManager()->publish(
|
||||
event: "h/lotgd/core/cli/module-config-list/".$module->getLibrary(),
|
||||
contextData: $context
|
||||
);
|
||||
$settings = $newContext->get("settings");
|
||||
|
||||
$io->title("Module ".$module->getLibrary());
|
||||
|
||||
if (count($settings) === 0) {
|
||||
$io->note("This module does not provide any settings.");
|
||||
} else {
|
||||
$io->table(["setting", "value", "description"], $settings);
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Module;
|
||||
|
||||
use LotGD\Core\Events\EventContextData;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class ModuleConfigResetCommand extends ModuleBaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName($this->namespaced("config:reset"))
|
||||
->setDescription('Reset a module setting')
|
||||
->setDefinition([
|
||||
$this->getModuleNameArgumentDefinition(),
|
||||
new InputArgument(
|
||||
"setting",
|
||||
mode: InputArgument::REQUIRED,
|
||||
description: "Name of setting, see {$this->namespaced('config:list')}.",
|
||||
),
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$logger = $this->getCliLogger();
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$module = $this->getModuleModel($input);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module was not found.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Create hook
|
||||
$context = EventContextData::create([
|
||||
"module" => $module,
|
||||
"io" => $io,
|
||||
"setting" => $input->getArgument("setting"),
|
||||
"return" => Command::FAILURE,
|
||||
"reason" => "Setting does not exist.",
|
||||
]);
|
||||
|
||||
$newContext = $this->game->getEventManager()->publish(
|
||||
event: "h/lotgd/core/cli/module-config-reset/{$module->getLibrary()}",
|
||||
contextData: $context
|
||||
);
|
||||
if ($newContext->get("return") != Command::SUCCESS) {
|
||||
$io->error($newContext->get("reason"));
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->game->getEntityManager()->flush();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Module;
|
||||
|
||||
use LotGD\Core\Events\EventContextData;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class ModuleConfigSetCommand extends ModuleBaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName($this->namespaced("config:set"))
|
||||
->setDescription('Change a module setting')
|
||||
->setDefinition([
|
||||
$this->getModuleNameArgumentDefinition(),
|
||||
new InputArgument(
|
||||
"setting",
|
||||
mode: InputArgument::REQUIRED,
|
||||
description: "Name of setting, see {$this->namespaced('config:list')}.",
|
||||
),
|
||||
new InputArgument(
|
||||
"value",
|
||||
InputArgument::REQUIRED,
|
||||
description: "New value for the given setting.",
|
||||
),
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$logger = $this->getCliLogger();
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$module = $this->getModuleModel($input);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module was not found.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->title("Module {$module->getLibrary()}");
|
||||
|
||||
// Create hook
|
||||
$context = EventContextData::create([
|
||||
"module" => $module,
|
||||
"io" => $io,
|
||||
"setting" => $input->getArgument("setting"),
|
||||
"value" => $input->getArgument("value"),
|
||||
"return" => Command::FAILURE,
|
||||
"reason" => "Setting does not exist.",
|
||||
]);
|
||||
$newContext = $this->game->getEventManager()->publish(
|
||||
event: "h/lotgd/core/cli/module-config-set/{$module->getLibrary()}",
|
||||
contextData: $context
|
||||
);
|
||||
if ($newContext->get("return") != Command::SUCCESS) {
|
||||
$io->error($newContext->get("reason"));
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->game->getEntityManager()->flush();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Console\Command\Module;
|
||||
|
||||
use LotGD\Core\Models\Module;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class ModuleListCommand extends ModuleBaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName($this->namespaced("list"))
|
||||
->setDescription('List all installed modules.')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
/** @var Module[] $modules */
|
||||
$modules = $this->game->getModuleManager()->getModules();
|
||||
|
||||
$io->title("Installed modules");
|
||||
|
||||
if (count($modules) > 0) {
|
||||
$listing = [];
|
||||
foreach ($modules as $module) {
|
||||
$listing[] = [$module->getLibrary() => $module->getCreatedAt()->format("d M Y, H:i")];
|
||||
}
|
||||
|
||||
$io->definitionList(...$listing);
|
||||
} else {
|
||||
$io->note("No modules installed.");
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,14 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
/**
|
||||
* Danerys command to register and initiate any newly installed modules.
|
||||
*/
|
||||
class ModuleRegisterCommand extends BaseCommand
|
||||
class ModuleRegisterCommand extends ModuleBaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('module:register')
|
||||
$this->setName($this->namespaced("register"))
|
||||
->setDescription('Register and initialize any newly installed modules')
|
||||
;
|
||||
}
|
||||
|
||||
@@ -11,14 +11,14 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
/**
|
||||
* Danerys command to validate installed modules.
|
||||
*/
|
||||
class ModuleValidateCommand extends BaseCommand
|
||||
class ModuleValidateCommand extends ModuleBaseCommand
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('module:validate')
|
||||
$this->setName($this->namespaced("validate"))
|
||||
->setDescription('Validate installed modules')
|
||||
;
|
||||
}
|
||||
|
||||
+13
-2
@@ -14,6 +14,10 @@ use LotGD\Core\Console\Command\Character\CharacterShowCommand;
|
||||
use LotGD\Core\Console\Command\ConsoleCommand;
|
||||
use LotGD\Core\Console\Command\Database\DatabaseInitCommand;
|
||||
use LotGD\Core\Console\Command\Database\DatabaseSchemaUpdateCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigListCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigResetCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigSetCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleListCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleRegisterCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleValidateCommand;
|
||||
use LotGD\Core\Console\Command\SceneTemplates\SceneTemplateListCommand;
|
||||
@@ -53,12 +57,19 @@ class Main
|
||||
*/
|
||||
protected function addCommands()
|
||||
{
|
||||
$this->application->add(new ModuleValidateCommand($this->game));
|
||||
$this->application->add(new ModuleRegisterCommand($this->game));
|
||||
$this->application->add(new DatabaseInitCommand($this->game));
|
||||
$this->application->add(new DatabaseSchemaUpdateCommand($this->game));
|
||||
|
||||
$this->application->add(new ConsoleCommand($this->game));
|
||||
|
||||
// Module commands
|
||||
$this->application->add(new ModuleConfigListCommand($this->game));
|
||||
$this->application->add(new ModuleConfigResetCommand($this->game));
|
||||
$this->application->add(new ModuleConfigSetCommand($this->game));
|
||||
$this->application->add(new ModuleListCommand($this->game));
|
||||
$this->application->add(new ModuleRegisterCommand($this->game));
|
||||
$this->application->add(new ModuleValidateCommand($this->game));
|
||||
|
||||
// Character commands
|
||||
$this->application->add(new CharacterAddCommand($this->game));
|
||||
$this->application->add(new CharacterEditCommand($this->game));
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Tests\Commands\CharacterCommands;
|
||||
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigListCommand;
|
||||
use LotGD\Core\EventManager;
|
||||
use LotGD\Core\Events\EventContextData;
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\Tests\CoreModelTestCase;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
class ModuleConfigListCommandTest extends CoreModelTestCase
|
||||
{
|
||||
/** @var string default data set */
|
||||
protected $dataset = "module-2";
|
||||
|
||||
protected function getCommand(): CommandTester
|
||||
{
|
||||
return new CommandTester(new ModuleConfigListCommand($this->g));
|
||||
}
|
||||
|
||||
public function testIfCommandRunsWithoutRegisteredEvents()
|
||||
{
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => "lotgd/tests"
|
||||
]);
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
|
||||
$this->assertStringContainsString("This module does not provide any settings.", $output);
|
||||
}
|
||||
|
||||
public function testIfCommandEmitsEvent()
|
||||
{
|
||||
/** @var Game $game */
|
||||
$game = $this->g;
|
||||
|
||||
$modules = [
|
||||
"lotgd/tests",
|
||||
"lotgd/tests-other"
|
||||
];
|
||||
|
||||
foreach ($modules as $module) {
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('publish'))
|
||||
->getMock();
|
||||
$eventManager->expects($this->once())
|
||||
->method('publish')
|
||||
->with(
|
||||
$this->equalTo("h/lotgd/core/cli/module-config-list/{$module}"),
|
||||
$this->callback(function (EventContextData $eventContextData) use ($module) {
|
||||
$pass = 1;
|
||||
|
||||
$pass &= $eventContextData->has("module") === true;
|
||||
$pass &= $eventContextData->get("module")->getLibrary() === $module;
|
||||
|
||||
$pass &= $eventContextData->has("io") === true;
|
||||
$pass &= $eventContextData->get("io") instanceof SymfonyStyle;
|
||||
|
||||
$pass &= $eventContextData->has("settings") === true;
|
||||
$pass &= $eventContextData->get("settings") === [];
|
||||
|
||||
return $pass == true;
|
||||
}),
|
||||
)->will($this->returnArgument(1));
|
||||
$game->setEventManager($eventManager);
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => $module,
|
||||
]);
|
||||
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
|
||||
$this->assertStringContainsString("Module {$module}", $output);
|
||||
$this->assertStringContainsString("This module does not provide any settings.", $output);
|
||||
}
|
||||
}
|
||||
|
||||
public function testIfCommandDisplaysSettingsWhenGivenByEvent()
|
||||
{
|
||||
/** @var Game $game */
|
||||
$game = $this->g;
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('publish'))
|
||||
->getMock();
|
||||
$eventManager->expects($this->once())
|
||||
->method('publish')
|
||||
->will($this->returnCallback(function (string $a, EventContextData $b) {
|
||||
$newSettings = [
|
||||
["setting1", "0.00000000000000000000000001", "float 0..1, chance to succeed"],
|
||||
["setting2", "DragonMillions", "string, name of the lottery"],
|
||||
];
|
||||
$settings = $b->get("settings");
|
||||
$settings = [...$settings, ...$newSettings];
|
||||
|
||||
return $b->set("settings", $settings);
|
||||
}));
|
||||
$game->setEventManager($eventManager);
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => "lotgd/tests"
|
||||
]);
|
||||
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
|
||||
$this->assertStringContainsString("setting1", $output);
|
||||
$this->assertStringContainsString("0.00000000000000000000000001", $output);
|
||||
$this->assertStringContainsString("float 0..1, chance to succeed", $output);
|
||||
$this->assertStringContainsString("setting2", $output);
|
||||
$this->assertStringContainsString("DragonMillions", $output);
|
||||
$this->assertStringContainsString("string, name of the lottery", $output);
|
||||
$this->assertStringNotContainsString("This module does not provide any settings.", $output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Tests\Commands\CharacterCommands;
|
||||
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigListCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigSetCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleListCommand;
|
||||
use LotGD\Core\EventHandler;
|
||||
use LotGD\Core\EventManager;
|
||||
use LotGD\Core\Events\EventContext;
|
||||
use LotGD\Core\Events\EventContextData;
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\Models\Module;
|
||||
use LotGD\Core\Tests\CoreModelTestCase;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
class ModuleConfigResetCommandTest extends CoreModelTestCase
|
||||
{
|
||||
/** @var string default data set */
|
||||
protected $dataset = "module-2";
|
||||
|
||||
protected function getCommand(): CommandTester
|
||||
{
|
||||
return new CommandTester(new ModuleConfigSetCommand($this->g));
|
||||
}
|
||||
|
||||
public function testIfCommandEmitsEvent()
|
||||
{
|
||||
/** @var Game $game */
|
||||
$game = $this->g;
|
||||
|
||||
$modules = [
|
||||
["lotgd/tests", "test"],
|
||||
["lotgd/tests-other", "test-other"],
|
||||
];
|
||||
|
||||
foreach ($modules as [$module, $setting]) {
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('publish'))
|
||||
->getMock();
|
||||
$eventManager->expects($this->once())
|
||||
->method('publish')
|
||||
->with(
|
||||
$this->equalTo("h/lotgd/core/cli/module-config-reset/{$module}"),
|
||||
$this->callback(function (EventContextData $eventContextData) use ($module, $setting) {
|
||||
$pass = 1;
|
||||
|
||||
$pass &= $eventContextData->has("module") === true;
|
||||
$pass &= $eventContextData->get("module")->getLibrary() === $module;
|
||||
|
||||
$pass &= $eventContextData->has("io") === true;
|
||||
$pass &= $eventContextData->get("io") instanceof SymfonyStyle;
|
||||
|
||||
$pass &= $eventContextData->has("setting") === true;
|
||||
$pass &= $eventContextData->get("setting") === $setting;
|
||||
|
||||
$pass &= $eventContextData->has("return") === true;
|
||||
$pass &= $eventContextData->get("return") === 1;
|
||||
|
||||
$pass &= $eventContextData->has("reason") === true;
|
||||
$pass &= $eventContextData->get("reason") === "Setting does not exist.";
|
||||
|
||||
return $pass == true;
|
||||
}),
|
||||
)->will($this->returnArgument(1));
|
||||
$game->setEventManager($eventManager);
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => $module,
|
||||
"setting" => $setting,
|
||||
"value" => $value,
|
||||
]);
|
||||
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::FAILURE, $command->getStatusCode());
|
||||
$this->assertStringContainsString("Module {$module}", $output);
|
||||
$this->assertStringContainsString("[ERROR]", $output);
|
||||
$this->assertStringContainsString("Setting does not exist.", $output);
|
||||
}
|
||||
}
|
||||
|
||||
public function testIfCommandSucceedsWhenReturnedCallbackIsSetToSuccess()
|
||||
{
|
||||
/** @var Game $game */
|
||||
$game = $this->g;
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('publish'))
|
||||
->getMock();
|
||||
$eventManager->expects($this->once())
|
||||
->method('publish')
|
||||
->will($this->returnCallback(function (string $a, EventContextData $b) {
|
||||
return $b->set("return", Command::SUCCESS);
|
||||
}));
|
||||
$game->setEventManager($eventManager);
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => "lotgd/tests",
|
||||
"setting" => "Setting",
|
||||
]);
|
||||
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
|
||||
$this->assertStringContainsString("Module lotgd/tests", $output);
|
||||
$this->assertStringNotContainsString("[ERROR]", $output);
|
||||
$this->assertStringNotContainsString("Setting does not exist.", $output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Tests\Commands\CharacterCommands;
|
||||
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigListCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleConfigSetCommand;
|
||||
use LotGD\Core\Console\Command\Module\ModuleListCommand;
|
||||
use LotGD\Core\EventHandler;
|
||||
use LotGD\Core\EventManager;
|
||||
use LotGD\Core\Events\EventContext;
|
||||
use LotGD\Core\Events\EventContextData;
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\Models\Module;
|
||||
use LotGD\Core\Tests\CoreModelTestCase;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
class ModuleConfigSetCommandTest extends CoreModelTestCase
|
||||
{
|
||||
/** @var string default data set */
|
||||
protected $dataset = "module-2";
|
||||
|
||||
protected function getCommand(): CommandTester
|
||||
{
|
||||
return new CommandTester(new ModuleConfigSetCommand($this->g));
|
||||
}
|
||||
|
||||
public function testIfCommandEmitsEvent()
|
||||
{
|
||||
/** @var Game $game */
|
||||
$game = $this->g;
|
||||
|
||||
$modules = [
|
||||
["lotgd/tests", "test", "0.126"],
|
||||
["lotgd/tests-other", "test-other", "hi"],
|
||||
];
|
||||
|
||||
foreach ($modules as [$module, $setting, $value]) {
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('publish'))
|
||||
->getMock();
|
||||
$eventManager->expects($this->once())
|
||||
->method('publish')
|
||||
->with(
|
||||
$this->equalTo("h/lotgd/core/cli/module-config-set/{$module}"),
|
||||
$this->callback(function (EventContextData $eventContextData) use ($module, $setting, $value) {
|
||||
$pass = 1;
|
||||
|
||||
$pass &= $eventContextData->has("module") === true;
|
||||
$pass &= $eventContextData->get("module")->getLibrary() === $module;
|
||||
|
||||
$pass &= $eventContextData->has("io") === true;
|
||||
$pass &= $eventContextData->get("io") instanceof SymfonyStyle;
|
||||
|
||||
$pass &= $eventContextData->has("setting") === true;
|
||||
$pass &= $eventContextData->get("setting") === $setting;
|
||||
|
||||
$pass &= $eventContextData->has("value") === true;
|
||||
$pass &= $eventContextData->get("value") === $value;
|
||||
|
||||
$pass &= $eventContextData->has("return") === true;
|
||||
$pass &= $eventContextData->get("return") === 1;
|
||||
|
||||
$pass &= $eventContextData->has("reason") === true;
|
||||
$pass &= $eventContextData->get("reason") === "Setting does not exist.";
|
||||
|
||||
return $pass == true;
|
||||
}),
|
||||
)->will($this->returnArgument(1));
|
||||
$game->setEventManager($eventManager);
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => $module,
|
||||
"setting" => $setting,
|
||||
"value" => $value,
|
||||
]);
|
||||
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::FAILURE, $command->getStatusCode());
|
||||
$this->assertStringContainsString("Module {$module}", $output);
|
||||
$this->assertStringContainsString("[ERROR]", $output);
|
||||
$this->assertStringContainsString("Setting does not exist.", $output);
|
||||
}
|
||||
}
|
||||
|
||||
public function testIfCommandSucceedsWhenReturnedCallbackIsSetToSuccess()
|
||||
{
|
||||
/** @var Game $game */
|
||||
$game = $this->g;
|
||||
$eventManager = $this->getMockBuilder(EventManager::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('publish'))
|
||||
->getMock();
|
||||
$eventManager->expects($this->once())
|
||||
->method('publish')
|
||||
->will($this->returnCallback(function (string $a, EventContextData $b) {
|
||||
return $b->set("return", Command::SUCCESS);
|
||||
}));
|
||||
$game->setEventManager($eventManager);
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->execute([
|
||||
"moduleName" => "lotgd/tests",
|
||||
"setting" => "Setting",
|
||||
"value" => 13,
|
||||
]);
|
||||
|
||||
$output = $command->getDisplay();
|
||||
|
||||
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
|
||||
$this->assertStringContainsString("Module lotgd/tests", $output);
|
||||
$this->assertStringNotContainsString("[ERROR]", $output);
|
||||
$this->assertStringNotContainsString("Setting does not exist.", $output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Tests\Commands\CharacterCommands;
|
||||
|
||||
use LotGD\Core\Console\Command\Module\ModuleListCommand;
|
||||
use LotGD\Core\Models\Module;
|
||||
use LotGD\Core\Tests\CoreModelTestCase;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
class ModuleListCommandTest extends CoreModelTestCase
|
||||
{
|
||||
/** @var string default data set */
|
||||
protected $dataset = "module-2";
|
||||
|
||||
protected function getCommand(): CommandTester
|
||||
{
|
||||
return new CommandTester(new ModuleListCommand($this->g));
|
||||
}
|
||||
|
||||
public function testIfCommandRunsWithModulesInstalled()
|
||||
{
|
||||
$command = $this->getCommand();
|
||||
$command->execute([]);
|
||||
$output = $command->getDisplay();
|
||||
|
||||
// Assertions
|
||||
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
|
||||
$this->assertStringContainsString("lotgd/tests", $output);
|
||||
$this->assertStringContainsString("lotgd/tests-other", $output);
|
||||
$this->assertStringNotContainsString("No modules installed.", $output);
|
||||
}
|
||||
|
||||
public function testIfCommandRunsWithNoModulesInstalled()
|
||||
{
|
||||
// Remove modules first
|
||||
$modules = $this->g->getEntityManager()->getRepository(Module::class)->findAll();
|
||||
foreach ($modules as $module) {
|
||||
$this->g->getEntityManager()->remove($module);
|
||||
$this->g->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
// Run command
|
||||
$command = $this->getCommand();
|
||||
$command->execute([]);
|
||||
$output = $command->getDisplay();
|
||||
|
||||
// Assert
|
||||
$this->assertStringContainsString("No modules installed.", $output);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class CharacterModelTest extends CoreModelTestCase
|
||||
$this->assertSame(null, $chars);
|
||||
|
||||
$allChars = $this->getEntityManager()->getRepository(Character::class)->findAll();
|
||||
$this->assertSame(2, count($allChars));
|
||||
$this->assertSame(3, count($allChars));
|
||||
|
||||
$char = $this->getEntityManager()->getRepository(Character::class)->find("10000000-0000-0000-0000-000000000001");
|
||||
$char->delete($this->getEntityManager());
|
||||
@@ -43,12 +43,12 @@ class CharacterModelTest extends CoreModelTestCase
|
||||
$allChars = $this->getEntityManager()
|
||||
->getRepository(Character::class)
|
||||
->findAll();
|
||||
$this->assertSame(1, count($allChars));
|
||||
$this->assertSame(2, count($allChars));
|
||||
|
||||
$allChars = $this->getEntityManager()
|
||||
->getRepository(Character::class)
|
||||
->findAll(CharacterRepository::INCLUDE_SOFTDELETED);
|
||||
$this->assertSame(3, count($allChars));
|
||||
$this->assertSame(4, count($allChars));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
modules:
|
||||
-
|
||||
library: "lotgd/tests"
|
||||
createdAt: "2016-05-01"
|
||||
-
|
||||
library: "lotgd/tests-other"
|
||||
createdAt: "2020-02-07"
|
||||
module_properties:
|
||||
-
|
||||
owner: 'lotgd/tests'
|
||||
propertyName: "test"
|
||||
propertyValue: 's:5:"hallo";'
|
||||
Reference in New Issue
Block a user