-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for listening for parent classes or interfaces (#196)
* Refactor to a single `$targets` array * Fix styling
- Loading branch information
Showing
8 changed files
with
106 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
use Thunk\Verbs\Attributes\Hooks\On; | ||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\Lifecycle\Dispatcher; | ||
use Thunk\Verbs\Lifecycle\Phase; | ||
|
||
it('can match events by type-hinting a specific class', function () { | ||
app(Dispatcher::class)->register(new HooksClassHierarchyTestOneListener); | ||
|
||
expect(fn () => HooksClassHierarchyTestEvent1::fire())->toThrow(RuntimeException::class, 'one') | ||
->and(fn () => HooksClassHierarchyTestEvent2::fire())->not->toThrow(RuntimeException::class); | ||
}); | ||
|
||
it('can match events by type-hinting an interface', function () { | ||
app(Dispatcher::class)->register(new HooksClassHierarchyTestInterfaceListener); | ||
|
||
expect(fn () => HooksClassHierarchyTestEvent1::fire())->not->toThrow(RuntimeException::class) | ||
->and(fn () => HooksClassHierarchyTestEvent2::fire())->toThrow(RuntimeException::class, 'interface'); | ||
}); | ||
|
||
it('can match all events by type-hinting the base class', function () { | ||
app(Dispatcher::class)->register(new HooksClassHierarchyTestEveryListener); | ||
|
||
expect(fn () => HooksClassHierarchyTestEvent1::fire())->toThrow(RuntimeException::class, 'every') | ||
->and(fn () => HooksClassHierarchyTestEvent2::fire())->toThrow(RuntimeException::class, 'every'); | ||
}); | ||
|
||
interface HooksClassHierarchyTestInterface {} | ||
|
||
class HooksClassHierarchyTestEvent1 extends Event {} | ||
|
||
class HooksClassHierarchyTestEvent2 extends Event implements HooksClassHierarchyTestInterface {} | ||
|
||
class HooksClassHierarchyTestOneListener | ||
{ | ||
#[On(Phase::Validate)] | ||
public static function one(HooksClassHierarchyTestEvent1 $event) | ||
{ | ||
throw new RuntimeException('one'); | ||
} | ||
} | ||
|
||
class HooksClassHierarchyTestInterfaceListener | ||
{ | ||
#[On(Phase::Validate)] | ||
public static function interface(HooksClassHierarchyTestInterface $event) | ||
{ | ||
throw new RuntimeException('interface'); | ||
} | ||
} | ||
|
||
class HooksClassHierarchyTestEveryListener | ||
{ | ||
#[On(Phase::Validate)] | ||
public static function every(Event $event) | ||
{ | ||
throw new RuntimeException('every'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters