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')
*
* @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 :
* ($id is 'flasher.noty' ? \Flasher\Noty\Prime\NotyInterface :
@@ -111,7 +111,7 @@ final class FlasherContainer
/**
* 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
*/
public static function getContainer(): ContainerInterface
@@ -13,32 +13,32 @@ use Flasher\Prime\Stamp\StampInterface;
*
* @mixin \Flasher\Prime\Notification\NotificationBuilderInterface
*
* @method $this title(string $title) Set the notification title
* @method $this message(string $message) Set the notification message
* @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 option(string $name, mixed $value) Set a single notification option
* @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 keep() Keep the notification for one more request
* @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 handler(string $handler) Set the notification handler/adapter
* @method $this context(array<string, mixed> $context) Set additional context data
* @method $this when(bool|\Closure $condition) Conditionally show the notification
* @method $this unless(bool|\Closure $condition) Conditionally hide the notification
* @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification
* @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 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 warning(string $message, array<string, mixed> $options = [], ?string $title = null) Create a warning notification
* @method $this title(string $title) Set the notification title
* @method $this message(string $message) Set the notification message
* @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 option(string $name, mixed $value) Set a single notification option
* @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 keep() Keep the notification for one more request
* @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 handler(string $handler) Set the notification handler/adapter
* @method $this context(array<string, mixed> $context) Set additional context data
* @method $this when(bool|\Closure $condition) Conditionally show the notification
* @method $this unless(bool|\Closure $condition) Conditionally hide the notification
* @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification
* @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 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 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 push() Push the notification to storage
* @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 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 push() Push the notification to storage
* @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 saved(string|object|null $resource = null) Create a "resource saved" notification
* @method Envelope deleted(string|object|null $resource = null) Create a "resource deleted" notification
*/
interface NotificationFactoryInterface
{
@@ -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)));
}
}
+25 -25
View File
@@ -16,32 +16,32 @@ use Flasher\Prime\Stamp\StampInterface;
*
* @phpstan-import-type ArrayPresenterType from ArrayPresenter
*
* @method $this title(string $title) Set the notification title
* @method $this message(string $message) Set the notification message
* @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 option(string $name, mixed $value) Set a single notification option
* @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 keep() Keep the notification for one more request
* @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 handler(string $handler) Set the notification handler/adapter
* @method $this context(array<string, mixed> $context) Set additional context data
* @method $this when(bool|\Closure $condition) Conditionally show the notification
* @method $this unless(bool|\Closure $condition) Conditionally hide the notification
* @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification
* @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 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 warning(string $message, array<string, mixed> $options = [], ?string $title = null) Create a warning notification
* @method $this title(string $title) Set the notification title
* @method $this message(string $message) Set the notification message
* @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 option(string $name, mixed $value) Set a single notification option
* @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 keep() Keep the notification for one more request
* @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 handler(string $handler) Set the notification handler/adapter
* @method $this context(array<string, mixed> $context) Set additional context data
* @method $this when(bool|\Closure $condition) Conditionally show the notification
* @method $this unless(bool|\Closure $condition) Conditionally hide the notification
* @method $this with(StampInterface[]|StampInterface $stamps) Add stamps to the notification
* @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 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 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 push() Push the notification to storage
* @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 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 push() Push the notification to storage
* @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 saved(string|object|null $resource = null) Create a "resource saved" notification
* @method Envelope deleted(string|object|null $resource = null) Create a "resource deleted" notification
*/
interface FlasherInterface
{
+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
{