Skip to content

Commit 97a9086

Browse files
committed
feat: don't run Boost during testing
Adds tests for: enabled, testing, environment
1 parent cf03125 commit 97a9086

File tree

3 files changed

+106
-14
lines changed

3 files changed

+106
-14
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"illuminate/contracts": "^10.0|^11.0|^12.0",
2020
"illuminate/routing": "^10.0|^11.0|^12.0",
2121
"illuminate/support": "^10.0|^11.0|^12.0",
22-
"laravel/mcp": "^0.1.0",
22+
"laravel/mcp": "^0.1.1",
2323
"laravel/prompts": "^0.1.9|^0.3",
2424
"laravel/roster": "^0.2"
2525
},

src/BoostServiceProvider.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919

2020
class BoostServiceProvider extends ServiceProvider
2121
{
22+
2223
public function register(): void
2324
{
2425
$this->mergeConfigFrom(
2526
__DIR__.'/../config/boost.php',
2627
'boost'
2728
);
2829

30+
if (! $this->shouldRun()) {
31+
return;
32+
}
33+
2934
$this->app->singleton(Roster::class, function () {
3035
$lockFiles = [
3136
base_path('composer.lock'),
@@ -55,24 +60,21 @@ public function register(): void
5560

5661
public function boot(Router $router): void
5762
{
58-
if (! config('boost.enabled', true)) {
63+
if (! $this->shouldRun()) {
5964
return;
6065
}
6166

62-
// Only enable Boost on local environments
63-
if (! app()->environment(['local', 'testing']) && config('app.debug', false) !== true) {
64-
return;
65-
}
66-
67-
// @phpstan-ignore-next-line
6867
Mcp::local('laravel-boost', Boost::class);
6968

7069
$this->registerPublishing();
7170
$this->registerCommands();
7271
$this->registerRoutes();
73-
$this->registerBrowserLogger();
74-
$this->callAfterResolving('blade.compiler', fn (BladeCompiler $bladeCompiler) => $this->registerBladeDirectives($bladeCompiler));
75-
$this->hookIntoResponses($router);
72+
73+
if (config('boost.browser_logs_watcher', true)) {
74+
$this->registerBrowserLogger();
75+
$this->callAfterResolving('blade.compiler', fn (BladeCompiler $bladeCompiler) => $this->registerBladeDirectives($bladeCompiler));
76+
$this->hookIntoResponses($router);
77+
}
7678
}
7779

7880
private function registerPublishing(): void
@@ -179,10 +181,24 @@ private static function mapJsTypeToPsr3Level(string $type): string
179181

180182
private function hookIntoResponses(Router $router): void
181183
{
182-
if (! config('boost.browser_logs_watcher', true)) {
183-
return;
184+
$router->pushMiddlewareToGroup('web', InjectBoost::class);
185+
}
186+
187+
private function shouldRun(): bool
188+
{
189+
if (! config('boost.enabled', true)) {
190+
return false;
184191
}
185192

186-
$router->pushMiddlewareToGroup('web', InjectBoost::class);
193+
if (app()->runningUnitTests()) {
194+
return false;
195+
}
196+
197+
// Only enable Boost on local environments or when debug is true
198+
if (! app()->environment('local') && config('app.debug', false) !== true) {
199+
return false;
200+
}
201+
202+
return true;
187203
}
188204
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Laravel\Boost\BoostServiceProvider;
7+
8+
beforeEach(function () {
9+
$this->refreshApplication();
10+
Config::set('logging.channels.browser', null);
11+
});
12+
13+
describe('boost.enabled configuration', function () {
14+
it('does not boot boost when disabled', function () {
15+
Config::set('boost.enabled', false);
16+
app()->detectEnvironment(fn() => 'local');
17+
18+
$provider = new BoostServiceProvider(app());
19+
$provider->register();
20+
$provider->boot(app('router'));
21+
22+
$this->artisan('list')->expectsOutputToContain('boost:install');
23+
});
24+
25+
it('boots boost when enabled in local environment', function () {
26+
Config::set('boost.enabled', true);
27+
app()->detectEnvironment(fn() => 'local');
28+
29+
$provider = new BoostServiceProvider(app());
30+
$provider->register();
31+
$provider->boot(app('router'));
32+
33+
expect(app()->bound(Laravel\Roster\Roster::class))->toBeTrue();
34+
expect(config('logging.channels.browser'))->not->toBeNull();
35+
});
36+
});
37+
38+
describe('environment restrictions', function () {
39+
it('does not boot boost in production even when enabled', function () {
40+
Config::set('boost.enabled', true);
41+
Config::set('app.debug', false);
42+
app()->detectEnvironment(fn() => 'production');
43+
44+
$provider = new BoostServiceProvider(app());
45+
$provider->register();
46+
$provider->boot(app('router'));
47+
48+
expect(config('logging.channels.browser'))->toBeNull();
49+
});
50+
51+
describe('testing environment', function () {
52+
it('does not boot boost when debug is false', function () {
53+
Config::set('boost.enabled', true);
54+
Config::set('app.debug', false);
55+
app()->detectEnvironment(fn() => 'testing');
56+
57+
$provider = new BoostServiceProvider(app());
58+
$provider->register();
59+
$provider->boot(app('router'));
60+
61+
expect(config('logging.channels.browser'))->toBeNull();
62+
});
63+
64+
it('does not boot boost when debug is true', function () {
65+
Config::set('boost.enabled', true);
66+
Config::set('app.debug', true);
67+
app()->detectEnvironment(fn() => 'testing');
68+
69+
$provider = new BoostServiceProvider(app());
70+
$provider->register();
71+
$provider->boot(app('router'));
72+
73+
expect(config('logging.channels.browser'))->toBeNull();
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)