-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace HasStateManager::generateMermaidGraphAsString with MermaidSta…
…teConfigValidator
- Loading branch information
Showing
4 changed files
with
81 additions
and
50 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
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,66 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\LaravelUtils\ModelStates; | ||
|
||
use JBZoo\MermaidPHP\Graph; | ||
use JBZoo\MermaidPHP\Link; | ||
use JBZoo\MermaidPHP\Node; | ||
use PHPUnit\Framework\Assert; | ||
|
||
/** Requires the package jbzoo/mermaid-php. */ | ||
class MermaidStateConfigValidator | ||
{ | ||
/** Performs a PHPUnit assertion that a generated Mermaid graph equals an expected result. */ | ||
public static function assertGraphEquals(string $expectedGraphString, Graph $actualGraph): void | ||
{ | ||
Assert::assertSame( | ||
expected: $expectedGraphString, | ||
actual: $actualGraph->__toString(), | ||
message: "The Mermaid graph did not match. See the actual generated graph at {$actualGraph->getLiveEditorUrl()}." | ||
); | ||
} | ||
|
||
public static function assertStateConfigEquals(string $expectedGraphString, StateConfig $actualStateConfig): void | ||
{ | ||
self::assertGraphEquals( | ||
expectedGraphString: $expectedGraphString, | ||
actualGraph: self::graphFromStateConfig($actualStateConfig), | ||
); | ||
} | ||
|
||
public static function graphFromStateConfig(StateConfig $stateConfig): Graph | ||
{ | ||
$graph = (new Graph(['abc_order' => true])) | ||
->addStyle('linkStyle default interpolate basis'); | ||
|
||
$possibleStateInstances = array_map( | ||
static fn (string $stateClass): State => new $stateClass(), | ||
$stateConfig->possibleStates(), | ||
); | ||
|
||
foreach ($possibleStateInstances as $state) { | ||
$graph->addNode(new Node($state::name())); | ||
} | ||
|
||
foreach ($possibleStateInstances as $state) { | ||
foreach ($stateConfig->possibleNextStates($state) as $possibleNextState) { | ||
$sourceNode = $graph->getNode($state::name()); | ||
$targetNode = $graph->getNode($possibleNextState::name()); | ||
|
||
if ($sourceNode instanceof Node && $targetNode instanceof Node) { | ||
$transition = $stateConfig->transition($state::class, $possibleNextState::class); | ||
assert($transition !== null); | ||
|
||
$reflectionClass = new \ReflectionClass($transition); | ||
$transitionName = $reflectionClass->getName() === DefaultTransition::class | ||
? '' | ||
: $reflectionClass->getShortName(); | ||
|
||
$graph->addLink(new Link($sourceNode, $targetNode, $transitionName)); | ||
} | ||
} | ||
} | ||
|
||
return $graph; | ||
} | ||
} |
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