Skip to content

Commit 30297b3

Browse files
committed
Improve coverage and tests
1 parent 8f81a22 commit 30297b3

File tree

7 files changed

+61
-53
lines changed

7 files changed

+61
-53
lines changed

src/Implementation/Events/StateChanged.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ public function getOldState(): StateInterface
2828
{
2929
return $this->oldState;
3030
}
31+
32+
public function __toString(): string
33+
{
34+
$item = $this->getItem();
35+
36+
return sprintf(
37+
'%s[%s, %s->%s]',
38+
__CLASS__,
39+
method_exists($item, '__toString') ? $item : get_class($item),
40+
$this->getOldState(),
41+
$item->getState()
42+
);
43+
}
3144
}

src/Implementation/Events/StateChanging.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ public function getNewState(): StateInterface
2828
{
2929
return $this->newState;
3030
}
31+
32+
public function __toString(): string
33+
{
34+
$item = $this->getItem();
35+
36+
return sprintf(
37+
'%s[%s, %s->%s]',
38+
__CLASS__,
39+
method_exists($item, '__toString') ? $item : get_class($item),
40+
$item->getState(),
41+
$this->getNewState()
42+
);
43+
}
3144
}

src/Implementation/Traits/Plantable.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@ trait Plantable
1414
{
1515
public function toPlantUML(): string
1616
{
17-
/** @var $this TransitionRepositoryInterface */
18-
return implode(PHP_EOL, array_merge(
19-
['@startuml', ''],
20-
array_map(
21-
static function (TransitionInterface $transition): string {
22-
$oldState = $transition->getOldState();
23-
$newState = $transition->getNewState();
24-
$oldText = $oldState instanceof DescribableInterface ? $oldState->getDescription() : $oldState->getName();
25-
$newText = $newState instanceof DescribableInterface ? $newState->getDescription() : $newState->getName();
17+
$generateNodesAndEdges = static function (TransitionInterface $transition): string {
18+
$oldText = ($oldState = $transition->getOldState()) instanceof DescribableInterface
19+
? $oldState->getDescription() : $oldState->getName();
20+
$newText = ($newState = $transition->getNewState()) instanceof DescribableInterface
21+
? $newState->getDescription() : $newState->getName();
22+
23+
$result = "($oldText) --> ($newText)";
2624

27-
$result = "($oldText) --> ($newText)";
25+
if ($transition instanceof DescribableInterface) {
26+
$result .= " : {$transition->getDescription()}";
27+
}
2828

29-
if ($transition instanceof DescribableInterface) {
30-
$result .= " : {$transition->getDescription()}";
31-
}
29+
return $result;
30+
};
3231

33-
return $result;
34-
},
35-
iterator_to_array($this)
36-
),
32+
/** @var $this TransitionRepositoryInterface */
33+
return implode(PHP_EOL, array_merge(
34+
['@startuml', ''],
35+
array_map($generateNodesAndEdges, iterator_to_array($this)),
3736
['', '@enduml']
3837
));
3938
}

tests/BuilderTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@ public function test_that_new_state_must_be_declared_before_transitions(): void
5050

5151
public function test_that_transitioning_with_mutator_works(): void
5252
{
53-
$this->expectNotToPerformAssertions();
54-
55-
$engine = Builder::create()
56-
->defState('a')
57-
->defState('b')
58-
->defTransition('a', 'b')
59-
->getEngine();
60-
61-
$item = new StatefulItem(new State('a'));
53+
$item = new StatefulItem(new State('started'));
6254
$mutator = Builder::makeStateMutator(
6355
static function () use ($item): State {
6456
return $item->getState();
@@ -68,6 +60,8 @@ static function (State $newState) use ($item): void {
6860
}
6961
);
7062

71-
$engine->changeState($mutator, new State('b'));
63+
$this->builder->getEngine()->changeState($mutator, new State('finished'));
64+
65+
$this->assertSame('finished', $item->getState()->getName());
7266
}
7367
}

tests/EventTest.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
use Psr\EventDispatcher\EventDispatcherInterface;
77
use uuf6429\StateEngine\Implementation\Builder;
88
use uuf6429\StateEngine\Implementation\Entities\State;
9-
use uuf6429\StateEngine\Implementation\Events\StateChanged;
10-
use uuf6429\StateEngine\Implementation\Events\StateChanging;
119
use uuf6429\StateEngine\Interfaces\EngineInterface;
12-
use uuf6429\StateEngine\Interfaces\StateAwareInterface;
13-
use uuf6429\StateEngine\Interfaces\StateInterface;
1410

1511
class EventTest extends TestCase
1612
{
@@ -32,7 +28,7 @@ protected function setUp(): void
3228
->getEngine($this->dispatcher);
3329
}
3430

35-
public function test_that_engine_fires_exception(): void
31+
public function test_that_engine_triggers_events(): void
3632
{
3733
$dispatchedEvents = [];
3834
$this->dispatcher
@@ -43,30 +39,16 @@ public function test_that_engine_fires_exception(): void
4339
$startedState = new State('started');
4440
$finishedState = new State('finished');
4541

46-
$statefulItem = new class implements StateAwareInterface {
47-
public StateInterface $state;
48-
49-
public function getState(): StateInterface
50-
{
51-
return $this->state;
52-
}
53-
54-
public function setState(StateInterface $newState): void
55-
{
56-
$this->state = $newState;
57-
}
58-
};
59-
60-
$statefulItem->state = $startedState;
42+
$statefulItem = new StatefulItem($startedState);
6143
$this->engine->changeState($statefulItem, $finishedState);
6244

63-
$this->assertEquals($finishedState, $statefulItem->state);
45+
$this->assertEquals($finishedState, $statefulItem->getState());
6446
$this->assertEquals(
6547
[
66-
new StateChanging($statefulItem, $finishedState),
67-
new StateChanged($statefulItem, $startedState),
48+
'uuf6429\StateEngine\Implementation\Events\StateChanging[StatefulItem, finished->finished]',
49+
'uuf6429\StateEngine\Implementation\Events\StateChanged[StatefulItem, started->finished]',
6850
],
69-
$dispatchedEvents
51+
array_map('strval', $dispatchedEvents)
7052
);
7153
}
7254
}

tests/JiraIssueTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,19 @@ static function (TransitionInterface $transition) {
8181

8282
public function test_that_transitioning_from_in_dev_to_ready_for_qa_is_allowed(): void
8383
{
84-
$this->expectNotToPerformAssertions();
85-
8684
$item = new StatefulItem(new State('in-dev'));
85+
8786
$this->engine->changeState($item, new State('ready-for-qa'));
87+
88+
$this->assertSame('ready-for-qa', $item->getState()->getName());
8889
}
8990

9091
public function test_that_transitioning_from_in_dev_to_in_qa_is_not_allowed(): void
9192
{
9293
$this->expectExceptionMessage('Cannot apply transition "in-dev -> in-qa"; no such transition was defined.');
9394

9495
$item = new StatefulItem(new State('in-dev'));
96+
9597
$this->engine->changeState($item, new State('in-qa'));
9698
}
9799

tests/StatefulItem.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public function setState(StateInterface $newState): void
2323
{
2424
$this->state = $newState;
2525
}
26+
27+
public function __toString(): string
28+
{
29+
return 'StatefulItem';
30+
}
2631
}

0 commit comments

Comments
 (0)