|
3 | 3 | namespace Illuminate\Tests\Integration\Foundation;
|
4 | 4 |
|
5 | 5 | use Illuminate\Foundation\Events\DiscoverEvents;
|
| 6 | +use Illuminate\Support\Str; |
6 | 7 | use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Events\EventOne;
|
7 | 8 | use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Events\EventTwo;
|
8 | 9 | use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\AbstractListener;
|
9 | 10 | use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\Listener;
|
10 | 11 | use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\ListenerInterface;
|
11 | 12 | use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\UnionListeners\UnionListener;
|
12 | 13 | use Orchestra\Testbench\TestCase;
|
| 14 | +use SplFileInfo; |
13 | 15 |
|
14 | 16 | class DiscoverEventsTest extends TestCase
|
15 | 17 | {
|
| 18 | + protected function tearDown(): void |
| 19 | + { |
| 20 | + DiscoverEvents::$guessClassNamesUsingCallback = null; |
| 21 | + |
| 22 | + parent::tearDown(); |
| 23 | + } |
| 24 | + |
16 | 25 | public function testEventsCanBeDiscovered()
|
17 | 26 | {
|
18 | 27 | class_alias(Listener::class, 'Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\Listener');
|
@@ -47,4 +56,29 @@ class_alias(UnionListener::class, 'Tests\Integration\Foundation\Fixtures\EventDi
|
47 | 56 | ],
|
48 | 57 | ], $events);
|
49 | 58 | }
|
| 59 | + |
| 60 | + public function testEventsCanBeDiscoveredUsingCustomClassNameGuessing() |
| 61 | + { |
| 62 | + DiscoverEvents::guessClassNamesUsing(function (SplFileInfo $file, $basePath) { |
| 63 | + return Str::of($file->getRealPath()) |
| 64 | + ->after($basePath.DIRECTORY_SEPARATOR) |
| 65 | + ->before('.php') |
| 66 | + ->replace(DIRECTORY_SEPARATOR, '\\') |
| 67 | + ->ucfirst() |
| 68 | + ->prepend('Illuminate\\') |
| 69 | + ->toString(); |
| 70 | + }); |
| 71 | + |
| 72 | + $events = DiscoverEvents::within(__DIR__.'/Fixtures/EventDiscovery/Listeners', getcwd()); |
| 73 | + |
| 74 | + $this->assertEquals([ |
| 75 | + EventOne::class => [ |
| 76 | + Listener::class.'@handle', |
| 77 | + Listener::class.'@handleEventOne', |
| 78 | + ], |
| 79 | + EventTwo::class => [ |
| 80 | + Listener::class.'@handleEventTwo', |
| 81 | + ], |
| 82 | + ], $events); |
| 83 | + } |
50 | 84 | }
|
0 commit comments