Skip to content

Commit ba80a4c

Browse files
authored
Merge pull request #28 from maartenpaauw/feat/custom-exception-code
Add support for custom exception code
2 parents b294e58 + 24202a7 commit ba80a4c

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `laravel-specification-pattern` will be documented in this file.
44

5+
## [v2.7.0] - 2024-11-20
6+
7+
### Added
8+
9+
- Support for custom exception code.
10+
511
## [v2.6.0] - 2024-09-20
612

713
### Added
@@ -76,6 +82,7 @@ All notable changes to `laravel-specification-pattern` will be documented in thi
7682

7783
- initial release
7884

85+
[v2.7.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.6.0...v2.7.0
7986
[v2.6.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.5.0...v2.6.0
8087
[v2.5.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.4.0...v2.5.0
8188
[v2.4.0]: https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.3.0...v2.4.0

src/CompositeSpecification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ final public function andNot(Specification $specification): self
8282
/**
8383
* @return CompositeSpecification<TCandidate>
8484
*/
85-
final public function verbose(string $message = ''): self
85+
final public function verbose(string $message = '', int $code = 0): self
8686
{
87-
return new VerboseSpecification($this, $message);
87+
return new VerboseSpecification($this, $message, $code);
8888
}
8989
}

src/VerboseSpecification.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class VerboseSpecification extends CompositeSpecification
1919
public function __construct(
2020
private readonly Specification $origin,
2121
private readonly string $message = '',
22+
private readonly int $code = 0,
2223
) {}
2324

2425
/**
@@ -29,6 +30,19 @@ public function withMessage(string $message): self
2930
return new self(
3031
$this->origin,
3132
$message,
33+
$this->code,
34+
);
35+
}
36+
37+
/**
38+
* @return VerboseSpecification<TCandidate>
39+
*/
40+
public function withCode(int $code): self
41+
{
42+
return new self(
43+
$this->origin,
44+
$this->message,
45+
$code,
3246
);
3347
}
3448

@@ -37,6 +51,11 @@ public function message(): string
3751
return $this->message;
3852
}
3953

54+
public function code(): int
55+
{
56+
return $this->code;
57+
}
58+
4059
/**
4160
* @param TCandidate $candidate
4261
*
@@ -49,6 +68,6 @@ public function isSatisfiedBy(mixed $candidate): bool
4968
return true;
5069
}
5170

52-
throw new DissatisfiedSpecification($this->message);
71+
throw new DissatisfiedSpecification($this->message, $this->code);
5372
}
5473
}

tests/CompositeSpecificationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ public function test_it_should_make_the_specification_verbose(): void
132132
{
133133
// Act
134134
$specification = $this->specification
135-
->verbose('The specification is not satisfied');
135+
->verbose('The specification is not satisfied', 10);
136136

137137
// Assert
138138
$this->assertInstanceOf(VerboseSpecification::class, $specification);
139139
$this->assertSame('The specification is not satisfied', $specification->message());
140+
$this->assertSame(10, $specification->code());
140141
}
141142
}

tests/VerboseSpecificationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function test_it_should_throw_an_exception_with_an_empty_message_when_the
3434
// Assert
3535
$this->expectException(DissatisfiedSpecification::class);
3636
$this->expectExceptionMessage('');
37+
$this->expectExceptionCode(0);
3738

3839
// Act
3940
$this->specification->isSatisfiedBy('Hello Laravel!');
@@ -51,6 +52,18 @@ public function test_it_should_be_possible_to_customize_the_exception_message():
5152
->isSatisfiedBy('Hello Laravel!');
5253
}
5354

55+
public function test_it_should_be_possible_to_customize_the_exception_code(): void
56+
{
57+
// Assert
58+
$this->expectException(DissatisfiedSpecification::class);
59+
$this->expectExceptionCode(10);
60+
61+
// Act
62+
$this->specification
63+
->withCode(10)
64+
->isSatisfiedBy('Hello Laravel!');
65+
}
66+
5467
public function test_it_should_return_true_when_the_specification_is_satisfied_with_the_given_candidate(): void
5568
{
5669
// Act
@@ -71,4 +84,16 @@ public function test_it_should_be_possible_to_receive_the_message(): void
7184
// Assert
7285
$this->assertSame('This is the reason why it is dissatisfied.', $message);
7386
}
87+
88+
public function test_it_should_be_possible_to_receive_the_code(): void
89+
{
90+
// Arrange
91+
$specification = $this->specification->withCode(10);
92+
93+
// Act
94+
$code = $specification->code();
95+
96+
// Assert
97+
$this->assertSame(10, $code);
98+
}
7499
}

0 commit comments

Comments
 (0)