Refactor code and update tests for improved DX

This commit is contained in:
Younes ENNAJI
2026-03-02 05:19:34 +00:00
parent 0d25c72743
commit bd8169619e
9 changed files with 88 additions and 78 deletions
@@ -29,12 +29,7 @@ final class NotificationFactoryLocator implements NotificationFactoryLocatorInte
$factory = $factory();
if (!$factory instanceof NotificationFactoryInterface) {
throw new \InvalidArgumentException(\sprintf(
'Factory callable for "%s" must return an instance of %s, %s returned.',
$id,
NotificationFactoryInterface::class,
\get_debug_type($factory)
));
throw new \InvalidArgumentException(\sprintf('Factory callable for "%s" must return an instance of %s, %s returned.', $id, NotificationFactoryInterface::class, get_debug_type($factory)));
}
}
+1 -6
View File
@@ -82,12 +82,7 @@ final class ResponseManager implements ResponseManagerInterface
$presenter = $presenter();
if (!$presenter instanceof PresenterInterface) {
throw new \InvalidArgumentException(\sprintf(
'Presenter callable for "%s" must return an instance of %s, %s returned.',
$alias,
PresenterInterface::class,
\get_debug_type($presenter)
));
throw new \InvalidArgumentException(\sprintf('Presenter callable for "%s" must return an instance of %s, %s returned.', $alias, PresenterInterface::class, get_debug_type($presenter)));
}
}
+2 -2
View File
@@ -15,10 +15,10 @@ use Symfony\Contracts\Service\ResetInterface;
* Tagged with kernel.reset in services.php to be automatically called
* by Symfony's kernel between requests.
*/
final class WorkerListener implements ResetInterface
final readonly class WorkerListener implements ResetInterface
{
public function __construct(
private readonly ContentSecurityPolicyHandlerInterface $cspHandler,
private ContentSecurityPolicyHandlerInterface $cspHandler,
) {
}
@@ -89,7 +89,7 @@ final class FlasherContainerTest extends TestCase
public function testFromWithClosureReturningInvalidTypeThrowsException(): void
{
// Pass a closure that returns something other than ContainerInterface
FlasherContainer::from(fn () => 'not a container');
FlasherContainer::from(fn () => 'not a container'); // @phpstan-ignore argument.type
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected an instance of "Psr\Container\ContainerInterface"');
+28 -10
View File
@@ -23,19 +23,37 @@ final class TypeTest extends TestCase
$this->assertSame($expected, Type::all());
}
public function testIsValidWithValidTypes(): void
#[\PHPUnit\Framework\Attributes\DataProvider('validTypesProvider')]
public function testIsValidWithValidTypes(string $type): void
{
$this->assertTrue(Type::isValid('success'));
$this->assertTrue(Type::isValid('error'));
$this->assertTrue(Type::isValid('info'));
$this->assertTrue(Type::isValid('warning'));
$this->assertTrue(Type::isValid($type));
}
public function testIsValidWithInvalidTypes(): void
/**
* @return iterable<string, array{string}>
*/
public static function validTypesProvider(): iterable
{
$this->assertFalse(Type::isValid('invalid'));
$this->assertFalse(Type::isValid(''));
$this->assertFalse(Type::isValid('SUCCESS'));
$this->assertFalse(Type::isValid('notice'));
yield 'success' => ['success'];
yield 'error' => ['error'];
yield 'info' => ['info'];
yield 'warning' => ['warning'];
}
#[\PHPUnit\Framework\Attributes\DataProvider('invalidTypesProvider')]
public function testIsValidWithInvalidTypes(string $type): void
{
$this->assertFalse(Type::isValid($type));
}
/**
* @return iterable<string, array{string}>
*/
public static function invalidTypesProvider(): iterable
{
yield 'invalid' => ['invalid'];
yield 'empty' => [''];
yield 'uppercase SUCCESS' => ['SUCCESS'];
yield 'notice' => ['notice'];
}
}
@@ -7,12 +7,14 @@ namespace Flasher\Tests\Symfony\EventListener;
use Flasher\Prime\Http\Csp\ContentSecurityPolicyHandlerInterface;
use Flasher\Symfony\EventListener\WorkerListener;
use Flasher\Symfony\Storage\FallbackSession;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Service\ResetInterface;
final class WorkerListenerTest extends TestCase
{
private ContentSecurityPolicyHandlerInterface $cspHandler;
/** @var MockObject&ContentSecurityPolicyHandlerInterface */
private MockObject $cspHandler;
protected function setUp(): void
{