Skip to content

Commit 22e3b09

Browse files
jdiddydaveinxilpro
andauthored
Allow StateId attribute to set an alias when using a single state (#102)
* failing test * Fix styling * Update StateAliasTest.php * Get to green! * Fix styling --------- Co-authored-by: jdiddydave <[email protected]> Co-authored-by: Chris Morrell <[email protected]>
1 parent dd1daf5 commit 22e3b09

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/Attributes/Autodiscovery/AppliesToState.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function discoverState(Event $event, StateManager $manager): array
3838
// TODO: Check type of data
3939

4040
if (! is_array($id)) {
41-
$this->alias = $this->inferAliasFromVariableName($property);
41+
$this->alias ??= $this->inferAliasFromVariableName($property);
4242
}
4343

4444
return collect(Arr::wrap($id))

src/Attributes/Autodiscovery/StateId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function discoverState(Event $event, StateManager $manager): array
3434
}
3535

3636
if (! is_array($id)) {
37-
$this->alias = $this->inferAliasFromVariableName($this->property->getName());
37+
$this->alias ??= $this->inferAliasFromVariableName($this->property->getName());
3838
}
3939

4040
return collect(Arr::wrap($id))

tests/Feature/StateAliasTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Thunk\Verbs\Attributes\Autodiscovery\AppliesToState;
4+
use Thunk\Verbs\Attributes\Autodiscovery\StateId;
5+
use Thunk\Verbs\Event;
6+
use Thunk\Verbs\Facades\Verbs;
7+
use Thunk\Verbs\State;
8+
9+
test('stateid attribute allows setting an alias for a state collection with a single state', function () {
10+
$event = StateAliasTestEvent::fire(bar: 1, baz: 2);
11+
12+
Verbs::commit();
13+
14+
$state_collection = $event->states();
15+
16+
expect($state_collection->aliasNames())->not->toContain('bar')
17+
->and($state_collection->aliasNames())->not->toContain('baz')
18+
->and($state_collection->aliasNames())->toContain('foo')
19+
->and($state_collection->aliasNames())->toContain('hello');
20+
});
21+
22+
class StateAliasTestState extends State
23+
{
24+
}
25+
26+
#[AppliesToState(StateAliasTestState::class, id: 'baz', alias: 'hello')]
27+
class StateAliasTestEvent extends Event
28+
{
29+
public function __construct(
30+
#[StateId(StateAliasTestState::class, 'foo')]
31+
public int $bar,
32+
public int $baz,
33+
) {
34+
}
35+
}

0 commit comments

Comments
 (0)