Skip to content

Commit cb12be7

Browse files
authored
Merge pull request #33 from yvoyer/release/3.1.2
Fix #32 - Fix deprecations for 8.4
2 parents 88ac233 + 0a3b9fb commit cb12be7

12 files changed

Lines changed: 145 additions & 95 deletions

.github/workflows/php.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@ jobs:
4343
- name: PHPStan
4444
run: bin/phpstan
4545

46+
- name: Composer validate
47+
run: composer validate
48+
4649
# - name: Infection
4750
# run: bin/infection --formatter=progress

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"infection/infection": "~0.13",
3232
"phpstan/phpstan": "^2.0",
3333
"phpstan/phpstan-phpunit": "^2.0",
34-
"phpunit/phpunit": "^8.5",
34+
"phpunit/phpunit": "^10.0|^11.0|^12.0",
3535
"squizlabs/php_codesniffer": "^3.2",
3636
"symfony/cache": "^4.0|^5.0|^6.0"
3737
},

examples/ContextUsingCustomMetadataTest.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function test_it_should_allow_to_transit_from_pending_to_archived(): void
6464
$this->assertTrue($context->state->isInState('archived'));
6565
}
6666

67-
public function test_it_should_allow_to_transit_from_approved_to_published(): ContextStub
67+
public function test_it_should_allow_to_transit_from_approved_to_published(): void
6868
{
6969
$context = new ContextStub();
7070
$context->approve();
@@ -73,8 +73,6 @@ public function test_it_should_allow_to_transit_from_approved_to_published(): Co
7373
$context->publish();
7474

7575
$this->assertTrue($context->state->isInState('published'));
76-
77-
return $context;
7876
}
7977

8078
public function test_it_should_allow_to_transit_from_approved_to_archived(): void
@@ -100,7 +98,7 @@ public function test_it_should_allow_to_transit_from_published_to_approved(): vo
10098
$this->assertTrue($context->state->isInState('approved'));
10199
}
102100

103-
public function test_it_should_allow_to_transit_from_published_to_archived(): ContextStub
101+
public function test_it_should_allow_to_transit_from_published_to_archived(): void
104102
{
105103
$context = new ContextStub();
106104
$context->approve();
@@ -110,8 +108,6 @@ public function test_it_should_allow_to_transit_from_published_to_archived(): Co
110108
$context->archive();
111109

112110
$this->assertTrue($context->state->isInState('archived'));
113-
114-
return $context;
115111
}
116112

117113
public function test_it_should_allow_to_transit_from_archived_to_pending(): void
@@ -125,7 +121,7 @@ public function test_it_should_allow_to_transit_from_archived_to_pending(): void
125121
$this->assertTrue($context->state->isInState('pending'));
126122
}
127123

128-
public function test_it_should_allow_to_transit_from_archived_to_approved(): ContextStub
124+
public function test_it_should_allow_to_transit_from_archived_to_approved(): void
129125
{
130126
$context = new ContextStub();
131127
$context->discard();
@@ -134,8 +130,6 @@ public function test_it_should_allow_to_transit_from_archived_to_approved(): Con
134130
$context->unArchive();
135131

136132
$this->assertTrue($context->state->isInState('approved'));
137-
138-
return $context;
139133
}
140134

141135
public function test_attributes_of_pending(): void
@@ -146,34 +140,35 @@ public function test_attributes_of_pending(): void
146140
$this->assertFalse($context->isVisible());
147141
}
148142

149-
/**
150-
* @param ContextStub $context
151-
* @depends test_it_should_allow_to_transit_from_archived_to_approved
152-
*/
153-
public function test_attributes_of_approved(ContextStub $context): void
143+
public function test_attributes_of_approved(): void
154144
{
145+
$context = new ContextStub();
146+
$context->discard();
147+
$context->unArchive();
148+
155149
$this->assertTrue($context->state->isInState('approved'));
156150
$this->assertTrue($context->isDraft());
157151
$this->assertFalse($context->isVisible());
158152
}
159153

160-
/**
161-
* @param ContextStub $context
162-
* @depends test_it_should_allow_to_transit_from_approved_to_published
163-
*/
164-
public function test_attributes_of_published(ContextStub $context): void
154+
public function test_attributes_of_published(): void
165155
{
156+
$context = new ContextStub();
157+
$context->approve();
158+
$context->publish();
159+
166160
$this->assertTrue($context->state->isInState('published'));
167161
$this->assertFalse($context->isDraft());
168162
$this->assertTrue($context->isVisible());
169163
}
170164

