Remove identical checking from "Equals" rule

This commit is contained in:
Henrique Moody 2015-10-13 13:27:10 -03:00
parent 5a9f1fe508
commit 641b169c1b
6 changed files with 29 additions and 54 deletions

View file

@ -36,6 +36,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
- On exceptions, do not display parent message then rule has only one child (#407)
- On exceptions, improve `Object` conversion to string (#399)
- On exceptions, improve conversion of all values by using JSON (#399)
- Remove identical checking from "Equals" rule (#442)
- Rename rule "Arr" to "ArrayVal"
- Rename rule "Bool" to "BoolType" (#426)
- Rename rule "False" to "FalseVal" (#426)

View file

@ -1,24 +1,17 @@
# Equals
- `v::equals(mixed $value)`
- `v::equals(mixed $value, boolean $identical = false)`
Validates if the input is equal some value.
Validates if the input is equal to some value.
```php
v::equals('alganet')->validate('alganet'); //true
```
Identical validation (===) is possible:
```php
v::equals(10)->validate('10'); //true
v::equals(10, true)->validate('10'); //false
```
Message template for this validator includes `{{compareTo}}`.
***
See also:
* [Contains](Contains.md)
* [Identical](Identical.md)

View file

@ -13,22 +13,12 @@ namespace Respect\Validation\Exceptions;
class EqualsException extends ValidationException
{
const EQUALS = 0;
const IDENTICAL = 1;
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::EQUALS => '{{name}} must be equals {{compareTo}}',
self::IDENTICAL => '{{name}} must be identical as {{compareTo}}',
self::STANDARD => '{{name}} must be equals {{compareTo}}',
),
self::MODE_NEGATIVE => array(
self::EQUALS => '{{name}} must not be equals {{compareTo}}',
self::IDENTICAL => '{{name}} must not be identical as {{compareTo}}',
self::STANDARD => '{{name}} must not be equals {{compareTo}}',
),
);
public function chooseTemplate()
{
return $this->getParam('identical') ? static::IDENTICAL : static::EQUALS;
}
}

View file

@ -13,26 +13,15 @@ namespace Respect\Validation\Rules;
class Equals extends AbstractRule
{
public $compareIdentical = false;
public $compareTo = null;
public $compareTo;
public function __construct($compareTo, $compareIdentical = false)
public function __construct($compareTo)
{
$this->compareTo = $compareTo;
$this->compareIdentical = $compareIdentical;
}
public function reportError($input, array $extraParams = array())
{
return parent::reportError($input, $extraParams);
}
public function validate($input)
{
if ($this->compareIdentical) {
return $input === $this->compareTo;
}
return $input == $this->compareTo;
}
}

View file

@ -73,15 +73,6 @@ class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foo', $x->getTemplate());
}
/**
* @link https://github.com/Respect/Validation/pull/214
*/
public function testFixedConstEqualsException()
{
$this->assertTrue(EqualsException::EQUALS === 0);
$this->assertTrue(EqualsException::IDENTICAL === 1);
}
public function providerForStringify()
{
$object1 = new SplFileInfo('stringify.phpt'); // __toString()

View file

@ -11,6 +11,8 @@
namespace Respect\Validation\Rules;
use stdClass;
/**
* @group rule
* @covers Respect\Validation\Rules\Equals
@ -21,29 +23,39 @@ class EqualsTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForEquals
*/
public function testStringsContainingExpectedValueShouldPass($start, $input)
public function testInputEqualsToExpectedValueShouldPass($compareTo, $input)
{
$v = new Equals($start);
$this->assertTrue($v->__invoke($input));
$this->assertTrue($v->check($input));
$this->assertTrue($v->assert($input));
$rule = new Equals($compareTo);
$this->assertTrue($rule->validate($input));
}
/**
* @dataProvider providerForNotEquals
* @expectedException Respect\Validation\Exceptions\EqualsException
*/
public function testStringsNotEqualsExpectedValueShouldNotPass($start, $input, $identical = false)
public function testInputNotEqualsToExpectedValueShouldPass($compareTo, $input)
{
$v = new Equals($start, $identical);
$this->assertFalse($v->__invoke($input));
$this->assertFalse($v->assert($input));
$rule = new Equals($compareTo);
$this->assertFalse($rule->validate($input));
}
/**
* @expectedException Respect\Validation\Exceptions\EqualsException
* @expectedExceptionMessage "24" must be equals 42
*/
public function testShouldThrowTheProperExceptionWhenFailure()
{
$rule = new Equals(42);
$rule->check('24');
}
public function providerForEquals()
{
return array(
array('foo', 'foo'),
array(array(), array()),
array(new stdClass(), new stdClass()),
array(10, '10'),
);
}
@ -53,7 +65,6 @@ class EqualsTest extends \PHPUnit_Framework_TestCase
return array(
array('foo', ''),
array('foo', 'bar'),
array(10, '10', true),
);
}
}