Files
breadcrumb-the-shire/tests/Unit/Module/ModuleEventDispatcherTest.php
fs 555199b217 fix: PHPStan and test strictness improvements
Add bin/ scripts to PHPStan scanFiles, suppress property.onlyWritten for
by-ref test properties, add exhaustive default match arm, use explicit
expects() on mock stubs, fix data provider return type, and simplify
redundant null check in NotificationServiceTest.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 14:15:36 +01:00

104 lines
3.3 KiB
PHP

<?php
namespace MintyPHP\Tests\Unit\Module;
use MintyPHP\App\AppContainer;
use MintyPHP\App\Module\Contracts\EventListener;
use MintyPHP\App\Module\ModuleEventDispatcher;
use PHPUnit\Framework\TestCase;
final class ModuleEventDispatcherTest extends TestCase
{
public function testDispatchCallsRegisteredListeners(): void
{
$called = false;
$capturedEvent = '';
$capturedPayload = [];
$listener = new class ($called, $capturedEvent, $capturedPayload) implements EventListener {
public function __construct(
/** @phpstan-ignore property.onlyWritten */
private bool &$called,
/** @phpstan-ignore property.onlyWritten */
private string &$capturedEvent,
/** @phpstan-ignore property.onlyWritten */
private array &$capturedPayload
) {
}
public function handle(string $event, array $payload): void
{
$this->called = true;
$this->capturedEvent = $event;
$this->capturedPayload = $payload;
}
};
$container = new AppContainer();
$container->set($listener::class, static fn () => $listener);
$dispatcher = new ModuleEventDispatcher(
['user.deleted' => [['class' => $listener::class, 'method' => 'handle']]],
$container
);
$dispatcher->dispatch('user.deleted', ['user_id' => 42]);
self::assertTrue($called);
self::assertSame('user.deleted', $capturedEvent);
self::assertSame(['user_id' => 42], $capturedPayload);
}
public function testDispatchIsNoOpForUnknownEvent(): void
{
$container = new AppContainer();
$dispatcher = new ModuleEventDispatcher([], $container);
// Should not throw
$dispatcher->dispatch('unknown.event', ['data' => 'test']);
self::addToAssertionCount(1);
}
public function testDispatchContinuesAfterListenerException(): void
{
$secondCalled = false;
$failingListener = new class () implements EventListener {
public function handle(string $event, array $payload): void
{
throw new \RuntimeException('Listener failure');
}
};
$successListener = new class ($secondCalled) implements EventListener {
public function __construct(/** @phpstan-ignore property.onlyWritten */ private bool &$called)
{
}
public function handle(string $event, array $payload): void
{
$this->called = true;
}
};
$container = new AppContainer();
$container->set($failingListener::class, static fn () => $failingListener);
$container->set($successListener::class, static fn () => $successListener);
$dispatcher = new ModuleEventDispatcher(
[
'user.created' => [
['class' => $failingListener::class, 'method' => 'handle'],
['class' => $successListener::class, 'method' => 'handle'],
],
],
$container
);
$dispatcher->dispatch('user.created', ['user_id' => 1]);
self::assertTrue($secondCalled, 'Second listener should be called even after the first one throws');
}
}