Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.0', '8.1', '8.2', '8.3', '8.4']
name: PHP ${{ matrix.php }} Test on ${{ matrix.os }}
steps:
- name: Checkout
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.0",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10",
"sentry/sentry": "^3.0 || ^4.0"
},
"suggest": {
"sentry/sentry": "Required to use the sentry middleware."
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 30 additions & 0 deletions src/Middleware/SentryMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Onliner\CommandBus\Middleware;

use Onliner\CommandBus\Context;
use Onliner\CommandBus\Middleware;
use Sentry\EventHint;
use Sentry\ExceptionMechanism;
use Throwable;
use function Sentry\captureException;

final class SentryMiddleware implements Middleware
{
public function call(object $message, Context $context, callable $next): void
{
try {
$next($message, $context);
} catch (Throwable $error) {
$hint = EventHint::fromArray([
'mechanism' => new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, handled: false),
]);

captureException($error, $hint);

throw $error;
}
}
}
60 changes: 60 additions & 0 deletions tests/Middleware/SentryMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Onliner\CommandBus\Tests\Middleware;

use LogicException;
use Onliner\CommandBus\Builder;
use Onliner\CommandBus\Middleware\SentryMiddleware;
use Onliner\CommandBus\Tests\Command;
use PHPUnit\Framework\TestCase;
use Sentry\EventHint;
use Sentry\ExceptionMechanism;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;

class SentryMiddlewareTest extends TestCase
{
public function testLogEmpty(): void
{
$hub = self::createMock(HubInterface::class);
SentrySdk::setCurrentHub($hub);
$hub->expects(self::never())->method('captureException');

$dispatcher = (new Builder())
->handle(Command\Hello::class, function () {})
->middleware(new SentryMiddleware())
->build();

$dispatcher->dispatch(new Command\Hello('onliner'));
}

public function testCaptureErrors(): void
{
$hub = self::createMock(HubInterface::class);
SentrySdk::setCurrentHub($hub);

$hint = EventHint::fromArray([
'mechanism' => new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, handled: false),
]);
$e = new LogicException('expected');
$hub->expects(self::once())->method('captureException')->with($e, $hint);

$dispatcher = (new Builder())
->handle(Command\Hello::class, function () use ($e) {
throw $e;
})
->middleware(new SentryMiddleware())
->build();

$error = null;

try {
$dispatcher->dispatch(new Command\Hello('onliner'));
} catch (LogicException $error) {
}

self::assertNotNull($error);
}
}