Skip to content

Commit

Permalink
feat: allow disabling middleware from configuration (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 authored Jan 7, 2025
1 parent 8d190e5 commit e49ae78
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^8.1",
"honeybadger-io/honeybadger-php": ">=2.21.0",
"honeybadger-io/honeybadger-php": ">=2.23.0",
"sixlive/dotenv-editor": "^1.1|^2.0",
"illuminate/console": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
Expand Down
5 changes: 5 additions & 0 deletions config/honeybadger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Honeybadger\BulkEventDispatcher;
use Honeybadger\Honeybadger;
use Honeybadger\HoneybadgerLaravel\HoneybadgerLaravel;
use Honeybadger\HoneybadgerLaravel\Middleware\AssignRequestId;

return [
/**
Expand Down Expand Up @@ -165,4 +166,8 @@
*/
'automatic' => HoneybadgerLaravel::DEFAULT_EVENTS,
],

'middleware' => [
AssignRequestId::class
]
];
14 changes: 13 additions & 1 deletion src/HoneybadgerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,19 @@ protected function registerReporters(): void

protected function registerMiddleware(): void
{
$middleware = config('honeybadger.middleware', [
// the default middleware if the config is not found - this should happen for
// all versions of the package up to and including 4.3.1
Middleware\AssignRequestId::class,
]);

if ($middleware == null || !is_array($middleware)) {
return;
}

$kernel = app(Kernel::class);
$kernel->prependMiddleware(Middleware\AssignRequestId::class);
foreach ($middleware as $class) {
$kernel->prependMiddleware($class);
}
}
}
28 changes: 28 additions & 0 deletions tests/HoneybadgerServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Honeybadger\Tests;

use Honeybadger\HoneybadgerLaravel\HoneybadgerServiceProvider;
use Honeybadger\HoneybadgerLaravel\Middleware\AssignRequestId;
use Honeybadger\LogEventHandler;
use Honeybadger\LogHandler;
use Honeybadger\Honeybadger;
use Honeybadger\Contracts\Reporter;
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger as HoneybadgerFacade;
use Illuminate\Contracts\Http\Kernel;

class HoneybadgerServiceProviderTest extends TestCase
{
Expand Down Expand Up @@ -35,4 +38,29 @@ public function it_registers_the_log_event_handler()
{
$this->assertInstanceOf(LogEventHandler::class, $this->app[LogEventHandler::class]);
}

/** @test */
public function it_registers_middleware_by_default()
{
$this->partialMock(Kernel::class, function ($mock) {
$mock->shouldReceive('prependMiddleware')
->with(AssignRequestId::class)
->once();
});
$provider = new HoneybadgerServiceProvider($this->app);
$provider->boot();
}

/** @test */
public function it_does_not_register_middleware_when_disabled()
{
$this->app['config']->set('honeybadger.middleware', []);
$this->partialMock(Kernel::class, function ($mock) {
$mock->shouldReceive('prependMiddleware')
->with(AssignRequestId::class)
->never();
});
$provider = new HoneybadgerServiceProvider($this->app);
$provider->boot();
}
}
8 changes: 8 additions & 0 deletions tests/LogEventDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public function getEvents(): array
{
return $this->events;
}

public function beforeNotify(callable $callback): void
{
}

public function beforeEvent(callable $callback): void
{
}
};
}

Expand Down

0 comments on commit e49ae78

Please sign in to comment.