Skip to content

Commit

Permalink
Improve coverage and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jun 16, 2022
1 parent 8f81a22 commit 30297b3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 53 deletions.
13 changes: 13 additions & 0 deletions src/Implementation/Events/StateChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
13 changes: 13 additions & 0 deletions src/Implementation/Events/StateChanging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
33 changes: 16 additions & 17 deletions src/Implementation/Traits/Plantable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
));
}
Expand Down
14 changes: 4 additions & 10 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
}
}
30 changes: 6 additions & 24 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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)
);
}
}
6 changes: 4 additions & 2 deletions tests/JiraIssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ 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
{
$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'));
}

Expand Down
5 changes: 5 additions & 0 deletions tests/StatefulItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public function setState(StateInterface $newState): void
{
$this->state = $newState;
}

public function __toString(): string
{
return 'StatefulItem';
}
}

0 comments on commit 30297b3

Please sign in to comment.