Skip to content

Commit

Permalink
Add email assertion (webmozarts#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco authored and BackEndTea committed Aug 7, 2019
1 parent 14b3911 commit 0317274
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Method | Description
`ip($value, $message = '')` | Check that a string is a valid IP (either IPv4 or IPv6)
`ipv4($value, $message = '')` | Check that a string is a valid IPv4
`ipv6($value, $message = '')` | Check that a string is a valid IPv6
`email($value, $message = '')` | Check that a string is a valid e-mail address
`notWhitespaceOnly($value, $message = '')` | Check that a string contains at least one non-whitespace character

### File Assertions
Expand Down
12 changes: 12 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @method static void nullOrIp($value, $message = '')
* @method static void nullOrIpv4($value, $message = '')
* @method static void nullOrIpv6($value, $message = '')
* @method static void nullOrEmail($value, $message = '')
* @method static void nullOrUniqueValues($values, $message = '')
* @method static void nullOrEq($value, $expect, $message = '')
* @method static void nullOrNotEq($value, $expect, $message = '')
Expand Down Expand Up @@ -131,6 +132,7 @@
* @method static void allIp($values, $message = '')
* @method static void allIpv4($values, $message = '')
* @method static void allIpv6($values, $message = '')
* @method static void allEmail($values, $message = '')
* @method static void allUniqueValues($values, $message = '')
* @method static void allEq($values, $expect, $message = '')
* @method static void allNotEq($values, $expect, $message = '')
Expand Down Expand Up @@ -501,6 +503,16 @@ public static function ipv6($value, $message = '')
}
}

public static function email($value, $message = '')
{
if (false === filter_var($value, FILTER_VALIDATE_EMAIL)) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected a value to be a valid e-mail address. Got %s',
static::valueToString($value)
));
}
}

public static function uniqueValues(array $values, $message = '')
{
$allValues = count($values);
Expand Down
4 changes: 4 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ public function getTests()
array('ipv6', array(array()), false),
array('ipv6', array(null), false),
array('ipv6', array(false), false),
array('email', array('foo'), false),
array('email', array(123), false),
array('email', array('foo.com'), false),
array('email', array('[email protected]'), true),
array('uniqueValues', array(array('qwerty', 'qwerty')), false),
array('uniqueValues', array(array('asdfg', 'qwerty')), true),
array('uniqueValues', array(array(123, '123')), false),
Expand Down

0 comments on commit 0317274

Please sign in to comment.