Added the possibilities to set character and scene settings over cli, with module hooks.

This commit is contained in:
Vassyli
2021-02-16 19:18:41 +01:00
committed by Basilius Sauter
parent f72adb1a38
commit 669085e65d
23 changed files with 1171 additions and 24 deletions
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Character;
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 CharacterConfigListCommand extends CharacterBaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName($this->namespaced("config:list"))
->setDescription('List available settings for a character')
->setDefinition([
$this->getCharacterIdArgumentDefinition(),
])
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$character = $this->getCharacter($input->getArgument("id"));
if (!$character) {
$io->error("Character was not found.");
return Command::FAILURE;
}
// Create hook
$context = EventContextData::create([
"character" => $character,
"io" => $io,
"settings" => [],
]);
$newContext = $this->game->getEventManager()->publish(
event: "h/lotgd/core/cli/character-config-list",
contextData: $context
);
$settings = $newContext->get("settings");
$io->title("Character ".$character->getDisplayName());
if (count($settings) === 0) {
$io->note("There are no character settings available.");
} else {
$io->table(["setting", "value", "description"], $settings);
}
return Command::SUCCESS;
}
}
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Character;
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 CharacterConfigResetCommand extends CharacterBaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName($this->namespaced("config:reset"))
->setDescription('Reset a character setting')
->setDefinition([
$this->getCharacterIdArgumentDefinition(),
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);
$character = $this->getCharacter($input->getArgument("id"));
if (!$character) {
$io->error("Module was not found.");
return Command::FAILURE;
}
$io->title("Character {$character->getDisplayName()}");
// Create hook
$context = EventContextData::create([
"character" => $character,
"io" => $io,
"setting" => $input->getArgument("setting"),
"return" => Command::FAILURE,
"reason" => "Setting does not exist.",
]);
$newContext = $this->game->getEventManager()->publish(
event: "h/lotgd/core/cli/character-config-reset",
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\Character;
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 CharacterConfigSetCommand extends CharacterBaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName($this->namespaced("config:set"))
->setDescription('Change a character setting')
->setDefinition([
$this->getCharacterIdArgumentDefinition(),
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);
$character = $this->getCharacter($input->getArgument("id"));
if (!$character) {
$io->error("Module was not found.");
return Command::FAILURE;
}
$io->title("Character {$character->getDisplayName()}");
// Create hook
$context = EventContextData::create([
"character" => $character,
"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/character-config-set",
contextData: $context
);
if ($newContext->get("return") != Command::SUCCESS) {
$io->error($newContext->get("reason"));
return Command::FAILURE;
}
$this->game->getEntityManager()->flush();
return Command::SUCCESS;
}
}
@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace LotGD\Core\Console\Command\Scene;
use LotGD\Core\Console\Command\BaseCommand;
use LotGD\Core\Models\Scene;
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
use Symfony\Component\Console\Input\InputArgument;
class SceneBaseCommand extends BaseCommand
@@ -21,4 +23,31 @@ class SceneBaseCommand extends BaseCommand
description: "Scene ID",
);
}
/**
* @param string $id
* @return Scene|null
*/
protected function getScene(string $id): ?Scene
{
/** @var Scene|null $scene */
$scene = $this->game->getEntityManager()->getRepository(Scene::class)->find($id);
return $scene;
}
/**
* @param Scene $scene
* @return string
*/
protected function getSceneTemplatePath(Scene $scene)
{
$sceneTemplate = "no-template";
if ($scene->getTemplate()) {
/** @var SceneTemplateInterface $templateClass */
$templateClass = $scene->getTemplate()->getClass();
$sceneTemplate = $templateClass::getNavigationEvent();
}
return $sceneTemplate;
}
}
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Scene;
use LotGD\Core\Events\EventContextData;
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
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 SceneConfigListCommand extends SceneBaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName($this->namespaced("config:list"))
->setDescription('List available settings for a scene')
->setDefinition([
$this->getSceneIdArgumentDefinition(),
])
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$scene = $this->getScene($input->getArgument("id"));
if (!$scene) {
$io->error("Scene was not found.");
return Command::FAILURE;
}
$sceneTemplate = $this->getSceneTemplatePath($scene);
// Create hook
$context = EventContextData::create([
"scene" => $scene,
"io" => $io,
"settings" => [],
]);
$newContext = $this->game->getEventManager()->publish(
event: "h/lotgd/core/cli/scene-config-list/$sceneTemplate",
contextData: $context
);
$settings = $newContext->get("settings");
$io->title("Scene ".$scene->getTitle());
if (count($settings) === 0) {
$io->note("There are no scene settings available.");
} else {
$io->table(["setting", "value", "description"], $settings);
}
return Command::SUCCESS;
}
}
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Scene;
use LotGD\Core\Events\EventContextData;
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
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 SceneConfigResetCommand extends SceneBaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName($this->namespaced("config:reset"))
->setDescription('Reset a scene setting')
->setDefinition([
$this->getSceneIdArgumentDefinition(),
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);
$scene = $this->getScene($input->getArgument("id"));
if (!$scene) {
$io->error("Scene was not found.");
return Command::FAILURE;
}
$sceneTemplate = $this->getSceneTemplatePath($scene);
$io->title("Scene {$scene->getTitle()}");
// Create hook
$context = EventContextData::create([
"scene" => $scene,
"io" => $io,
"setting" => $input->getArgument("setting"),
"return" => Command::FAILURE,
"reason" => "Setting does not exist.",
]);
$newContext = $this->game->getEventManager()->publish(
event: "h/lotgd/core/cli/character-config-reset/{$sceneTemplate}",
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,75 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Console\Command\Scene;
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 SceneConfigSetCommand extends SceneBaseCommand
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->setName($this->namespaced("config:set"))
->setDescription('Change a scene setting')
->setDefinition([
$this->getSceneIdArgumentDefinition(),
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);
$scene = $this->getScene($input->getArgument("id"));
if (!$scene) {
$io->error("Scene was not found.");
return Command::FAILURE;
}
$sceneTemplate = $this->getSceneTemplatePath($scene);
$io->title("Scene {$scene->getTitle()}");
// Create hook
$context = EventContextData::create([
"scene" => $scene,
"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/character-config-set/{$sceneTemplate}",
contextData: $context
);
if ($newContext->get("return") != Command::SUCCESS) {
$io->error($newContext->get("reason"));
return Command::FAILURE;
}
$this->game->getEntityManager()->flush();
return Command::SUCCESS;
}
}
@@ -55,7 +55,7 @@ class SceneShowCommand extends SceneBaseCommand
$io->listing([
"ID: {$scene->getId()}",
"Title: {$scene->getTitle()}",
"Template: {$scene->getTemplate()->getClass()}",
"Template: {$scene->getTemplate()?->getClass()}",
]);
$io->text($scene->getDescription());
+6 -6
View File
@@ -14,9 +14,9 @@ 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\CharacterConfigListCommand;
use LotGD\Core\Console\Command\Module\CharacterConfigResetCommand;
use LotGD\Core\Console\Command\Module\CharacterConfigSetCommand;
use LotGD\Core\Console\Command\Module\ModuleListCommand;
use LotGD\Core\Console\Command\Module\ModuleRegisterCommand;
use LotGD\Core\Console\Command\Module\ModuleValidateCommand;
@@ -63,9 +63,9 @@ class Main
$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 CharacterConfigListCommand($this->game));
$this->application->add(new CharacterConfigResetCommand($this->game));
$this->application->add(new CharacterConfigSetCommand($this->game));
$this->application->add(new ModuleListCommand($this->game));
$this->application->add(new ModuleRegisterCommand($this->game));
$this->application->add(new ModuleValidateCommand($this->game));
@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
use LotGD\Core\Console\Command\Character\CharacterConfigListCommand;
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 CharacterConfigListCommandTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "character";
protected function getCommand(): CommandTester
{
return new CommandTester(new CharacterConfigListCommand($this->g));
}
public function testIfCommandRunsWithoutRegisteredEvents()
{
$command = $this->getCommand();
$command->execute([
"id" => "10000000-0000-0000-0000-000000000001",
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("There are no character settings available.", $output);
}
public function testIfCommandEmitsEvent()
{
/** @var Game $game */
$game = $this->g;
$characters = [
["10000000-0000-0000-0000-000000000001", "Testcharacter 1"],
["10000000-0000-0000-0000-000000000002", "Testcharacter 2"],
];
foreach ($characters as [$character, $displayName]) {
$eventManager = $this->getMockBuilder(EventManager::class)
->disableOriginalConstructor()
->setMethods(array('publish'))
->getMock();
$eventManager->expects($this->once())
->method('publish')
->with(
$this->equalTo("h/lotgd/core/cli/character-config-list"),
$this->callback(function (EventContextData $eventContextData) use ($character, $displayName) {
$pass = 1;
$pass &= $eventContextData->has("character") === true;
$pass &= $eventContextData->get("character")->getDisplayName() === $displayName;
$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([
"id" => $character,
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("Character {$displayName}", $output);
$this->assertStringContainsString("There are no character settings available.", $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([
"id" => "10000000-0000-0000-0000-000000000001",
]);
$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("There are no character settings available.", $output);
}
}
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
use LotGD\Core\Console\Command\Character\CharacterConfigResetCommand;
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 CharacterConfigResetCommandTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "character";
protected function getCommand(): CommandTester
{
return new CommandTester(new CharacterConfigResetCommand($this->g));
}
public function testIfCommandEmitsEvent()
{
/** @var Game $game */
$game = $this->g;
$modules = [
["10000000-0000-0000-0000-000000000001", "Testcharacter 1", "test"],
["10000000-0000-0000-0000-000000000002", "Testcharacter 2", "test-other"],
];
foreach ($modules as [$module, $displayName, $setting]) {
$eventManager = $this->getMockBuilder(EventManager::class)
->disableOriginalConstructor()
->setMethods(array('publish'))
->getMock();
$eventManager->expects($this->once())
->method('publish')
->with(
$this->equalTo("h/lotgd/core/cli/character-config-reset"),
$this->callback(function (EventContextData $eventContextData) use ($module, $displayName, $setting) {
$pass = 1;
$pass &= $eventContextData->has("character") === true;
$pass &= $eventContextData->get("character")->getDisplayName() === $displayName;
$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([
"id" => $module,
"setting" => $setting,
]);
$output = $command->getDisplay();
$this->assertSame(Command::FAILURE, $command->getStatusCode());
$this->assertStringContainsString("Character {$displayName}", $output);
$this->assertStringContainsString("[ERROR]", $output);
$this->assertStringNotContainsString("[OK]", $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([
"id" => "10000000-0000-0000-0000-000000000001",
"setting" => "Setting",
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("Character Testcharacter 1", $output);
$this->assertStringNotContainsString("[ERROR]", $output);
$this->assertStringNotContainsString("Setting does not exist.", $output);
}
}
@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
use LotGD\Core\Console\Command\Character\CharacterConfigSetCommand;
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 CharacterConfigSetCommandTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "character";
protected function getCommand(): CommandTester
{
return new CommandTester(new CharacterConfigSetCommand($this->g));
}
public function testIfCommandEmitsEvent()
{
/** @var Game $game */
$game = $this->g;
$characters = [
["10000000-0000-0000-0000-000000000001", "Testcharacter 1", "test", "0.126"],
["10000000-0000-0000-0000-000000000002", "Testcharacter 2", "test-other", "hi"],
];
foreach ($characters as [$character, $displayName, $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/character-config-set"),
$this->callback(function (EventContextData $eventContextData) use ($character, $displayName, $setting, $value) {
$pass = 1;
$pass &= $eventContextData->has("character") === true;
$pass &= $eventContextData->get("character")->getDisplayName() === $displayName;
$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([
"id" => $character,
"setting" => $setting,
"value" => $value,
]);
$output = $command->getDisplay();
$this->assertSame(Command::FAILURE, $command->getStatusCode());
$this->assertStringContainsString("Character {$displayName}", $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([
"id" => "10000000-0000-0000-0000-000000000001",
"setting" => "Setting",
"value" => 13,
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("Character Testcharacter 1", $output);
$this->assertStringNotContainsString("[ERROR]", $output);
$this->assertStringNotContainsString("Setting does not exist.", $output);
}
}
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
namespace LotGD\Core\Tests\Commands\ModuleCommands;
use LotGD\Core\Console\Command\Module\ModuleConfigListCommand;
use LotGD\Core\EventManager;
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
namespace LotGD\Core\Tests\Commands\ModuleCommands;
use LotGD\Core\Console\Command\Module\ModuleConfigResetCommand;
use LotGD\Core\EventManager;
@@ -1,17 +1,12 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
namespace LotGD\Core\Tests\Commands\ModuleCommands;
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;
@@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
use LotGD\Core\Console\Command\Scene\SceneConfigListCommand;
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 SceneConfigListCommandTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "scene-2";
protected function getCommand(): CommandTester
{
return new CommandTester(new SceneConfigListCommand($this->g));
}
public function testIfCommandRunsWithoutRegisteredEvents()
{
$command = $this->getCommand();
$command->execute([
"id" => "30000000-0000-0000-0000-000000000001",
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("There are no scene settings available.", $output);
}
public function testIfCommandEmitsEvent()
{
/** @var Game $game */
$game = $this->g;
$scenes = [
["30000000-0000-0000-0000-000000000001", "The Village", "tests/village"],
["30000000-0000-0000-0000-000000000002", "The Forest", "tests/forest"],
];
foreach ($scenes as [$scene, $sceneTitle, $path]) {
$eventManager = $this->getMockBuilder(EventManager::class)
->disableOriginalConstructor()
->setMethods(['publish'])
->getMock();
$eventManager->expects($this->once())
->method('publish')
->with(
$this->equalTo("h/lotgd/core/cli/scene-config-list/{$path}"),
$this->callback(function (EventContextData $eventContextData) use ($scene, $sceneTitle) {
$pass = 1;
$pass &= $eventContextData->has("scene") === true;
$pass &= $eventContextData->get("scene")->getTitle() === $sceneTitle;
$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([
"id" => $scene,
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("Scene {$sceneTitle}", $output);
$this->assertStringContainsString("There are no scene settings available.", $output);
}
}
public function testIfCommandDisplaysSettingsWhenGivenByEvent()
{
/** @var Game $game */
$game = $this->g;
$eventManager = $this->getMockBuilder(EventManager::class)
->disableOriginalConstructor()
->setMethods(['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([
"id" => "30000000-0000-0000-0000-000000000001",
]);
$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("There are no scene settings available.", $output);
}
}
@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\SceneCommands;
use LotGD\Core\Console\Command\Character\CharacterConfigResetCommand;
use LotGD\Core\Console\Command\Scene\SceneConfigResetCommand;
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 SceneConfigResetCommandTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "scene-2";
protected function getCommand(): CommandTester
{
return new CommandTester(new SceneConfigResetCommand($this->g));
}
public function testIfCommandEmitsEvent()
{
/** @var Game $game */
$game = $this->g;
$scenes = [
["30000000-0000-0000-0000-000000000001", "The Village", "tests/village", "test"],
["30000000-0000-0000-0000-000000000002", "The Forest", "tests/forest", "test-other"],
];
foreach ($scenes as [$scene, $sceneTitle, $path, $setting]) {
$eventManager = $this->getMockBuilder(EventManager::class)
->disableOriginalConstructor()
->setMethods(array('publish'))
->getMock();
$eventManager->expects($this->once())
->method('publish')
->with(
$this->equalTo("h/lotgd/core/cli/character-config-reset/{$path}"),
$this->callback(function (EventContextData $eventContextData) use ($scene, $sceneTitle, $setting) {
$pass = 1;
$pass &= $eventContextData->has("scene") === true;
$pass &= $eventContextData->get("scene")->getTitle() === $sceneTitle;
$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([
"id" => $scene,
"setting" => $setting,
]);
$output = $command->getDisplay();
$this->assertSame(Command::FAILURE, $command->getStatusCode());
$this->assertStringContainsString("Scene {$sceneTitle}", $output);
$this->assertStringContainsString("[ERROR]", $output);
$this->assertStringNotContainsString("[OK]", $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([
"id" => "30000000-0000-0000-0000-000000000001",
"setting" => "Setting",
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("Scene The Village", $output);
$this->assertStringNotContainsString("[ERROR]", $output);
$this->assertStringNotContainsString("Setting does not exist.", $output);
}
}
@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\Commands\CharacterCommands;
use LotGD\Core\Console\Command\Character\CharacterConfigSetCommand;
use LotGD\Core\Console\Command\Scene\SceneConfigSetCommand;
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 SceneConfigSetCommandTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "scene-2";
protected function getCommand(): CommandTester
{
return new CommandTester(new SceneConfigSetCommand($this->g));
}
public function testIfCommandEmitsEvent()
{
/** @var Game $game */
$game = $this->g;
$characters = [
["30000000-0000-0000-0000-000000000001", "The Village", "tests/village", "test", "0.126"],
["30000000-0000-0000-0000-000000000002", "The Forest", "tests/forest", "test-other", "hi"],
];
foreach ($characters as [$character, $displayName, $path, $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/character-config-set/{$path}"),
$this->callback(function (EventContextData $eventContextData) use ($character, $displayName, $setting, $value) {
$pass = 1;
$pass &= $eventContextData->has("scene") === true;
$pass &= $eventContextData->get("scene")->getTitle() === $displayName;
$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([
"id" => $character,
"setting" => $setting,
"value" => $value,
]);
$output = $command->getDisplay();
$this->assertSame(Command::FAILURE, $command->getStatusCode());
$this->assertStringContainsString("Scene {$displayName}", $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([
"id" => "30000000-0000-0000-0000-000000000001",
"setting" => "Setting",
"value" => 13,
]);
$output = $command->getDisplay();
$this->assertSame(Command::SUCCESS, $command->getStatusCode());
$this->assertStringContainsString("Scene The Village", $output);
$this->assertStringNotContainsString("[ERROR]", $output);
$this->assertStringNotContainsString("Setting does not exist.", $output);
}
}
@@ -39,7 +39,7 @@ class SceneShowCommandTest extends CoreModelTestCase
$this->assertStringContainsString("Connection Test Scene", $output);
$this->assertStringContainsString("30000000-0000-0000-0000-000000000006", $output);
$this->assertStringContainsString("LotGD\\Core\\Tests\\SceneTemplates\\Village", $output);
$this->assertStringContainsString("LotGD\\Core\\Tests\\SceneTemplates\\VillageSceneTemplate", $output);
$this->assertStringContainsString("This is a connection test scene", $output);
$this->assertStringContainsString("Group One (id=lotgd/tests/testscene/one)", $output);
+1
View File
@@ -20,6 +20,7 @@ use LotGD\Core\{Action,
Game,
GameBuilder,
Tests\SceneTemplates\ParameterTestSceneTemplate,
Tests\SceneTemplates\ForestSceneTemplate,
Tests\SceneTemplates\VillageSceneTemplate,
TimeKeeper,
ModuleManager};
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\SceneTemplates;
use LotGD\Core\SceneTemplates\BasicSceneTemplate;
class ForestSceneTemplate extends BasicSceneTemplate
{
public static function getNavigationEvent(): string
{
return "tests/forest";
}
}
@@ -1,10 +1,8 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\SceneTemplates;
use LotGD\Core\SceneTemplates\BasicSceneTemplate;
class VillageSceneTemplate extends BasicSceneTemplate
@@ -13,4 +11,4 @@ class VillageSceneTemplate extends BasicSceneTemplate
{
return "tests/village";
}
}
}
+5 -5
View File
@@ -3,13 +3,13 @@ scenes:
id: "30000000-0000-0000-0000-000000000001"
title: "The Village"
description: "This is the village."
template: "LotGD\\Core\\Tests\\SceneTemplates\\Village"
template: "LotGD\\Core\\Tests\\SceneTemplates\\VillageSceneTemplate"
removeable: true
-
id: "30000000-0000-0000-0000-000000000002"
title: "The Forest"
description: "This is a very dangerous and dark forest"
template: "LotGD\\Core\\Tests\\SceneTemplates\\Forest"
template: "LotGD\\Core\\Tests\\SceneTemplates\\ForestSceneTemplate"
removeable: true
-
id: "30000000-0000-0000-0000-000000000003"
@@ -33,7 +33,7 @@ scenes:
id: "30000000-0000-0000-0000-000000000006"
title: "Connection Test Scene"
description: "This is a connection test scene"
template: "LotGD\\Core\\Tests\\SceneTemplates\\Village"
template: "LotGD\\Core\\Tests\\SceneTemplates\\VillageSceneTemplate"
removeable: true
scene_connection_groups:
-
@@ -95,11 +95,11 @@ scene_connections:
directionality: 1
scene_templates:
-
class: "LotGD\\Core\\Tests\\SceneTemplates\\Village"
class: "LotGD\\Core\\Tests\\SceneTemplates\\VillageSceneTemplate"
module: "lotgd/core"
userAssignable: true
-
class: "LotGD\\Core\\Tests\\SceneTemplates\\Forest"
class: "LotGD\\Core\\Tests\\SceneTemplates\\ForestSceneTemplate"
module: "lotgd/core"
userAssignable: true
-