respect-validation/tests/unit/Rules/IpTest.php

146 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
2017-11-04 11:21:40 +01:00
use PHPUnit\Framework\TestCase;
/**
* @group rule
2017-02-04 14:01:14 +01:00
* @covers \Respect\Validation\Rules\Ip
* @covers \Respect\Validation\Exceptions\IpException
*/
2017-11-04 11:21:40 +01:00
class IpTest extends TestCase
{
/**
* @dataProvider providerForIp
*/
2015-06-08 16:47:14 +02:00
public function testValidIpsShouldReturnTrue($input, $options = null)
{
2013-01-09 15:48:11 +01:00
$ipValidator = new Ip($options);
self::assertTrue($ipValidator->__invoke($input));
self::assertTrue($ipValidator->assert($input));
self::assertTrue($ipValidator->check($input));
2013-01-09 15:48:11 +01:00
}
/**
* @dataProvider providerForIpBetweenRange
*/
public function testIpsBetweenRangeShouldReturnTrue($input, $networkRange)
2013-01-09 15:48:11 +01:00
{
$ipValidator = new Ip($networkRange);
self::assertTrue($ipValidator->__invoke($input));
self::assertTrue($ipValidator->assert($input));
self::assertTrue($ipValidator->check($input));
}
/**
* @dataProvider providerForNotIp
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\IpException
*/
2015-06-08 16:47:14 +02:00
public function testInvalidIpsShouldThrowIpException($input, $options = null)
{
2013-01-09 15:48:11 +01:00
$ipValidator = new Ip($options);
self::assertFalse($ipValidator->__invoke($input));
self::assertFalse($ipValidator->assert($input));
2013-01-09 15:48:11 +01:00
}
/**
* @dataProvider providerForIpOutsideRange
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\IpException
2013-01-09 15:48:11 +01:00
*/
public function testIpsOutsideRangeShouldReturnFalse($input, $networkRange)
2013-01-09 15:48:11 +01:00
{
$ipValidator = new Ip($networkRange);
self::assertFalse($ipValidator->__invoke($input));
self::assertFalse($ipValidator->assert($input));
}
public function providerForIp()
{
2015-10-18 03:44:47 +02:00
return [
['127.0.0.1'],
];
}
2013-01-09 15:48:11 +01:00
public function providerForIpBetweenRange()
{
2015-10-18 03:44:47 +02:00
return [
['127.0.0.1', '127.*'],
['127.0.0.1', '127.0.*'],
['127.0.0.1', '127.0.0.*'],
['192.168.2.6', '192.168.*.6'],
['192.168.2.6', '192.*.2.6'],
['10.168.2.6', '*.168.2.6'],
['192.168.2.6', '192.168.*.*'],
['192.10.2.6', '192.*.*.*'],
['192.168.255.156', '*'],
['192.168.255.156', '*.*.*.*'],
['127.0.0.1', '127.0.0.0-127.0.0.255'],
['192.168.2.6', '192.168.0.0-192.168.255.255'],
['192.10.2.6', '192.0.0.0-192.255.255.255'],
['192.168.255.156', '0.0.0.0-255.255.255.255'],
['220.78.173.2', '220.78.168/21'],
['220.78.173.2', '220.78.168.0/21'],
['220.78.173.2', '220.78.168.0/255.255.248.0'],
];
2013-01-09 15:48:11 +01:00
}
public function providerForNotIp()
{
2015-10-18 03:44:47 +02:00
return [
[''],
[null],
['j'],
[' '],
['Foo'],
['192.168.0.1', FILTER_FLAG_NO_PRIV_RANGE],
];
}
2013-01-09 15:48:11 +01:00
public function providerForIpOutsideRange()
{
2015-10-18 03:44:47 +02:00
return [
['127.0.0.1', '127.0.1.*'],
['192.168.2.6', '192.163.*.*'],
['192.10.2.6', '193.*.*.*'],
['127.0.0.1', '127.0.1.0-127.0.1.255'],
['192.168.2.6', '192.163.0.0-192.163.255.255'],
['192.10.2.6', '193.168.0.0-193.255.255.255'],
['220.78.176.1', '220.78.168/21'],
['220.78.176.2', '220.78.168.0/21'],
['220.78.176.3', '220.78.168.0/255.255.248.0'],
];
2013-01-09 15:48:11 +01:00
}
/**
* @dataProvider providerForInvalidRanges
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\ComponentException
2013-01-09 15:48:11 +01:00
*/
public function testInvalidRangeShouldRaiseException($range)
2013-01-09 15:48:11 +01:00
{
$o = new Ip($range);
}
public function providerForInvalidRanges()
{
2015-10-18 03:44:47 +02:00
return [
['192.168'],
['asd'],
['192.168.0.0-192.168.0.256'],
['192.168.0.0-192.168.0.1/4'],
['192.168.0/1'],
['192.168.2.0/256.256.256.256'],
['192.168.2.0/8.256.256.256'],
];
2013-01-09 15:48:11 +01:00
}
}