-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved event discovery (Laravel 10/11 compatible) (#88)
* Config switch added for event autodiscovery * WIP * WIP * Working Laravel 10 * Remove old test * Working Laravel 11 * Fix code style --------- Co-authored-by: Alex Wulf <alex.f.wulf@gmail.com>
- Loading branch information
Showing
20 changed files
with
508 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace InterNACHI\Modular\Tests\Concerns; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
|
||
trait PreloadsAppModules | ||
{ | ||
protected static $autoloader_registered = false; | ||
|
||
/** @before */ | ||
public function prepareTestModule(): void | ||
{ | ||
$src = __DIR__.'/../testbench-core/app-modules'; | ||
$dest = static::applicationBasePath().'/app-modules'; | ||
|
||
$fs = new Filesystem(); | ||
$fs->deleteDirectory($dest); | ||
$fs->copyDirectory($src, $dest); | ||
} | ||
|
||
/** @before */ | ||
public function prepareModuleAutoloader(): void | ||
{ | ||
if (! static::$autoloader_registered) { | ||
spl_autoload_register(function($fqcn) { | ||
if (str_starts_with($fqcn, 'Modules\\TestModule\\')) { | ||
$path = str_replace( | ||
['Modules\\TestModule\\', '\\'], | ||
['', DIRECTORY_SEPARATOR], | ||
$fqcn | ||
); | ||
$path = static::applicationBasePath().'/app-modules/test-module/src/'.$path.'.php'; | ||
if (file_exists($path)) { | ||
include_once $path; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
static::$autoloader_registered = true; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
tests/EventDiscovery/EventDiscoveryExplicitlyDisabledTest.php
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,67 @@ | ||
<?php | ||
|
||
// Because we need to preload files before TestBench boots the app, | ||
// this needs to be its own isolated test file. | ||
|
||
namespace InterNACHI\Modular\Tests\EventDiscovery { | ||
use App\EventDiscoveryExplicitlyDisabledTestProvider; | ||
use Illuminate\Support\Facades\Event; | ||
use InterNACHI\Modular\Support\Facades\Modules; | ||
use InterNACHI\Modular\Tests\Concerns\PreloadsAppModules; | ||
use InterNACHI\Modular\Tests\TestCase; | ||
|
||
class EventDiscoveryExplicitlyDisabledTest extends TestCase | ||
{ | ||
use PreloadsAppModules; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->beforeApplicationDestroyed(fn() => $this->artisan('event:clear')); | ||
} | ||
|
||
public function test_it_does_not_auto_discover_event_listeners(): void | ||
{ | ||
$module = Modules::module('test-module'); | ||
|
||
$this->assertEmpty(Event::getListeners($module->qualify('Events\\TestEvent'))); | ||
|
||
// Also check that the events are cached correctly | ||
|
||
$this->artisan('event:cache'); | ||
|
||
$cache = require $this->app->getCachedEventsPath(); | ||
|
||
$this->assertEmpty($cache[EventDiscoveryExplicitlyDisabledTestProvider::class]); | ||
|
||
$this->artisan('event:clear'); | ||
} | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return array_merge([EventDiscoveryExplicitlyDisabledTestProvider::class], parent::getPackageProviders($app)); | ||
} | ||
|
||
protected function resolveApplicationConfiguration($app) | ||
{ | ||
parent::resolveApplicationConfiguration($app); | ||
|
||
$app['config']['app-modules.should_discover_events'] = false; | ||
} | ||
} | ||
} | ||
|
||
// We need to use an "App" namespace to tell modular that this provider should be deferred to | ||
|
||
namespace App { | ||
use Illuminate\Foundation\Support\Providers\EventServiceProvider; | ||
|
||
class EventDiscoveryExplicitlyDisabledTestProvider extends EventServiceProvider | ||
{ | ||
public function shouldDiscoverEvents() | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
tests/EventDiscovery/EventDiscoveryExplicitlyEnabledTest.php
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,72 @@ | ||
<?php | ||
|
||
// Because we need to preload files before TestBench boots the app, | ||
// this needs to be its own isolated test file. | ||
|
||
namespace InterNACHI\Modular\Tests\EventDiscovery { | ||
use App\EventDiscoveryExplicitlyEnabledTestProvider; | ||
use Illuminate\Support\Facades\Event; | ||
use InterNACHI\Modular\Support\Facades\Modules; | ||
use InterNACHI\Modular\Tests\Concerns\PreloadsAppModules; | ||
use InterNACHI\Modular\Tests\TestCase; | ||
|
||
class EventDiscoveryExplicitlyEnabledTest extends TestCase | ||
{ | ||
use PreloadsAppModules; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->beforeApplicationDestroyed(fn() => $this->artisan('event:clear')); | ||
} | ||
|
||
public function test_it_auto_discovers_event_listeners(): void | ||
{ | ||
$module = Modules::module('test-module'); | ||
|
||
$this->assertNotEmpty(Event::getListeners($module->qualify('Events\\TestEvent'))); | ||
|
||
// Also check that the events are cached correctly | ||
|
||
$this->artisan('event:cache'); | ||
|
||
$cache = require $this->app->getCachedEventsPath(); | ||
|
||
$this->assertArrayHasKey($module->qualify('Events\\TestEvent'), $cache[EventDiscoveryExplicitlyEnabledTestProvider::class]); | ||
|
||
$this->assertContains( | ||
$module->qualify('Listeners\\TestEventListener@handle'), | ||
$cache[EventDiscoveryExplicitlyEnabledTestProvider::class][$module->qualify('Events\\TestEvent')] | ||
); | ||
|
||
$this->artisan('event:clear'); | ||
} | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return array_merge([EventDiscoveryExplicitlyEnabledTestProvider::class], parent::getPackageProviders($app)); | ||
} | ||
|
||
protected function resolveApplicationConfiguration($app) | ||
{ | ||
parent::resolveApplicationConfiguration($app); | ||
|
||
$app['config']['app-modules.should_discover_events'] = true; | ||
} | ||
} | ||
} | ||
|
||
// We need to use an "App" namespace to tell modular that this provider should be deferred to | ||
|
||
namespace App { | ||
use Illuminate\Foundation\Support\Providers\EventServiceProvider; | ||
|
||
class EventDiscoveryExplicitlyEnabledTestProvider extends EventServiceProvider | ||
{ | ||
public function shouldDiscoverEvents() | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
tests/EventDiscovery/EventDiscoveryImplicitlyDisabledTest.php
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 | ||
|
||
// Because we need to preload files before TestBench boots the app, | ||
// this needs to be its own isolated test file. | ||
|
||
namespace InterNACHI\Modular\Tests\EventDiscovery { | ||
use App\EventDiscoveryImplicitlyDisabledTestProvider; | ||
use Illuminate\Support\Facades\Event; | ||
use InterNACHI\Modular\Support\Facades\Modules; | ||
use InterNACHI\Modular\Tests\Concerns\PreloadsAppModules; | ||
use InterNACHI\Modular\Tests\TestCase; | ||
|
||
class EventDiscoveryImplicitlyDisabledTest extends TestCase | ||
{ | ||
use PreloadsAppModules; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->beforeApplicationDestroyed(fn() => $this->artisan('event:clear')); | ||
} | ||
|
||
public function test_it_does_not_auto_discover_event_listeners(): void | ||
{ | ||
$module = Modules::module('test-module'); | ||
|
||
$this->assertEmpty(Event::getListeners($module->qualify('Events\\TestEvent'))); | ||
|
||
// Also check that the events are cached correctly | ||
|
||
$this->artisan('event:cache'); | ||
|
||
$cache = require $this->app->getCachedEventsPath(); | ||
|
||
$this->assertEmpty($cache[EventDiscoveryImplicitlyDisabledTestProvider::class]); | ||
|
||
$this->artisan('event:clear'); | ||
} | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return array_merge([EventDiscoveryImplicitlyDisabledTestProvider::class], parent::getPackageProviders($app)); | ||
} | ||
} | ||
} | ||
|
||
// We need to use an "App" namespace to tell modular that this provider should be deferred to | ||
|
||
namespace App { | ||
use Illuminate\Foundation\Support\Providers\EventServiceProvider; | ||
|
||
class EventDiscoveryImplicitlyDisabledTestProvider extends EventServiceProvider | ||
{ | ||
public function shouldDiscoverEvents() | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.