respect-validation/tests/unit/Rules/WhenTest.php
Henrique Moody 748b280c34 Update conversion to strings on exceptions
Many changes were made on `ValidationException::stringify`:
- Add support for instances of `Exception`;
- Add support for instances of `Traversable`;
- Add support for resources;
- Improve `Array` conversion;
- Improve `Object` conversion;
- Improve conversion of all values by using JSON.

Now, all the parameters of the exception classes are just converted to
string when replacing parameters on exceptions, so the exception classes
now keep the original value of all parameters.
2015-09-04 17:11:40 -03:00

93 lines
2.4 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\When
* @covers Respect\Validation\Exceptions\WhenException
*/
class WhenTest extends \PHPUnit_Framework_TestCase
{
public function testWhenHappypath()
{
$v = new When(new Int(), new Between(1, 5), new NotEmpty());
$this->assertTrue($v->validate(3));
$this->assertTrue($v->validate('aaa'));
}
public function testWhenError()
{
$v = new When(new Int(), new Between(1, 5), new NotEmpty());
$this->assertFalse($v->validate(15));
}
public function testWhenWithoutElseHappypath()
{
$v = new When(new Int(), new Between(1, 5));
$this->assertTrue($v->validate(3));
}
public function testWhenWithoutElseError()
{
$v = new When(new String(), new Between(1, 5));
$this->assertFalse($v->validate(15));
}
/**
* @expectedException Respect\Validation\Exceptions\AlwaysInvalidException
* @expectedExceptionMessage 15 is not valid
*/
public function testWhenWithoutElseAssert()
{
$v = new When(new String(), new Between(1, 5));
$v->assert(15);
}
/**
* @expectedException Respect\Validation\Exceptions\BetweenException
*/
public function testWhenException()
{
$v = new When(new Int(), new Between(1, 5), new NotEmpty());
$this->assertFalse($v->assert(15));
}
/**
* @expectedException Respect\Validation\Exceptions\NotEmptyException
*/
public function testWhenException_on_else()
{
$v = new When(new Int(), new Between(1, 5), new NotEmpty());
$this->assertFalse($v->assert(''));
}
/**
* @expectedException Respect\Validation\Exceptions\MaxException
*/
public function testWhenException_failfast()
{
$v = new When(new Int(), new Between(1, 5), new NotEmpty());
$this->assertFalse($v->check(15));
}
/**
* @expectedException Respect\Validation\Exceptions\NotEmptyException
*/
public function testWhenException_on_else_failfast()
{
$v = new When(new Int(), new Between(1, 5), new NotEmpty());
$this->assertFalse($v->check(''));
}
}