diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d04a3d..c608a37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `laravel-specification-pattern` will be documented in this file. +## [v2.7.0] - 2024-11-20 + +### Added + +- Support for custom exception code. + ## [v2.6.0] - 2024-09-20 ### Added @@ -76,6 +82,7 @@ All notable changes to `laravel-specification-pattern` will be documented in thi - initial release +[v2.7.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.6.0...v2.7.0 [v2.6.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.5.0...v2.6.0 [v2.5.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.4.0...v2.5.0 [v2.4.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.3.0...v2.4.0 diff --git a/src/CompositeSpecification.php b/src/CompositeSpecification.php index 52c1b4f..4abb473 100644 --- a/src/CompositeSpecification.php +++ b/src/CompositeSpecification.php @@ -82,8 +82,8 @@ final public function andNot(Specification $specification): self /** * @return CompositeSpecification */ - final public function verbose(string $message = ''): self + final public function verbose(string $message = '', int $code = 0): self { - return new VerboseSpecification($this, $message); + return new VerboseSpecification($this, $message, $code); } } diff --git a/src/VerboseSpecification.php b/src/VerboseSpecification.php index 974fc62..22f0f0c 100644 --- a/src/VerboseSpecification.php +++ b/src/VerboseSpecification.php @@ -19,6 +19,7 @@ final class VerboseSpecification extends CompositeSpecification public function __construct( private readonly Specification $origin, private readonly string $message = '', + private readonly int $code = 0, ) {} /** @@ -29,6 +30,19 @@ public function withMessage(string $message): self return new self( $this->origin, $message, + $this->code, + ); + } + + /** + * @return VerboseSpecification + */ + public function withCode(int $code): self + { + return new self( + $this->origin, + $this->message, + $code, ); } @@ -37,6 +51,11 @@ public function message(): string return $this->message; } + public function code(): int + { + return $this->code; + } + /** * @param TCandidate $candidate * @@ -49,6 +68,6 @@ public function isSatisfiedBy(mixed $candidate): bool return true; } - throw new DissatisfiedSpecification($this->message); + throw new DissatisfiedSpecification($this->message, $this->code); } } diff --git a/tests/CompositeSpecificationTest.php b/tests/CompositeSpecificationTest.php index 39b9509..d22cda2 100644 --- a/tests/CompositeSpecificationTest.php +++ b/tests/CompositeSpecificationTest.php @@ -132,10 +132,11 @@ public function test_it_should_make_the_specification_verbose(): void { // Act $specification = $this->specification - ->verbose('The specification is not satisfied'); + ->verbose('The specification is not satisfied', 10); // Assert $this->assertInstanceOf(VerboseSpecification::class, $specification); $this->assertSame('The specification is not satisfied', $specification->message()); + $this->assertSame(10, $specification->code()); } } diff --git a/tests/VerboseSpecificationTest.php b/tests/VerboseSpecificationTest.php index a1024ad..1506fd0 100644 --- a/tests/VerboseSpecificationTest.php +++ b/tests/VerboseSpecificationTest.php @@ -34,6 +34,7 @@ public function test_it_should_throw_an_exception_with_an_empty_message_when_the // Assert $this->expectException(DissatisfiedSpecification::class); $this->expectExceptionMessage(''); + $this->expectExceptionCode(0); // Act $this->specification->isSatisfiedBy('Hello Laravel!'); @@ -51,6 +52,18 @@ public function test_it_should_be_possible_to_customize_the_exception_message(): ->isSatisfiedBy('Hello Laravel!'); } + public function test_it_should_be_possible_to_customize_the_exception_code(): void + { + // Assert + $this->expectException(DissatisfiedSpecification::class); + $this->expectExceptionCode(10); + + // Act + $this->specification + ->withCode(10) + ->isSatisfiedBy('Hello Laravel!'); + } + public function test_it_should_return_true_when_the_specification_is_satisfied_with_the_given_candidate(): void { // Act @@ -71,4 +84,16 @@ public function test_it_should_be_possible_to_receive_the_message(): void // Assert $this->assertSame('This is the reason why it is dissatisfied.', $message); } + + public function test_it_should_be_possible_to_receive_the_code(): void + { + // Arrange + $specification = $this->specification->withCode(10); + + // Act + $code = $specification->code(); + + // Assert + $this->assertSame(10, $code); + } }