44
55namespace Valbeat \Result \Tests ;
66
7+ use PHPUnit \Framework \Attributes \Test ;
78use PHPUnit \Framework \TestCase ;
89use Valbeat \Result \Err ;
910use Valbeat \Result \Ok ;
1011
1112class ErrTest extends TestCase
1213{
13- public function testIsOkReturnsFalse (): void
14+ #[Test]
15+ public function isOk_returns_false (): void
1416 {
1517 $ err = new Err ('error ' );
1618 $ this ->assertFalse ($ err ->isOk ());
1719 }
1820
19- public function testIsErrReturnsTrue (): void
21+ #[Test]
22+ public function isErr_returns_true (): void
2023 {
2124 $ err = new Err ('error ' );
2225 $ this ->assertTrue ($ err ->isErr ());
2326 }
2427
25- public function testIsOkAndAlwaysReturnsFalse (): void
28+ #[Test]
29+ public function isOkAnd_always_returns_false (): void
2630 {
2731 $ err = new Err ('error ' );
2832 $ result = $ err ->isOkAnd (fn () => true );
2933 $ this ->assertFalse ($ result );
3034 }
3135
32- public function testIsErrAndReturnsTrueWhenCallbackReturnsTrue (): void
36+ #[Test]
37+ public function isErrAnd_whenCallbackReturnsTrue_returns_true (): void
3338 {
3439 $ err = new Err ('critical ' );
3540 $ result = $ err ->isErrAnd (fn ($ error ) => $ error === 'critical ' );
3641 $ this ->assertTrue ($ result );
3742 }
3843
39- public function testIsErrAndReturnsFalseWhenCallbackReturnsFalse (): void
44+ #[Test]
45+ public function isErrAnd_whenCallbackReturnsFalse_returns_false (): void
4046 {
4147 $ err = new Err ('warning ' );
4248 $ result = $ err ->isErrAnd (fn ($ error ) => $ error === 'critical ' );
4349 $ this ->assertFalse ($ result );
4450 }
4551
46- public function testUnwrapThrowsException (): void
52+ #[Test]
53+ public function unwrap_throws_exception (): void
4754 {
4855 $ err = new Err ('error ' );
4956 $ this ->expectException (\LogicException::class);
5057 $ this ->expectExceptionMessage ('called Result::unwrap() on an Err value ' );
5158 $ err ->unwrap ();
5259 }
5360
54- public function testUnwrapErrReturnsErrorValue (): void
61+ #[Test]
62+ public function unwrapErr_returns_error_value (): void
5563 {
5664 $ err = new Err ('error message ' );
5765 $ this ->assertSame ('error message ' , $ err ->unwrapErr ());
5866 }
5967
60- public function testUnwrapErrWithIntValue (): void
68+ #[Test]
69+ public function unwrapErr_withIntValue_returns_int (): void
6170 {
6271 $ err = new Err (404 );
6372 $ this ->assertSame (404 , $ err ->unwrapErr ());
6473 }
6574
66- public function testUnwrapErrWithArrayValue (): void
75+ #[Test]
76+ public function unwrapErr_withArrayValue_returns_array (): void
6777 {
6878 $ error = ['code ' => 500 , 'message ' => 'Internal Server Error ' ];
6979 $ err = new Err ($ error );
7080 $ this ->assertSame ($ error , $ err ->unwrapErr ());
7181 }
7282
73- public function testUnwrapErrWithObjectValue (): void
83+ #[Test]
84+ public function unwrapErr_withObjectValue_returns_object (): void
7485 {
7586 $ error = new \Exception ('Test exception ' );
7687 $ err = new Err ($ error );
7788 $ this ->assertSame ($ error , $ err ->unwrapErr ());
7889 }
7990
80- public function testUnwrapOrReturnsDefault (): void
91+ #[Test]
92+ public function unwrapOr_returns_default (): void
8193 {
8294 $ err = new Err ('error ' );
8395 $ this ->assertSame (42 , $ err ->unwrapOr (42 ));
8496 }
8597
86- public function testUnwrapOrWithDifferentTypes (): void
98+ #[Test]
99+ public function unwrapOr_withDifferentTypes_returns_default (): void
87100 {
88101 $ err = new Err ('error ' );
89102 $ this ->assertSame ('default ' , $ err ->unwrapOr ('default ' ));
90103 $ this ->assertSame (['default ' ], $ err ->unwrapOr (['default ' ]));
91104 }
92105
93- public function testUnwrapOrElseCallsFunction (): void
106+ #[Test]
107+ public function unwrapOrElse_calls_function (): void
94108 {
95109 $ err = new Err ('error ' );
96110 $ result = $ err ->unwrapOrElse (fn ($ error ) => "Handled: $ error " );
97111 $ this ->assertSame ('Handled: error ' , $ result );
98112 }
99113
100- public function testUnwrapOrElseReceivesErrorValue (): void
114+ #[Test]
115+ public function unwrapOrElse_receives_error_value (): void
101116 {
102117 $ err = new Err (404 );
103118 $ result = $ err ->unwrapOrElse (fn ($ code ) => $ code === 404 ? 'Not Found ' : 'Unknown ' );
104119 $ this ->assertSame ('Not Found ' , $ result );
105120 }
106121
107- public function testMapDoesNotApplyFunction (): void
122+ #[Test]
123+ public function map_does_not_apply_function (): void
108124 {
109125 $ err = new Err ('error ' );
110126 $ mapped = $ err ->map (fn ($ x ) => $ x * 2 );
111127 $ this ->assertSame ($ err , $ mapped );
112128 $ this ->assertSame ('error ' , $ mapped ->unwrapErr ());
113129 }
114130
115- public function testMapErrAppliesFunctionToError (): void
131+ #[Test]
132+ public function mapErr_applies_function_to_error (): void
116133 {
117134 $ err = new Err ('error ' );
118135 $ mapped = $ err ->mapErr (fn ($ e ) => strtoupper ($ e ));
119136 $ this ->assertInstanceOf (Err::class, $ mapped );
120137 $ this ->assertSame ('ERROR ' , $ mapped ->unwrapErr ());
121138 }
122139
123- public function testMapErrWithTypeChange (): void
140+ #[Test]
141+ public function mapErr_withTypeChange_transforms_type (): void
124142 {
125143 $ err = new Err (404 );
126144 $ mapped = $ err ->mapErr (fn ($ code ) => "Error code: $ code " );
127145 $ this ->assertInstanceOf (Err::class, $ mapped );
128146 $ this ->assertSame ('Error code: 404 ' , $ mapped ->unwrapErr ());
129147 }
130148
131- public function testInspectDoesNotCallFunction (): void
149+ #[Test]
150+ public function inspect_does_not_call_function (): void
132151 {
133152 $ err = new Err ('error ' );
134153 $ called = false ;
@@ -139,7 +158,8 @@ public function testInspectDoesNotCallFunction(): void
139158 $ this ->assertSame ($ err , $ result );
140159 }
141160
142- public function testInspectErrCallsFunctionWithError (): void
161+ #[Test]
162+ public function inspectErr_calls_function_with_error (): void
143163 {
144164 $ err = new Err ('error ' );
145165 $ capturedError = null ;
@@ -150,21 +170,24 @@ public function testInspectErrCallsFunctionWithError(): void
150170 $ this ->assertSame ($ err , $ result );
151171 }
152172
153- public function testMapOrReturnsDefault (): void
173+ #[Test]
174+ public function mapOr_returns_default (): void
154175 {
155176 $ err = new Err ('error ' );
156177 $ result = $ err ->mapOr (100 , fn ($ x ) => $ x * 2 );
157178 $ this ->assertSame (100 , $ result );
158179 }
159180
160- public function testMapOrElseCallsDefaultFunction (): void
181+ #[Test]
182+ public function mapOrElse_calls_default_function (): void
161183 {
162184 $ err = new Err ('error ' );
163185 $ result = $ err ->mapOrElse (fn () => 100 , fn ($ x ) => $ x * 2 );
164186 $ this ->assertSame (100 , $ result );
165187 }
166188
167- public function testAndReturnsSelf (): void
189+ #[Test]
190+ public function and_returns_self (): void
168191 {
169192 $ err1 = new Err ('error1 ' );
170193 $ ok = new Ok (42 );
@@ -173,7 +196,8 @@ public function testAndReturnsSelf(): void
173196 $ this ->assertSame ('error1 ' , $ result ->unwrapErr ());
174197 }
175198
176- public function testAndWithAnotherErrReturnsSelf (): void
199+ #[Test]
200+ public function and_withAnotherErr_returns_self (): void
177201 {
178202 $ err1 = new Err ('error1 ' );
179203 $ err2 = new Err ('error2 ' );
@@ -182,15 +206,17 @@ public function testAndWithAnotherErrReturnsSelf(): void
182206 $ this ->assertSame ('error1 ' , $ result ->unwrapErr ());
183207 }
184208
185- public function testAndThenReturnsSelf (): void
209+ #[Test]
210+ public function andThen_returns_self (): void
186211 {
187212 $ err = new Err ('error ' );
188213 $ result = $ err ->andThen (fn ($ x ) => new Ok ($ x * 2 ));
189214 $ this ->assertSame ($ err , $ result );
190215 $ this ->assertSame ('error ' , $ result ->unwrapErr ());
191216 }
192217
193- public function testOrWithAnotherErrReturnsSecondErr (): void
218+ #[Test]
219+ public function or_withAnotherErr_returns_second_err (): void
194220 {
195221 $ err1 = new Err ('error1 ' );
196222 $ err2 = new Err ('error2 ' );
@@ -199,15 +225,17 @@ public function testOrWithAnotherErrReturnsSecondErr(): void
199225 $ this ->assertSame ('error2 ' , $ result ->unwrapErr ());
200226 }
201227
202- public function testOrElseCanReturnAnotherErr (): void
228+ #[Test]
229+ public function orElse_canReturnAnotherErr_returns_new_err (): void
203230 {
204231 $ err = new Err (404 );
205232 $ result = $ err ->orElse (fn ($ code ) => new Err ("HTTP Error: $ code " ));
206233 $ this ->assertInstanceOf (Err::class, $ result );
207234 $ this ->assertSame ('HTTP Error: 404 ' , $ result ->unwrapErr ());
208235 }
209236
210- public function testMatchCallsErrFunction (): void
237+ #[Test]
238+ public function match_calls_err_function (): void
211239 {
212240 $ err = new Err ('error ' );
213241 $ result = $ err ->match (
@@ -217,7 +245,8 @@ public function testMatchCallsErrFunction(): void
217245 $ this ->assertSame ('Error: error ' , $ result );
218246 }
219247
220- public function testMatchWithDifferentReturnTypes (): void
248+ #[Test]
249+ public function match_withDifferentReturnTypes_returns_err_branch (): void
221250 {
222251 $ err = new Err (404 );
223252 $ result = $ err ->match (
@@ -227,36 +256,41 @@ public function testMatchWithDifferentReturnTypes(): void
227256 $ this ->assertSame (['status ' => 'error ' , 'code ' => 404 ], $ result );
228257 }
229258
230- public function testErrWithNullValue (): void
259+ #[Test]
260+ public function err_withNullValue_handles_null (): void
231261 {
232262 $ err = new Err (null );
233263 $ this ->assertNull ($ err ->unwrapErr ());
234264 $ this ->assertFalse ($ err ->isOk ());
235265 $ this ->assertTrue ($ err ->isErr ());
236266 }
237267
238- public function testErrWithFalseValue (): void
268+ #[Test]
269+ public function err_withFalseValue_handles_false (): void
239270 {
240271 $ err = new Err (false );
241272 $ this ->assertFalse ($ err ->unwrapErr ());
242273 $ this ->assertTrue ($ err ->isErr ());
243274 }
244275
245- public function testErrWithZeroValue (): void
276+ #[Test]
277+ public function err_withZeroValue_handles_zero (): void
246278 {
247279 $ err = new Err (0 );
248280 $ this ->assertSame (0 , $ err ->unwrapErr ());
249281 $ this ->assertTrue ($ err ->isErr ());
250282 }
251283
252- public function testErrWithEmptyStringValue (): void
284+ #[Test]
285+ public function err_withEmptyString_handles_empty_string (): void
253286 {
254287 $ err = new Err ('' );
255288 $ this ->assertSame ('' , $ err ->unwrapErr ());
256289 $ this ->assertTrue ($ err ->isErr ());
257290 }
258291
259- public function testChainingOperations (): void
292+ #[Test]
293+ public function chainingOperations_applies_transformations (): void
260294 {
261295 $ err = new Err ('initial error ' );
262296 $ result = $ err
@@ -268,7 +302,8 @@ public function testChainingOperations(): void
268302 $ this ->assertSame ('Final: [INITIAL ERROR] ' , $ result ->unwrapErr ());
269303 }
270304
271- public function testExceptionAsErrorValue (): void
305+ #[Test]
306+ public function exceptionAsErrorValue_handles_exception (): void
272307 {
273308 $ exception = new \RuntimeException ('Something went wrong ' );
274309 $ err = new Err ($ exception );
@@ -279,4 +314,4 @@ public function testExceptionAsErrorValue(): void
279314 $ handled = $ err ->mapErr (fn ($ e ) => $ e ->getMessage ());
280315 $ this ->assertSame ('Something went wrong ' , $ handled ->unwrapErr ());
281316 }
282- }
317+ }
0 commit comments