Skip to content

Commit

Permalink
Add ip, ipv4 and ipv6 assertions (webmozarts#88)
Browse files Browse the repository at this point in the history
* Add ip, ipv4 and ipv6 assertions

Fixes webmozarts#44

* Add docs for ip, ipv4, ipv6 assertions
  • Loading branch information
JustBlackBird authored and Nyholm committed Dec 24, 2018
1 parent f6d8ee7 commit 827c644
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ Method | Description
`maxLength($value, $max, $message = '')` | Check that a string has at most a certain number of characters
`lengthBetween($value, $min, $max, $message = '')` | Check that a string has a length in the given range
`uuid($value, $message = '')` | Check that a string is a valid UUID
`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
`notWhitespaceOnly($value, $message = '')` | Check that a string contains at least one non-whitespace character

### File Assertions
Expand Down
36 changes: 36 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
* @method static void nullOrNotEmpty($value, $message = '')
* @method static void nullOrTrue($value, $message = '')
* @method static void nullOrFalse($value, $message = '')
* @method static void nullOrIp($value, $message = '')
* @method static void nullOrIpv4($value, $message = '')
* @method static void nullOrIpv6($value, $message = '')
* @method static void nullOrEq($value, $value2, $message = '')
* @method static void nullOrNotEq($value,$value2, $message = '')
* @method static void nullOrSame($value, $value2, $message = '')
Expand Down Expand Up @@ -116,6 +119,9 @@
* @method static void allNotEmpty($values, $message = '')
* @method static void allTrue($values, $message = '')
* @method static void allFalse($values, $message = '')
* @method static void allIp($values, $message = '')
* @method static void allIpv4($values, $message = '')
* @method static void allIpv6($values, $message = '')
* @method static void allEq($values, $value2, $message = '')
* @method static void allNotEq($values,$value2, $message = '')
* @method static void allSame($values, $value2, $message = '')
Expand Down Expand Up @@ -448,6 +454,36 @@ public static function false($value, $message = '')
}
}

public static function ip($value, $message = '')
{
if (false === filter_var($value, FILTER_VALIDATE_IP)) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected a value to be an IP. Got: %s',
static::valueToString($value)
));
}
}

public static function ipv4($value, $message = '')
{
if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected a value to be an IPv4. Got: %s',
static::valueToString($value)
));
}
}

public static function ipv6($value, $message = '')
{
if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected a value to be an IPv6. Got %s',
static::valueToString($value)
));
}
}

public static function eq($value, $value2, $message = '')
{
if ($value2 != $value) {
Expand Down
36 changes: 36 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,42 @@ public function getTests()
array('throws', array(function() { trigger_error('test'); }, 'Throwable'), true, false, 70000),
array('throws', array(function() { trigger_error('test'); }, 'Unthrowable'), false, false, 70000),
array('throws', array(function() { throw new Error(); }, 'Throwable'), true, true, 70000),
array('ip', array('192.168.0.1'), true),
array('ip', array('255.255.255.255'), true),
array('ip', array('0.0.0.0'), true),
array('ip', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), true),
array('ip', array('::ffff:192.0.2.1'), true),
array('ip', array('::1'), true),
array('ip', array('::'), true),
array('ip', array('foo'), false),
array('ip', array(123), false),
array('ip', array(array()), false),
array('ip', array(null), false),
array('ip', array(false), false),
array('ipv4', array('192.168.0.1'), true),
array('ipv4', array('255.255.255.255'), true),
array('ipv4', array('0.0.0.0'), true),
array('ipv4', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), false),
array('ipv4', array('::ffff:192.0.2.1'), false),
array('ipv4', array('::1'), false),
array('ipv4', array('::'), false),
array('ipv4', array('foo'), false),
array('ipv4', array(123), false),
array('ipv4', array(array()), false),
array('ipv4', array(null), false),
array('ipv4', array(false), false),
array('ipv6', array('192.168.0.1'), false),
array('ipv6', array('255.255.255.255'), false),
array('ipv6', array('0.0.0.0'), false),
array('ipv6', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), true),
array('ipv6', array('::ffff:192.0.2.1'), true),
array('ipv6', array('::1'), true),
array('ipv6', array('::'), true),
array('ipv6', array('foo'), false),
array('ipv6', array(123), false),
array('ipv6', array(array()), false),
array('ipv6', array(null), false),
array('ipv6', array(false), false),
);
}

Expand Down

0 comments on commit 827c644

Please sign in to comment.