Release v1.4.0 #146
2 changed files with 50 additions and 52 deletions
|
|
@ -2,62 +2,11 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\Enums\LogLevelEnum;
|
|
||||||
use App\Events\ActionPerformed;
|
|
||||||
use App\Events\ExceptionOccurred;
|
|
||||||
use App\Events\NewArticleFetched;
|
|
||||||
use App\Events\RouteArticleApproved;
|
|
||||||
use App\Listeners\LogActionListener;
|
|
||||||
use App\Listeners\LogExceptionToDatabase;
|
|
||||||
use App\Listeners\PublishApprovedArticleListener;
|
|
||||||
use App\Listeners\ValidateArticleListener;
|
|
||||||
use Error;
|
|
||||||
use Illuminate\Contracts\Debug\ExceptionHandler;
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use InvalidArgumentException;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function register(): void {}
|
public function register(): void {}
|
||||||
|
|
||||||
public function boot(): void
|
public function boot(): void {}
|
||||||
{
|
|
||||||
Event::listen(
|
|
||||||
ActionPerformed::class,
|
|
||||||
LogActionListener::class,
|
|
||||||
);
|
|
||||||
|
|
||||||
Event::listen(
|
|
||||||
ExceptionOccurred::class,
|
|
||||||
LogExceptionToDatabase::class,
|
|
||||||
);
|
|
||||||
|
|
||||||
Event::listen(
|
|
||||||
NewArticleFetched::class,
|
|
||||||
ValidateArticleListener::class,
|
|
||||||
);
|
|
||||||
|
|
||||||
Event::listen(
|
|
||||||
RouteArticleApproved::class,
|
|
||||||
PublishApprovedArticleListener::class,
|
|
||||||
);
|
|
||||||
|
|
||||||
app()->make(ExceptionHandler::class)
|
|
||||||
->reportable(function (Throwable $e) {
|
|
||||||
$level = $this->mapExceptionToLogLevel($e);
|
|
||||||
|
|
||||||
ExceptionOccurred::dispatch($e, $level, $e->getMessage(), []);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private function mapExceptionToLogLevel(Throwable $exception): LogLevelEnum
|
|
||||||
{
|
|
||||||
return match (true) {
|
|
||||||
$exception instanceof Error => LogLevelEnum::CRITICAL,
|
|
||||||
$exception instanceof InvalidArgumentException => LogLevelEnum::WARNING,
|
|
||||||
default => LogLevelEnum::ERROR,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
49
tests/Feature/EventListenerRegistrationTest.php
Normal file
49
tests/Feature/EventListenerRegistrationTest.php
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Events\ActionPerformed;
|
||||||
|
use App\Events\ActivityLogged;
|
||||||
|
use App\Events\ExceptionOccurred;
|
||||||
|
use App\Events\NewArticleFetched;
|
||||||
|
use App\Events\RouteArticleApproved;
|
||||||
|
use Illuminate\Contracts\Debug\ExceptionHandler;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use RuntimeException;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class EventListenerRegistrationTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, array{class-string}>
|
||||||
|
*/
|
||||||
|
public static function eventProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'ActionPerformed' => [ActionPerformed::class],
|
||||||
|
'ActivityLogged' => [ActivityLogged::class],
|
||||||
|
'ExceptionOccurred' => [ExceptionOccurred::class],
|
||||||
|
'NewArticleFetched' => [NewArticleFetched::class],
|
||||||
|
'RouteArticleApproved' => [RouteArticleApproved::class],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param class-string $event
|
||||||
|
*/
|
||||||
|
#[DataProvider('eventProvider')]
|
||||||
|
public function test_event_has_exactly_one_listener(string $event): void
|
||||||
|
{
|
||||||
|
$this->assertCount(1, Event::getListeners($event));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_reporting_an_exception_dispatches_one_event(): void
|
||||||
|
{
|
||||||
|
Event::fake([ExceptionOccurred::class]);
|
||||||
|
|
||||||
|
app(ExceptionHandler::class)->report(new RuntimeException('boom'));
|
||||||
|
|
||||||
|
Event::assertDispatchedTimes(ExceptionOccurred::class, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue