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

163 lines
4.5 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.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
2018-12-05 08:36:38 +01:00
use Respect\Validation\Test\TestCase;
2017-11-04 11:21:40 +01:00
/**
* @group rule
2017-02-04 14:01:14 +01:00
* @covers \Respect\Validation\Exceptions\IpException
* @covers \Respect\Validation\Rules\Ip
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
*/
2017-11-04 11:21:40 +01:00
class IpTest extends TestCase
{
/**
* @dataProvider providerForIp
*
* @test
*/
public function validIpsShouldReturnTrue($input, $options = null): void
{
2013-01-09 15:48:11 +01:00
$ipValidator = new Ip($options);
self::assertTrue($ipValidator->__invoke($input));
$ipValidator->assert($input);
$ipValidator->check($input);
2013-01-09 15:48:11 +01:00
}
/**
* @dataProvider providerForIpBetweenRange
*
* @test
2013-01-09 15:48:11 +01:00
*/
public function ipsBetweenRangeShouldReturnTrue($input, $networkRange): void
2013-01-09 15:48:11 +01:00
{
$ipValidator = new Ip($networkRange);
self::assertTrue($ipValidator->__invoke($input));
$ipValidator->assert($input);
$ipValidator->check($input);
}
/**
* @dataProvider providerForNotIp
2017-02-04 14:01:14 +01:00
* @expectedException \Respect\Validation\Exceptions\IpException
*
* @test
*/
public function invalidIpsShouldThrowIpException($input, $options = null): void
{
2013-01-09 15:48:11 +01:00
$ipValidator = new Ip($options);
self::assertFalse($ipValidator->__invoke($input));
$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
*
* @test
2013-01-09 15:48:11 +01:00
*/
public function ipsOutsideRangeShouldReturnFalse($input, $networkRange): void
2013-01-09 15:48:11 +01:00
{
$ipValidator = new Ip($networkRange);
self::assertFalse($ipValidator->__invoke($input));
$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
*
* @test
2013-01-09 15:48:11 +01:00
*/
public function invalidRangeShouldRaiseException($range): void
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
}
}