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
+2 -2
View File
@@ -72,7 +72,7 @@ final class FlasherContainer
* @param string $id The service identifier (e.g., 'flasher', 'flasher.toastr') * @param string $id The service identifier (e.g., 'flasher', 'flasher.toastr')
* *
* @throws \InvalidArgumentException If the service is not found or invalid * @throws \InvalidArgumentException If the service is not found or invalid
* @throws \LogicException If the container has not been initialized * @throws \LogicException If the container has not been initialized
* *
* @phpstan-return ($id is 'flasher' ? \Flasher\Prime\FlasherInterface : * @phpstan-return ($id is 'flasher' ? \Flasher\Prime\FlasherInterface :
* ($id is 'flasher.noty' ? \Flasher\Noty\Prime\NotyInterface : * ($id is 'flasher.noty' ? \Flasher\Noty\Prime\NotyInterface :
@@ -111,7 +111,7 @@ final class FlasherContainer
/** /**
* Get the underlying PSR-11 container. * Get the underlying PSR-11 container.
* *
* @throws \LogicException If the container has not been initialized * @throws \LogicException If the container has not been initialized
* @throws \InvalidArgumentException If the container closure returns an invalid type * @throws \InvalidArgumentException If the container closure returns an invalid type
*/ */
public static function getContainer(): ContainerInterface public static function getContainer(): ContainerInterface
@@ -13,32 +13,32 @@ use Flasher\Prime\Stamp\StampInterface;
* *
* @mixin \Flasher\Prime\Notification\NotificationBuilderInterface * @mixin \Flasher\Prime\Notification\NotificationBuilderInterface
* *
* @method $this title(string $title) Set the notification title * @method $this title(string $title) Set the notification title
* @method $this message(string $message) Set the notification message * @method $this message(string $message) Set the notification message
* @method $this type(string $type) Set the notification type (success, error, info, warning) * @method $this type(string $type) Set the notification type (success, error, info, warning)
* @method $this options(array<string, mixed> $options, bool $append = true) Set notification options * @method $this options(array<string, mixed> $options, bool $append = true) Set notification options
* @method $this option(string $name, mixed $value) Set a single notification option * @method $this option(string $name, mixed $value) Set a single notification option
* @method $this priority(int $priority) Set the notification priority * @method $this priority(int $priority) Set the notification priority
* @method $this hops(int $amount) Set the number of requests the notification should persist * @method $this hops(int $amount) Set the number of requests the notification should persist
* @method $this keep() Keep the notification for one more request * @method $this keep() Keep the notification for one more request
* @method $this delay(int $delay) Set the delay in milliseconds before showing the notification * @method $this delay(int $delay) Set the delay in milliseconds before showing the notification
* @method $this translate(array<string, mixed> $parameters = [], ?string $locale = null) Mark the notification for translation * @method $this translate(array<string, mixed> $parameters = [], ?string $locale = null) Mark the notification for translation
* @method $this handler(string $handler) Set the notification handler/adapter * @method $this handler(string $handler) Set the notification handler/adapter
* @method $this context(array<string, mixed> $context) Set additional context data * @method $this context(array<string, mixed> $context) Set additional context data
* @method $this when(bool|\Closure $condition) Conditionally show the notification * @method $this when(bool|\Closure $condition) Conditionally show the notification
* @method $this unless(bool|\Closure $condition) Conditionally hide the notification * @method $this unless(bool|\Closure $condition) Conditionally hide the notification
* @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification * @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification
* @method Envelope getEnvelope() Get the notification envelope * @method Envelope getEnvelope() Get the notification envelope
* @method Envelope success(string $message, array<string, mixed> $options = [], ?string $title = null) Create a success notification * @method Envelope success(string $message, array<string, mixed> $options = [], ?string $title = null) Create a success notification
* @method Envelope error(string $message, array<string, mixed> $options = [], ?string $title = null) Create an error notification * @method Envelope error(string $message, array<string, mixed> $options = [], ?string $title = null) Create an error notification
* @method Envelope info(string $message, array<string, mixed> $options = [], ?string $title = null) Create an info notification * @method Envelope info(string $message, array<string, mixed> $options = [], ?string $title = null) Create an info notification
* @method Envelope warning(string $message, array<string, mixed> $options = [], ?string $title = null) Create a warning notification * @method Envelope warning(string $message, array<string, mixed> $options = [], ?string $title = null) Create a warning notification
* @method Envelope flash(?string $type = null, ?string $message = null, array<string, mixed> $options = [], ?string $title = null) Create a flash notification * @method Envelope flash(?string $type = null, ?string $message = null, array<string, mixed> $options = [], ?string $title = null) Create a flash notification
* @method Envelope push() Push the notification to storage * @method Envelope push() Push the notification to storage
* @method Envelope created(string|object|null $resource = null) Create a "resource created" notification * @method Envelope created(string|object|null $resource = null) Create a "resource created" notification
* @method Envelope updated(string|object|null $resource = null) Create a "resource updated" notification * @method Envelope updated(string|object|null $resource = null) Create a "resource updated" notification
* @method Envelope saved(string|object|null $resource = null) Create a "resource saved" notification * @method Envelope saved(string|object|null $resource = null) Create a "resource saved" notification
* @method Envelope deleted(string|object|null $resource = null) Create a "resource deleted" notification * @method Envelope deleted(string|object|null $resource = null) Create a "resource deleted" notification
*/ */
interface NotificationFactoryInterface interface NotificationFactoryInterface
{ {
@@ -29,12 +29,7 @@ final class NotificationFactoryLocator implements NotificationFactoryLocatorInte
$factory = $factory(); $factory = $factory();
if (!$factory instanceof NotificationFactoryInterface) { if (!$factory instanceof NotificationFactoryInterface) {
throw new \InvalidArgumentException(\sprintf( throw new \InvalidArgumentException(\sprintf('Factory callable for "%s" must return an instance of %s, %s returned.', $id, NotificationFactoryInterface::class, get_debug_type($factory)));
'Factory callable for "%s" must return an instance of %s, %s returned.',
$id,
NotificationFactoryInterface::class,
\get_debug_type($factory)
));
} }
} }
+25 -25
View File
@@ -16,32 +16,32 @@ use Flasher\Prime\Stamp\StampInterface;
* *
* @phpstan-import-type ArrayPresenterType from ArrayPresenter * @phpstan-import-type ArrayPresenterType from ArrayPresenter
* *
* @method $this title(string $title) Set the notification title * @method $this title(string $title) Set the notification title
* @method $this message(string $message) Set the notification message * @method $this message(string $message) Set the notification message
* @method $this type(string $type) Set the notification type (success, error, info, warning) * @method $this type(string $type) Set the notification type (success, error, info, warning)
* @method $this options(array<string, mixed> $options, bool $append = true) Set notification options * @method $this options(array<string, mixed> $options, bool $append = true) Set notification options
* @method $this option(string $name, mixed $value) Set a single notification option * @method $this option(string $name, mixed $value) Set a single notification option
* @method $this priority(int $priority) Set the notification priority * @method $this priority(int $priority) Set the notification priority
* @method $this hops(int $amount) Set the number of requests the notification should persist * @method $this hops(int $amount) Set the number of requests the notification should persist
* @method $this keep() Keep the notification for one more request * @method $this keep() Keep the notification for one more request
* @method $this delay(int $delay) Set the delay in milliseconds before showing the notification * @method $this delay(int $delay) Set the delay in milliseconds before showing the notification
* @method $this translate(array<string, mixed> $parameters = [], ?string $locale = null) Mark the notification for translation * @method $this translate(array<string, mixed> $parameters = [], ?string $locale = null) Mark the notification for translation
* @method $this handler(string $handler) Set the notification handler/adapter * @method $this handler(string $handler) Set the notification handler/adapter
* @method $this context(array<string, mixed> $context) Set additional context data * @method $this context(array<string, mixed> $context) Set additional context data
* @method $this when(bool|\Closure $condition) Conditionally show the notification * @method $this when(bool|\Closure $condition) Conditionally show the notification
* @method $this unless(bool|\Closure $condition) Conditionally hide the notification * @method $this unless(bool|\Closure $condition) Conditionally hide the notification
* @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification * @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification
* @method Envelope getEnvelope() Get the notification envelope * @method Envelope getEnvelope() Get the notification envelope
* @method Envelope success(string $message, array<string, mixed> $options = [], ?string $title = null) Create a success notification * @method Envelope success(string $message, array<string, mixed> $options = [], ?string $title = null) Create a success notification
* @method Envelope error(string $message, array<string, mixed> $options = [], ?string $title = null) Create an error notification * @method Envelope error(string $message, array<string, mixed> $options = [], ?string $title = null) Create an error notification
* @method Envelope info(string $message, array<string, mixed> $options = [], ?string $title = null) Create an info notification * @method Envelope info(string $message, array<string, mixed> $options = [], ?string $title = null) Create an info notification
* @method Envelope warning(string $message, array<string, mixed> $options = [], ?string $title = null) Create a warning notification * @method Envelope warning(string $message, array<string, mixed> $options = [], ?string $title = null) Create a warning notification
* @method Envelope flash(?string $type = null, ?string $message = null, array<string, mixed> $options = [], ?string $title = null) Create a flash notification * @method Envelope flash(?string $type = null, ?string $message = null, array<string, mixed> $options = [], ?string $title = null) Create a flash notification
* @method Envelope push() Push the notification to storage * @method Envelope push() Push the notification to storage
* @method Envelope created(string|object|null $resource = null) Create a "resource created" notification * @method Envelope created(string|object|null $resource = null) Create a "resource created" notification
* @method Envelope updated(string|object|null $resource = null) Create a "resource updated" notification * @method Envelope updated(string|object|null $resource = null) Create a "resource updated" notification
* @method Envelope saved(string|object|null $resource = null) Create a "resource saved" notification * @method Envelope saved(string|object|null $resource = null) Create a "resource saved" notification
* @method Envelope deleted(string|object|null $resource = null) Create a "resource deleted" notification * @method Envelope deleted(string|object|null $resource = null) Create a "resource deleted" notification
*/ */
interface FlasherInterface interface FlasherInterface
{ {
+1 -6
View File
@@ -82,12 +82,7 @@ final class ResponseManager implements ResponseManagerInterface
$presenter = $presenter(); $presenter = $presenter();
if (!$presenter instanceof PresenterInterface) { if (!$presenter instanceof PresenterInterface) {
throw new \InvalidArgumentException(\sprintf( throw new \InvalidArgumentException(\sprintf('Presenter callable for "%s" must return an instance of %s, %s returned.', $alias, PresenterInterface::class, get_debug_type($presenter)));
'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 * Tagged with kernel.reset in services.php to be automatically called
* by Symfony's kernel between requests. * by Symfony's kernel between requests.
*/ */
final class WorkerListener implements ResetInterface final readonly class WorkerListener implements ResetInterface
{ {
public function __construct( public function __construct(
private readonly ContentSecurityPolicyHandlerInterface $cspHandler, private ContentSecurityPolicyHandlerInterface $cspHandler,
) { ) {
} }
@@ -89,7 +89,7 @@ final class FlasherContainerTest extends TestCase
public function testFromWithClosureReturningInvalidTypeThrowsException(): void public function testFromWithClosureReturningInvalidTypeThrowsException(): void
{ {
// Pass a closure that returns something other than ContainerInterface // 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->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected an instance of "Psr\Container\ContainerInterface"'); $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()); $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($type));
$this->assertTrue(Type::isValid('error'));
$this->assertTrue(Type::isValid('info'));
$this->assertTrue(Type::isValid('warning'));
} }
public function testIsValidWithInvalidTypes(): void /**
* @return iterable<string, array{string}>
*/
public static function validTypesProvider(): iterable
{ {
$this->assertFalse(Type::isValid('invalid')); yield 'success' => ['success'];
$this->assertFalse(Type::isValid('')); yield 'error' => ['error'];
$this->assertFalse(Type::isValid('SUCCESS')); yield 'info' => ['info'];
$this->assertFalse(Type::isValid('notice')); 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\Prime\Http\Csp\ContentSecurityPolicyHandlerInterface;
use Flasher\Symfony\EventListener\WorkerListener; use Flasher\Symfony\EventListener\WorkerListener;
use Flasher\Symfony\Storage\FallbackSession; use Flasher\Symfony\Storage\FallbackSession;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Service\ResetInterface; use Symfony\Contracts\Service\ResetInterface;
final class WorkerListenerTest extends TestCase final class WorkerListenerTest extends TestCase
{ {
private ContentSecurityPolicyHandlerInterface $cspHandler; /** @var MockObject&ContentSecurityPolicyHandlerInterface */
private MockObject $cspHandler;
protected function setUp(): void protected function setUp(): void
{ {