You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am writing a package with a service provider which adds a middleware to a middleware group specified in the package's config. For a while I have been trying to write a test to make sure the middleware is registered to the correct middleware group. In the test I set the desired config use Config::set(), resolve the kernel and check if the appropriate middleware group contains the middleware.
The issue is that the app and with it the service provider is booted before the test case and therefore the config is set too late. I have tried to boot the app myself after setting the config but have not been able to do so.
Is there a way to hook into the booting of the app in order to change config before the service provider is booted?
This is my test case:
it('registers the middleware if no middleware group is configured', function () {
Config::set('statamic-redirect-entry-uri.middleware-group', null);
expect(resolve(Kernel::class)->hasMiddleware(RedirectEntryUri::class))->toBeTrue();
});
This is my service provider:
<?phpnamespaceNovu\RedirectEntryUri;
useIlluminate\Contracts\Http\Kernel;
useNovu\RedirectEntryUri\Http\Middleware\RedirectEntryUri;
useSpatie\LaravelPackageTools\Package;
useSpatie\LaravelPackageTools\PackageServiceProvider;
class RedirectEntryUriServiceProvider extends PackageServiceProvider
{
publicfunctionbootingPackage()
{
$this->registerMiddleware(
RedirectEntryUri::class,
config('statamic-redirect-entry-uri.middleware-group'),
);
}
publicfunctionconfigurePackage(Package$package): void
{
/* * This class is a Package Service Provider * * More info: https://github.com/spatie/laravel-package-tools */$package
->name('statamic-redirect-entry-uri')
->hasConfigFile();
}
/** * Register middleware. * * @param string $middleware */protectedfunctionregisterMiddleware(string$middleware, ?string$middlewareGroup = null)
{
$kernel = $this->app[Kernel::class];
if ($middlewareGroup) {
$kernel->appendMiddlewareToGroup($middlewareGroup, $middleware);
} else {
$kernel->pushMiddleware($middleware);
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am writing a package with a service provider which adds a middleware to a middleware group specified in the package's config. For a while I have been trying to write a test to make sure the middleware is registered to the correct middleware group. In the test I set the desired config use
Config::set()
, resolve the kernel and check if the appropriate middleware group contains the middleware.The issue is that the app and with it the service provider is booted before the test case and therefore the config is set too late. I have tried to boot the app myself after setting the config but have not been able to do so.
Is there a way to hook into the booting of the app in order to change config before the service provider is booted?
This is my test case:
This is my service provider:
Beta Was this translation helpful? Give feedback.
All reactions