171-
/**
172-
* @param ContextStub $context
173-
* @depends test_it_should_allow_to_transit_from_published_to_archived
174-
*/
175-
public function test_attributes_of_archived(ContextStub $context): void
165+
public function test_attributes_of_archived(): void
176166
{
167+
$context = new ContextStub();
168+
$context->approve();
169+
$context->publish();
170+
$context->archive();
171+
177172
$this->assertTrue($context->state->isInState('archived'));
178173
$this->assertFalse($context->isDraft());
179174
$this->assertFalse($context->isVisible());

phpunit.xml.dist

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
44
<phpunit
5-
backupGlobals = "false"
6-
backupStaticAttributes = "false"
7-
colors = "true"
8-
convertErrorsToExceptions = "true"
9-
convertNoticesToExceptions = "true"
10-
convertWarningsToExceptions = "true"
11-
convertDeprecationsToExceptions = "true"
12-
beStrictAboutOutputDuringTests = "true"
13-
beStrictAboutTestsThatDoNotTestAnything="true"
14-
failOnRisky="true"
15-
failOnWarning="true"
16-
bootstrap = "vendor/autoload.php" >
5+
backupGlobals="false"
6+
backupStaticProperties="false"
7+
colors="true"
8+
beStrictAboutOutputDuringTests="true"
9+
failOnAllIssues="true"
10+
bootstrap="vendor/autoload.php"
11+
>
1712

1813
<testsuites>
1914
<testsuite name="main">
@@ -25,9 +20,9 @@
2520
</testsuite>
2621
</testsuites>
2722

28-
<filter>
29-
<whitelist>
30-
<directory>src</directory>
31-
</whitelist>
32-
</filter>
23+
<source>
24+
<include>
25+
<directory suffix=".php">src</directory>
26+
</include>
27+
</source>
3328
</phpunit>

src/StateMachine.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Star\Component\State;
99

10+
use Closure;
1011
use Star\Component\State\Callbacks\AlwaysThrowExceptionOnFailure;
1112
use Star\Component\State\Callbacks\TransitionCallback;
1213
use Star\Component\State\Event\StateEventStore;
@@ -39,8 +40,11 @@ public function __construct(
3940
* @throws InvalidStateTransitionException
4041
* @throws NotFoundException
4142
*/
42-
public function transit(string $transitionName, $context, TransitionCallback $callback = null): string
43-
{
43+
public function transit(
44+
string $transitionName,
45+
mixed $context,
46+
?TransitionCallback $callback = null
47+
): string {
4448
if (!$callback) {
4549
$callback = new AlwaysThrowExceptionOnFailure();
4650
}
@@ -101,7 +105,7 @@ public function hasAttribute(string $attribute): bool
101105
return $this->states->hasAttribute($this->currentState, $attribute);
102106
}
103107

104-
public function addListener(string $event, \Closure $listener): void
108+
public function addListener(string $event, Closure $listener): void
105109
{
106110
$this->listeners->addListener($event, $listener);
107111
}

src/StateMetadata.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ private function getMachine(): StateMachine
3939
*
4040
* @return static
4141
*/
42-
final public function transit(string $name, $context, TransitionCallback $callback = null): StateMetadata
43-
{
42+
final public function transit(
43+
string $name,
44+
$context,
45+
?TransitionCallback $callback = null
46+
): StateMetadata {
4447
$this->current = $this->getMachine()->transit($name, $context, $callback);
4548

4649
return $this;

tests/StateMachineTest.php

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@
77

88
namespace Star\Component\State;
99

10-
use PHPUnit\Framework\MockObject\MockObject;
1110
use PHPUnit\Framework\TestCase;
1211
use Star\Component\State\Event\StateEventStore;
1312
use Star\Component\State\Event\TransitionWasFailed;
1413
use Star\Component\State\Event\TransitionWasSuccessful;
1514
use Star\Component\State\Event\TransitionWasRequested;
15+
use Star\Component\State\Stub\EventRegistrySpy;
1616
use Star\Component\State\Transitions\OneToOneTransition;
17+
use stdClass;
18+
use Throwable;
1719

1820
final class StateMachineTest extends TestCase
1921
{
2022
private TransitionRegistry $registry;
2123
private StateMachine $machine;
2224
private TestContext $context;
23-
/**
24-
* @var MockObject|EventRegistry
25-
*/
26-
private $listeners;
25+
private EventRegistrySpy $events;
2726

2827
public function setUp(): void
2928
{
30-
$this->listeners = $this->createMock(EventRegistry::class);
29+
$this->events = new EventRegistrySpy();
3130
$this->context = new TestContext();
3231
$this->registry = new TransitionRegistry();
33-
$this->machine = new StateMachine('current', $this->registry, $this->listeners);
32+
$this->machine = new StateMachine('current', $this->registry, $this->events);
3433
}
3534

3635
public function test_it_should_not_allow_to_transition_to_a_not_configured_transition(): void
@@ -52,30 +51,22 @@ public function test_it_should_transition_from_one_state_to_the_other(): void
5251

5352
public function test_it_should_trigger_an_event_before_any_transition(): void
5453
{
55-
$this->listeners
56-
->expects($this->at(0))
57-
->method('dispatch')
58-
->with(
59-
StateEventStore::BEFORE_TRANSITION,
60-
$this->isInstanceOf(TransitionWasRequested::class)
61-
);
62-
6354
$this->registry->addTransition(new OneToOneTransition('name', 'current', 'next'));
6455
$this->machine->transit('name', $this->context);
56+
$name = StateEventStore::BEFORE_TRANSITION;
57+
$events = $this->events->getDispatchedEvents($name);
58+
self::assertCount(1, $events);
59+
self::assertContainsOnlyInstancesOf(TransitionWasRequested::class, $events);
6560
}
6661

6762
public function test_it_should_trigger_an_event_after_any_transition(): void
6863
{
69-
$this->listeners
70-
->expects($this->at(1))
71-
->method('dispatch')
72-
->with(
73-
StateEventStore::AFTER_TRANSITION,
74-
$this->isInstanceOf(TransitionWasSuccessful::class)
75-
);
76-
7764
$this->registry->addTransition(new OneToOneTransition('name', 'current', 'next'));
7865
$this->machine->transit('name', $this->context);
66+
$name = StateEventStore::AFTER_TRANSITION;
67+
$events = $this->events->getDispatchedEvents($name);
68+
self::assertCount(1, $events);
69+
self::assertContainsOnlyInstancesOf(TransitionWasSuccessful::class, $events);
7970
}
8071

8172
public function test_it_should_throw_exception_with_class_context_when_transition_not_allowed(): void
@@ -87,7 +78,7 @@ public function test_it_should_throw_exception_with_class_context_when_transitio
8778
$this->expectExceptionMessage(
8879
"The transition 't' is not allowed when context 'stdClass' is in state 'current'."
8980
);
90-
$this->machine->transit('t', new \stdClass);
81+
$this->machine->transit('t', new stdClass);
9182
}
9283

9384
public function test_it_should_throw_exception_with_context_as_string_when_transition_not_allowed(): void
@@ -112,7 +103,7 @@ public function test_state_can_have_attribute(): void
112103
public function test_it_should_visit_the_transitions(): void
113104
{
114105
$registry = $this->createMock(StateRegistry::class);
115-
$machine = new StateMachine('', $registry, $this->listeners);
106+
$machine = new StateMachine('', $registry, $this->events);
116107
$visitor = $this->createMock(TransitionVisitor::class);
117108

118109
$registry
@@ -131,20 +122,18 @@ public function test_it_should_throw_exception_when_state_do_not_exists(): void
131122

132123
public function test_it_should_dispatch_an_event_before_a_transition_has_failed(): void
133124
{
134-
$this->listeners
135-
->expects($this->at(1))
136-
->method('dispatch')
137-
->with(
138-
StateEventStore::FAILURE_TRANSITION,
139-
$this->isInstanceOf(TransitionWasFailed::class)
140-
);
141-
142125
$this->registry->addTransition(new OneToOneTransition('t', 'from', 'to'));
143126
try {
144127
$this->machine->transit('t', 'context');
145128
$this->fail('An exception should have been thrown');
146-
} catch (InvalidStateTransitionException $exception) {
129+
} catch (Throwable $exception) {
147130
// silence it
131+
self::assertInstanceOf(InvalidStateTransitionException::class, $exception);
148132
}
133+
134+
$name = StateEventStore::FAILURE_TRANSITION;
135+
$events = $this->events->getDispatchedEvents($name);
136+
self::assertCount(1, $events);
137+
self::assertContainsOnlyInstancesOf(TransitionWasFailed::class, $events);
149138
}
150139
}

tests/Stub/EventRegistrySpy.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Star\Component\State\Stub;
4+
5+
use Star\Component\State\Event\StateEvent;
6+
use Star\Component\State\EventRegistry;
7+
8+
final class EventRegistrySpy implements EventRegistry
9+
{
10+
/**
11+
* @var array<string, callable[]>
12+
*/
13+
private array $listeners = [];
14+
15+
/**
16+
* @var array<string, array<int, StateEvent>>
17+
*/
18+
private array $dispatches = [];
19+
20+
public function dispatch(string $name, StateEvent $event): void
21+
{
22+
$this->dispatches[$name][] = $event;
23+
}
24+
25+
public function addListener(string $event, callable $listener): void
26+
{
27+
$this->listeners[$event][] = $listener;
28+
}
29+
30+
/**
31+
* @return StateEvent[]
32+
*/
33+
public function getDispatchedEvents(string $event): array
34+
{
35+
return $this->dispatches[$event] ?? [];
36+
}
37+
38+
/**
39+
* @return callable[]
40+
*/
41+
public function getListenersOfEvent(string $event): array
42+
{
43+
return $this->listeners[$event] ?? [];
44+
}
45+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Star\Component\State\RegistryBuilder;
66

7-
final class RegistrySpy implements RegistryBuilder
7+
final class RegistryBuilderSpy implements RegistryBuilder
88
{
99
/**
1010
* @var array<string, array{

0 commit comments

Comments
 (0)