Adds new commands to manage modules and corresponding tests.

This commit is contained in:
Vassyli
2021-02-07 18:34:40 +01:00
committed by Basilius Sauter
parent eea72f848b
commit 0292a66252
14 changed files with 727 additions and 9 deletions
@@ -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);
}
}