Release v1.4.0 #146

Merged
myrmidex merged 71 commits from release/v1.4.0 into main 2026-08-15 00:36:54 +02:00
2 changed files with 50 additions and 52 deletions
Showing only changes of commit d3e589c36c - Show all commits

View file

@ -2,62 +2,11 @@
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 InvalidArgumentException;
use Throwable;
class AppServiceProvider extends ServiceProvider
{
public function register(): 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,
};
}
public function boot(): void {}
}

View 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);
}
}