File tree Expand file tree Collapse file tree 5 files changed +56
-4
lines changed Expand file tree Collapse file tree 5 files changed +56
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
All notable changes to ` laravel-specification-pattern ` will be documented in this file.
4
4
5
+ ## [ v2.7.0] - 2024-11-20
6
+
7
+ ### Added
8
+
9
+ - Support for custom exception code.
10
+
5
11
## [ v2.6.0] - 2024-09-20
6
12
7
13
### Added
@@ -76,6 +82,7 @@ All notable changes to `laravel-specification-pattern` will be documented in thi
76
82
77
83
- initial release
78
84
85
+ [ v2.7.0 ] : https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.6.0...v2.7.0
79
86
[ v2.6.0 ] : https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.5.0...v2.6.0
80
87
[ v2.5.0 ] : https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.4.0...v2.5.0
81
88
[ v2.4.0 ] : https://github.com/maartenpaauw/laravel-specification-pattern/compare/v2.3.0...v2.4.0
Original file line number Diff line number Diff line change @@ -82,8 +82,8 @@ final public function andNot(Specification $specification): self
82
82
/**
83
83
* @return CompositeSpecification<TCandidate>
84
84
*/
85
- final public function verbose (string $ message = '' ): self
85
+ final public function verbose (string $ message = '' , int $ code = 0 ): self
86
86
{
87
- return new VerboseSpecification ($ this , $ message );
87
+ return new VerboseSpecification ($ this , $ message, $ code );
88
88
}
89
89
}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ final class VerboseSpecification extends CompositeSpecification
19
19
public function __construct (
20
20
private readonly Specification $ origin ,
21
21
private readonly string $ message = '' ,
22
+ private readonly int $ code = 0 ,
22
23
) {}
23
24
24
25
/**
@@ -29,6 +30,19 @@ public function withMessage(string $message): self
29
30
return new self (
30
31
$ this ->origin ,
31
32
$ 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 ,
32
46
);
33
47
}
34
48
@@ -37,6 +51,11 @@ public function message(): string
37
51
return $ this ->message ;
38
52
}
39
53
54
+ public function code (): int
55
+ {
56
+ return $ this ->code ;
57
+ }
58
+
40
59
/**
41
60
* @param TCandidate $candidate
42
61
*
@@ -49,6 +68,6 @@ public function isSatisfiedBy(mixed $candidate): bool
49
68
return true ;
50
69
}
51
70
52
- throw new DissatisfiedSpecification ($ this ->message );
71
+ throw new DissatisfiedSpecification ($ this ->message , $ this -> code );
53
72
}
54
73
}
Original file line number Diff line number Diff line change @@ -132,10 +132,11 @@ public function test_it_should_make_the_specification_verbose(): void
132
132
{
133
133
// Act
134
134
$ specification = $ this ->specification
135
- ->verbose ('The specification is not satisfied ' );
135
+ ->verbose ('The specification is not satisfied ' , 10 );
136
136
137
137
// Assert
138
138
$ this ->assertInstanceOf (VerboseSpecification::class, $ specification );
139
139
$ this ->assertSame ('The specification is not satisfied ' , $ specification ->message ());
140
+ $ this ->assertSame (10 , $ specification ->code ());
140
141
}
141
142
}
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ public function test_it_should_throw_an_exception_with_an_empty_message_when_the
34
34
// Assert
35
35
$ this ->expectException (DissatisfiedSpecification::class);
36
36
$ this ->expectExceptionMessage ('' );
37
+ $ this ->expectExceptionCode (0 );
37
38
38
39
// Act
39
40
$ this ->specification ->isSatisfiedBy ('Hello Laravel! ' );
@@ -51,6 +52,18 @@ public function test_it_should_be_possible_to_customize_the_exception_message():
51
52
->isSatisfiedBy ('Hello Laravel! ' );
52
53
}
53
54
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
+
54
67
public function test_it_should_return_true_when_the_specification_is_satisfied_with_the_given_candidate (): void
55
68
{
56
69
// Act
@@ -71,4 +84,16 @@ public function test_it_should_be_possible_to_receive_the_message(): void
71
84
// Assert
72
85
$ this ->assertSame ('This is the reason why it is dissatisfied. ' , $ message );
73
86
}
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
+ }
74
99
}
You can’t perform that action at this time.
0 commit comments