respect-validation/tests/unit/Rules/LengthTest.php
2015-10-17 15:09:48 -03:00

118 lines
3.6 KiB
PHP

<?php
/*
* 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;
/**
* @group rule
* @covers Respect\Validation\Rules\Length
* @covers Respect\Validation\Exceptions\LengthException
*/
class LengthTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerForValidLengthInclusive
*/
public function testLengthInsideBoundsForInclusiveCasesReturnTrue($string, $min, $max)
{
$validator = new Length($min, $max, true);
$this->assertTrue($validator->validate($string));
}
/**
* @dataProvider providerForValidLengthNonInclusive
*/
public function testLengthInsideBoundsForNonInclusiveCasesShouldReturnTrue($string, $min, $max)
{
$validator = new Length($min, $max, false);
$this->assertTrue($validator->validate($string));
}
/**
* @dataProvider providerForInvalidLengthInclusive
*/
public function testLengthOutsideBoundsForInclusiveCasesReturnFalse($string, $min, $max)
{
$validator = new Length($min, $max, true);
$this->assertfalse($validator->validate($string));
}
/**
* @dataProvider providerForInvalidLengthNonInclusive
*/
public function testLengthOutsideBoundsForNonInclusiveCasesReturnFalse($string, $min, $max)
{
$validator = new Length($min, $max, false);
$this->assertfalse($validator->validate($string));
}
/**
* @dataProvider providerForComponentException
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testComponentExceptionsForInvalidParameters($min, $max)
{
$buggyValidator = new Length($min, $max);
}
public function providerForValidLengthInclusive()
{
return array(
array('alganet', 1, 15),
array('ççççç', 4, 6),
array(range(1, 20), 1, 30),
array((object) array('foo' => 'bar', 'bar' => 'baz'), 0, 2),
array('alganet', 1, null), //null is a valid max length, means "no maximum",
array('alganet', null, 15), //null is a valid min length, means "no minimum"
);
}
public function providerForValidLengthNonInclusive()
{
return array(
array('alganet', 1, 15),
array('ççççç', 4, 6),
array(range(1, 20), 1, 30),
array((object) array('foo' => 'bar', 'bar' => 'baz'), 1, 3),
array('alganet', 1, null), //null is a valid max length, means "no maximum",
array('alganet', null, 15), //null is a valid min length, means "no minimum"
);
}
public function providerForInvalidLengthInclusive()
{
return array(
array('', 1, 15),
array('alganet', 1, 6),
array(range(1, 20), 1, 19),
array('alganet', 8, null), //null is a valid max length, means "no maximum",
array('alganet', null, 6), //null is a valid min length, means "no minimum"
);
}
public function providerForInvalidLengthNonInclusive()
{
return array(
array('alganet', 1, 7),
array((object) array('foo' => 'bar', 'bar' => 'baz'), 3, 5),
array(range(1, 50), 1, 30),
);
}
public function providerForComponentException()
{
return array(
array('a', 15),
array(1, 'abc d'),
array(10, 1),
);
}
}