From d4abef58eb8938bcbc92fb1cdcf48fe730d800ef Mon Sep 17 00:00:00 2001 From: Younes ENNAJI Date: Mon, 2 Mar 2026 04:06:51 +0000 Subject: [PATCH] Standardize exception message format and add exception tests --- CHANGELOG.md | 2 + .../CriteriaNotRegisteredException.php | 2 +- .../Exception/PresetNotFoundException.php | 2 +- .../EventListener/PresetListenerTest.php | 2 +- .../CriteriaNotRegisteredExceptionTest.php | 39 +++++++++++++++++++ .../PresenterNotFoundExceptionTest.php | 39 +++++++++++++++++++ .../Exception/PresetNotFoundExceptionTest.php | 39 +++++++++++++++++++ 7 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 tests/Prime/Exception/CriteriaNotRegisteredExceptionTest.php create mode 100644 tests/Prime/Exception/PresenterNotFoundExceptionTest.php create mode 100644 tests/Prime/Exception/PresetNotFoundExceptionTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9a632e..3844358f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ * fix [Flasher] Add type validation for callable presenter return values in ResponseManager with descriptive error messages * fix [Flasher] Fix FlasherPlugin::normalizePlugins() losing scripts/styles when both top-level and plugin-level configs are provided - replaced array union operator with array_merge * fix [Flasher] Simplify FlasherPlugin::normalizeFlashBag() by replacing redundant array union with direct array_merge +* fix [Flasher] Standardize exception message format in PresetNotFoundException to use brackets like other exceptions +* fix [Flasher] Standardize exception message wording in CriteriaNotRegisteredException to use "not found" instead of "is not found" ## [v2.1.3](https://github.com/php-flasher/php-flasher/compare/v2.1.2...v2.1.3) - 2025-01-25 diff --git a/src/Prime/Exception/CriteriaNotRegisteredException.php b/src/Prime/Exception/CriteriaNotRegisteredException.php index ddbba3e7..2d826e8c 100644 --- a/src/Prime/Exception/CriteriaNotRegisteredException.php +++ b/src/Prime/Exception/CriteriaNotRegisteredException.php @@ -11,7 +11,7 @@ final class CriteriaNotRegisteredException extends \Exception */ public static function create(string $alias, array $availableCriteria = []): self { - $message = \sprintf('Criteria "%s" is not found, did you forget to register it?', $alias); + $message = \sprintf('Criteria "%s" not found, did you forget to register it?', $alias); if ([] !== $availableCriteria) { $message .= \sprintf(' Available criteria: [%s]', implode(', ', $availableCriteria)); diff --git a/src/Prime/Exception/PresetNotFoundException.php b/src/Prime/Exception/PresetNotFoundException.php index de87eed3..b4914ff8 100644 --- a/src/Prime/Exception/PresetNotFoundException.php +++ b/src/Prime/Exception/PresetNotFoundException.php @@ -14,7 +14,7 @@ final class PresetNotFoundException extends \Exception $message = \sprintf('Preset "%s" not found, did you forget to register it?', $preset); if ([] !== $availablePresets) { - $message .= \sprintf(' Available presets: "%s"', implode('", "', $availablePresets)); + $message .= \sprintf(' Available presets: [%s]', implode(', ', $availablePresets)); } return new self($message); diff --git a/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php b/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php index 40dc2dc5..3a0ffc9a 100644 --- a/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php +++ b/tests/Prime/EventDispatcher/EventListener/PresetListenerTest.php @@ -54,7 +54,7 @@ final class PresetListenerTest extends TestCase PresetNotFoundException::class ); $this->expectExceptionMessage( - 'Preset "entity_deleted" not found, did you forget to register it? Available presets: "entity_saved"' + 'Preset "entity_deleted" not found, did you forget to register it? Available presets: [entity_saved]' ); $eventDispatcher = new EventDispatcher(); diff --git a/tests/Prime/Exception/CriteriaNotRegisteredExceptionTest.php b/tests/Prime/Exception/CriteriaNotRegisteredExceptionTest.php new file mode 100644 index 00000000..5128ee73 --- /dev/null +++ b/tests/Prime/Exception/CriteriaNotRegisteredExceptionTest.php @@ -0,0 +1,39 @@ +assertSame('Criteria "custom_criteria" not found, did you forget to register it?', $exception->getMessage()); + } + + public function testCreateWithAvailableCriteria(): void + { + $exception = CriteriaNotRegisteredException::create('custom_criteria', ['limit', 'order_by', 'filter']); + + $this->assertSame('Criteria "custom_criteria" not found, did you forget to register it? Available criteria: [limit, order_by, filter]', $exception->getMessage()); + } + + public function testCreateWithEmptyAvailableCriteria(): void + { + $exception = CriteriaNotRegisteredException::create('custom_criteria', []); + + $this->assertSame('Criteria "custom_criteria" not found, did you forget to register it?', $exception->getMessage()); + } + + public function testIsException(): void + { + $exception = CriteriaNotRegisteredException::create('test'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/tests/Prime/Exception/PresenterNotFoundExceptionTest.php b/tests/Prime/Exception/PresenterNotFoundExceptionTest.php new file mode 100644 index 00000000..b8cee23b --- /dev/null +++ b/tests/Prime/Exception/PresenterNotFoundExceptionTest.php @@ -0,0 +1,39 @@ +assertSame('Presenter "xml" not found, did you forget to register it?', $exception->getMessage()); + } + + public function testCreateWithAvailablePresenters(): void + { + $exception = PresenterNotFoundException::create('xml', ['html', 'json', 'array']); + + $this->assertSame('Presenter "xml" not found, did you forget to register it? Available presenters: [html, json, array]', $exception->getMessage()); + } + + public function testCreateWithEmptyAvailablePresenters(): void + { + $exception = PresenterNotFoundException::create('xml', []); + + $this->assertSame('Presenter "xml" not found, did you forget to register it?', $exception->getMessage()); + } + + public function testIsException(): void + { + $exception = PresenterNotFoundException::create('test'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/tests/Prime/Exception/PresetNotFoundExceptionTest.php b/tests/Prime/Exception/PresetNotFoundExceptionTest.php new file mode 100644 index 00000000..906af7b6 --- /dev/null +++ b/tests/Prime/Exception/PresetNotFoundExceptionTest.php @@ -0,0 +1,39 @@ +assertSame('Preset "custom_preset" not found, did you forget to register it?', $exception->getMessage()); + } + + public function testCreateWithAvailablePresets(): void + { + $exception = PresetNotFoundException::create('custom_preset', ['created', 'updated', 'deleted']); + + $this->assertSame('Preset "custom_preset" not found, did you forget to register it? Available presets: [created, updated, deleted]', $exception->getMessage()); + } + + public function testCreateWithEmptyAvailablePresets(): void + { + $exception = PresetNotFoundException::create('custom_preset', []); + + $this->assertSame('Preset "custom_preset" not found, did you forget to register it?', $exception->getMessage()); + } + + public function testIsException(): void + { + $exception = PresetNotFoundException::create('test'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +}