-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from yohang/feature/dumpers
feat: Add state machine dumpers
- Loading branch information
Showing
10 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Finite\Dumper; | ||
|
||
use Finite\State; | ||
|
||
interface Dumper | ||
{ | ||
/** | ||
* @param enum-string<State> $stateEnum | ||
*/ | ||
public function dump(string $stateEnum): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Finite\Dumper; | ||
|
||
use Finite\State; | ||
|
||
class MermaidDumper implements Dumper | ||
{ | ||
/** | ||
* @param enum-string<State> $stateEnum | ||
*/ | ||
public function dump(string $stateEnum): string | ||
{ | ||
$output = [ | ||
'---', | ||
'title: ' . $stateEnum, | ||
'---', | ||
'stateDiagram-v2', | ||
]; | ||
|
||
foreach ($stateEnum::getTransitions() as $transition) { | ||
foreach ($transition->getSourceStates() as $state) { | ||
$output[] = sprintf( | ||
' %s --> %s: %s', | ||
$state->value, | ||
$transition->getTargetState()->value, | ||
$transition->getName() | ||
); | ||
} | ||
} | ||
|
||
return implode(PHP_EOL, $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Finite\Extension\Symfony\Command; | ||
|
||
use Finite\Dumper\MermaidDumper; | ||
use Finite\State; | ||
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; | ||
|
||
final class DumpStateMachineCommand extends Command | ||
{ | ||
private const FORMAT_MERMAID = 'mermaid'; | ||
private const FORMATS = [self::FORMAT_MERMAID]; | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('finite:state-machine:dump') | ||
->setDescription('Dump the state machine graph into requested format') | ||
->addArgument('state_enum', InputArgument::REQUIRED, 'The state enum to use') | ||
->addArgument('format', InputArgument::REQUIRED, 'The format to dump the graph in'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$stateEnum = (string)$input->getArgument('state_enum'); | ||
|
||
if (!(enum_exists($stateEnum) && is_subclass_of($stateEnum, State::class))) { | ||
$io->error('The state enum "' . $stateEnum . '" does not exist.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$format = (string)$input->getArgument('format'); | ||
switch ($format) { | ||
case self::FORMAT_MERMAID: | ||
/** @psalm-suppress ArgumentTypeCoercion Type is enforced upper but not detected by psalm */ | ||
$output->writeln((new MermaidDumper)->dump($stateEnum)); | ||
|
||
break; | ||
default: | ||
$output->writeln('Unknown format "' . $format . '". Supported formats are: ' . implode(', ', self::FORMATS)); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Finite\Tests\Dumper; | ||
|
||
use Finite\Dumper\MermaidDumper; | ||
use Finite\Tests\E2E\SimpleArticleState; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MermaidDumperTest extends TestCase | ||
{ | ||
public function test_it_dumps(): void | ||
{ | ||
$this->assertSame( | ||
<<<MERMAID | ||
--- | ||
title: Finite\Tests\E2E\SimpleArticleState | ||
--- | ||
stateDiagram-v2 | ||
draft --> published: publish | ||
reported --> published: clear | ||
disabled --> published: clear | ||
published --> reported: report | ||
reported --> disabled: disable | ||
published --> disabled: disable | ||
MERMAID, | ||
(new MermaidDumper)->dump(SimpleArticleState::class) | ||
); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/Extension/Symfony/Command/DumpStateMachineCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Finite\Tests\Extension\Symfony\Command; | ||
|
||
use Finite\Tests\Extension\Symfony\Fixtures\State\DocumentState; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class DumpStateMachineCommandTest extends KernelTestCase | ||
{ | ||
private ?CommandTester $commandTester = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$kernel = self::bootKernel(); | ||
$application = new Application($kernel); | ||
|
||
$command = $application->find('finite:state-machine:dump'); | ||
$this->commandTester = new CommandTester($command); | ||
} | ||
|
||
public function test_it_returns_mermaid_dump(): void | ||
{ | ||
$this->commandTester->execute([ | ||
'state_enum' => DocumentState::class, | ||
'format' => 'mermaid', | ||
]); | ||
|
||
$this->commandTester->assertCommandIsSuccessful(); | ||
} | ||
|
||
public function test_it_fails_with_unknown_state_enum(): void | ||
{ | ||
$this->commandTester->execute([ | ||
'state_enum' => 'UnknownStateEnum', | ||
'format' => 'mermaid', | ||
]); | ||
|
||
$this->assertSame(1, $this->commandTester->getStatusCode()); | ||
} | ||
|
||
public function test_it_fails_with_unknown_format(): void | ||
{ | ||
$this->commandTester->execute([ | ||
'state_enum' => DocumentState::class, | ||
'format' => 'blobfish', | ||
]); | ||
|
||
$this->assertSame(1, $this->commandTester->getStatusCode()); | ||
} | ||
|
||
protected static function getKernelClass(): string | ||
{ | ||
return \AppKernel::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters