Skip to content

Commit

Permalink
Allow StateId attribute to set an alias when using a single state (#102)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Apr 9, 2024
1 parent dd1daf5 commit 22e3b09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Attributes/Autodiscovery/AppliesToState.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function discoverState(Event $event, StateManager $manager): array
// TODO: Check type of data

if (! is_array($id)) {
$this->alias = $this->inferAliasFromVariableName($property);
$this->alias ??= $this->inferAliasFromVariableName($property);
}

return collect(Arr::wrap($id))
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Autodiscovery/StateId.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function discoverState(Event $event, StateManager $manager): array
}

if (! is_array($id)) {
$this->alias = $this->inferAliasFromVariableName($this->property->getName());
$this->alias ??= $this->inferAliasFromVariableName($this->property->getName());
}

return collect(Arr::wrap($id))
Expand Down
35 changes: 35 additions & 0 deletions tests/Feature/StateAliasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Thunk\Verbs\Attributes\Autodiscovery\AppliesToState;
use Thunk\Verbs\Attributes\Autodiscovery\StateId;
use Thunk\Verbs\Event;
use Thunk\Verbs\Facades\Verbs;
use Thunk\Verbs\State;

test('stateid attribute allows setting an alias for a state collection with a single state', function () {
$event = StateAliasTestEvent::fire(bar: 1, baz: 2);

Verbs::commit();

$state_collection = $event->states();

expect($state_collection->aliasNames())->not->toContain('bar')
->and($state_collection->aliasNames())->not->toContain('baz')
->and($state_collection->aliasNames())->toContain('foo')
->and($state_collection->aliasNames())->toContain('hello');
});

class StateAliasTestState extends State
{
}

#[AppliesToState(StateAliasTestState::class, id: 'baz', alias: 'hello')]
class StateAliasTestEvent extends Event
{
public function __construct(
#[StateId(StateAliasTestState::class, 'foo')]
public int $bar,
public int $baz,
) {
}
}

0 comments on commit 22e3b09

Please sign in to comment.