File tree Expand file tree Collapse file tree
tests/FluentAssertions/Asserts/IsValidEmail Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -172,6 +172,9 @@ fact('')->isNotEmptyString(); // Fails
172172
173173fact('{"key": "value"}')->isJson(); // Passes
174174fact('invalid json')->isJson(); // Fails
175+
176+ fact('user@example.com')->isValidEmail(); // Passes
177+ fact('invalid-email')->isValidEmail(); // Fails
175178```
176179
177180### Type Checking assertions
Original file line number Diff line number Diff line change @@ -313,5 +313,25 @@ public function isJson(string $message = ''): self
313313 return $ this ;
314314 }
315315
316+ /**
317+ * Asserts that a string is a valid email address.
318+ *
319+ * This method checks if the actual string is a valid email format.
320+ *
321+ * Example usage:
322+ * fact('user@example.com')->isValidEmail(); // Passes
323+ * fact('invalid-email')->isValidEmail(); // Fails
324+ *
325+ * @param string $message Optional custom error message.
326+ *
327+ * @return self Enables fluent chaining of assertion methods.
328+ */
329+ public function isValidEmail (string $ message = '' ): self
330+ {
331+ Assert::assertTrue (filter_var ($ this ->variable , FILTER_VALIDATE_EMAIL ) !== false , $ message ?: 'String is not a valid email address. ' );
332+
333+ return $ this ;
334+ }
335+
316336 // endregion Length Methods
317337}
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types=1 );
2+
3+ namespace K2gl \PHPUnitFluentAssertions \Tests \FluentAssertions \Asserts \IsValidEmail ;
4+
5+ use K2gl \PHPUnitFluentAssertions \Tests \FluentAssertions \FluentAssertionsTestCase ;
6+ use K2gl \PHPUnitFluentAssertions \FluentAssertions ;
7+ use PHPUnit \Framework \Attributes \CoversMethod ;
8+ use PHPUnit \Framework \Attributes \DataProvider ;
9+
10+ use function K2gl \PHPUnitFluentAssertions \fact ;
11+
12+ #[CoversMethod(className: FluentAssertions::class, methodName: 'isValidEmail ' )]
13+ final class IsValidEmailTest extends FluentAssertionsTestCase
14+ {
15+ #[DataProvider('isValidEmailDataProvider ' )]
16+ public function testIsValidEmail (mixed $ variable ): void
17+ {
18+ // act
19+ fact ($ variable )->isValidEmail ();
20+
21+ // assert
22+ $ this ->correctAssertionExecuted ();
23+ }
24+
25+ #[DataProvider('notIsValidEmailDataProvider ' )]
26+ public function testNotIsValidEmail (mixed $ variable ): void
27+ {
28+ // assert
29+ $ this ->incorrectAssertionExpected ();
30+
31+ // act
32+ fact ($ variable )->isValidEmail ();
33+ }
34+
35+ public static function isValidEmailDataProvider (): array
36+ {
37+ return [
38+ ['user@example.com ' ],
39+ ['test.email+tag@domain.co.uk ' ],
40+ ];
41+ }
42+
43+ public static function notIsValidEmailDataProvider (): array
44+ {
45+ return [
46+ ['invalid-email ' ],
47+ ['@example.com ' ],
48+ ['user@ ' ],
49+ ['' ],
50+ ];
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments