diff --git a/src/Implementation/Events/StateChanged.php b/src/Implementation/Events/StateChanged.php index 54cafab..f12003a 100644 --- a/src/Implementation/Events/StateChanged.php +++ b/src/Implementation/Events/StateChanged.php @@ -28,4 +28,17 @@ public function getOldState(): StateInterface { return $this->oldState; } + + public function __toString(): string + { + $item = $this->getItem(); + + return sprintf( + '%s[%s, %s->%s]', + __CLASS__, + method_exists($item, '__toString') ? $item : get_class($item), + $this->getOldState(), + $item->getState() + ); + } } diff --git a/src/Implementation/Events/StateChanging.php b/src/Implementation/Events/StateChanging.php index 9ff372c..ee0f916 100644 --- a/src/Implementation/Events/StateChanging.php +++ b/src/Implementation/Events/StateChanging.php @@ -28,4 +28,17 @@ public function getNewState(): StateInterface { return $this->newState; } + + public function __toString(): string + { + $item = $this->getItem(); + + return sprintf( + '%s[%s, %s->%s]', + __CLASS__, + method_exists($item, '__toString') ? $item : get_class($item), + $item->getState(), + $this->getNewState() + ); + } } diff --git a/src/Implementation/Traits/Plantable.php b/src/Implementation/Traits/Plantable.php index 50e4b47..d3d8299 100644 --- a/src/Implementation/Traits/Plantable.php +++ b/src/Implementation/Traits/Plantable.php @@ -14,26 +14,25 @@ trait Plantable { public function toPlantUML(): string { - /** @var $this TransitionRepositoryInterface */ - return implode(PHP_EOL, array_merge( - ['@startuml', ''], - array_map( - static function (TransitionInterface $transition): string { - $oldState = $transition->getOldState(); - $newState = $transition->getNewState(); - $oldText = $oldState instanceof DescribableInterface ? $oldState->getDescription() : $oldState->getName(); - $newText = $newState instanceof DescribableInterface ? $newState->getDescription() : $newState->getName(); + $generateNodesAndEdges = static function (TransitionInterface $transition): string { + $oldText = ($oldState = $transition->getOldState()) instanceof DescribableInterface + ? $oldState->getDescription() : $oldState->getName(); + $newText = ($newState = $transition->getNewState()) instanceof DescribableInterface + ? $newState->getDescription() : $newState->getName(); + + $result = "($oldText) --> ($newText)"; - $result = "($oldText) --> ($newText)"; + if ($transition instanceof DescribableInterface) { + $result .= " : {$transition->getDescription()}"; + } - if ($transition instanceof DescribableInterface) { - $result .= " : {$transition->getDescription()}"; - } + return $result; + }; - return $result; - }, - iterator_to_array($this) - ), + /** @var $this TransitionRepositoryInterface */ + return implode(PHP_EOL, array_merge( + ['@startuml', ''], + array_map($generateNodesAndEdges, iterator_to_array($this)), ['', '@enduml'] )); } diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 055c461..836c1e7 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -50,15 +50,7 @@ public function test_that_new_state_must_be_declared_before_transitions(): void public function test_that_transitioning_with_mutator_works(): void { - $this->expectNotToPerformAssertions(); - - $engine = Builder::create() - ->defState('a') - ->defState('b') - ->defTransition('a', 'b') - ->getEngine(); - - $item = new StatefulItem(new State('a')); + $item = new StatefulItem(new State('started')); $mutator = Builder::makeStateMutator( static function () use ($item): State { return $item->getState(); @@ -68,6 +60,8 @@ static function (State $newState) use ($item): void { } ); - $engine->changeState($mutator, new State('b')); + $this->builder->getEngine()->changeState($mutator, new State('finished')); + + $this->assertSame('finished', $item->getState()->getName()); } } diff --git a/tests/EventTest.php b/tests/EventTest.php index 0c81511..588c900 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -6,11 +6,7 @@ use Psr\EventDispatcher\EventDispatcherInterface; use uuf6429\StateEngine\Implementation\Builder; use uuf6429\StateEngine\Implementation\Entities\State; -use uuf6429\StateEngine\Implementation\Events\StateChanged; -use uuf6429\StateEngine\Implementation\Events\StateChanging; use uuf6429\StateEngine\Interfaces\EngineInterface; -use uuf6429\StateEngine\Interfaces\StateAwareInterface; -use uuf6429\StateEngine\Interfaces\StateInterface; class EventTest extends TestCase { @@ -32,7 +28,7 @@ protected function setUp(): void ->getEngine($this->dispatcher); } - public function test_that_engine_fires_exception(): void + public function test_that_engine_triggers_events(): void { $dispatchedEvents = []; $this->dispatcher @@ -43,30 +39,16 @@ public function test_that_engine_fires_exception(): void $startedState = new State('started'); $finishedState = new State('finished'); - $statefulItem = new class implements StateAwareInterface { - public StateInterface $state; - - public function getState(): StateInterface - { - return $this->state; - } - - public function setState(StateInterface $newState): void - { - $this->state = $newState; - } - }; - - $statefulItem->state = $startedState; + $statefulItem = new StatefulItem($startedState); $this->engine->changeState($statefulItem, $finishedState); - $this->assertEquals($finishedState, $statefulItem->state); + $this->assertEquals($finishedState, $statefulItem->getState()); $this->assertEquals( [ - new StateChanging($statefulItem, $finishedState), - new StateChanged($statefulItem, $startedState), + 'uuf6429\StateEngine\Implementation\Events\StateChanging[StatefulItem, finished->finished]', + 'uuf6429\StateEngine\Implementation\Events\StateChanged[StatefulItem, started->finished]', ], - $dispatchedEvents + array_map('strval', $dispatchedEvents) ); } } diff --git a/tests/JiraIssueTest.php b/tests/JiraIssueTest.php index 78d65e8..a9d8a4b 100644 --- a/tests/JiraIssueTest.php +++ b/tests/JiraIssueTest.php @@ -81,10 +81,11 @@ static function (TransitionInterface $transition) { public function test_that_transitioning_from_in_dev_to_ready_for_qa_is_allowed(): void { - $this->expectNotToPerformAssertions(); - $item = new StatefulItem(new State('in-dev')); + $this->engine->changeState($item, new State('ready-for-qa')); + + $this->assertSame('ready-for-qa', $item->getState()->getName()); } public function test_that_transitioning_from_in_dev_to_in_qa_is_not_allowed(): void @@ -92,6 +93,7 @@ public function test_that_transitioning_from_in_dev_to_in_qa_is_not_allowed(): v $this->expectExceptionMessage('Cannot apply transition "in-dev -> in-qa"; no such transition was defined.'); $item = new StatefulItem(new State('in-dev')); + $this->engine->changeState($item, new State('in-qa')); } diff --git a/tests/StatefulItem.php b/tests/StatefulItem.php index 6944aa1..38c45de 100644 --- a/tests/StatefulItem.php +++ b/tests/StatefulItem.php @@ -23,4 +23,9 @@ public function setState(StateInterface $newState): void { $this->state = $newState; } + + public function __toString(): string + { + return 'StatefulItem'; + } }