Skip to content

Commit

Permalink
Fixes #244, #154, #196 and #173
Browse files Browse the repository at this point in the history
  - Moves code from  __invoke on AbstractRule into NotEmpty.
  - Moves foreign type check to the chain.
  - Fixes tests using the chain instead of concrete instances.
  - Adds a parameter for juggling types on NotEmpty. (no BC break)
  - Adds new tests for the BC behavior and new parameter.
  • Loading branch information
Alexandre Gaigalas authored and henriquemoody committed Feb 13, 2015
1 parent 2883ea0 commit 558fd61
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
3 changes: 1 addition & 2 deletions library/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function __construct()

public function __invoke($input)
{
return !is_a($this, __NAMESPACE__.'\\NotEmpty')
&& $input === '' || $this->validate($input);
return '' === $input || $this->validate($input);
}

public function addOr()
Expand Down
25 changes: 22 additions & 3 deletions library/Rules/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@

class NotEmpty extends AbstractRule
{
/** Wheter to juggle PHP types **/
public $jugglesTypes = true;

public function __construct($jugglesTypes = true)
{
$this->jugglesTypes = $jugglesTypes;
}

public function validate($input)
{
if (is_string($input)) {
$input = trim($input);
if (!is_string($input)) {
return !empty($input);
}

$input = trim($input);

if ($this->jugglesTypes) {
return !empty($input);
}

return !empty($input);
return '' !== $input;
}

public function __invoke($input)
{
return $this->validate($input);
}
}
8 changes: 8 additions & 0 deletions library/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Rules\NotEmpty;

/**
* @method static Validator allOf()
Expand Down Expand Up @@ -162,6 +163,13 @@ public static function __callStatic($ruleName, $arguments)
return $validator->__call($ruleName, $arguments);
}


public function __invoke($input)
{
return ( ! $this instanceof NotEmpty && $input === '' ) ||
$this->validate($input);
}

/**
* @param mixed $ruleSpec
* @param array $arguments
Expand Down
46 changes: 46 additions & 0 deletions tests/Rules/NotEmptyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public function testStringNotEmpty($input)
$this->assertTrue($this->object->assert($input));
}

/**
* @dataProvider providerForNotEmptyNoJuggle
*/
public function testStringNotEmptyNoJuggle($input)
{
$this->object = new NotEmpty(false);
$this->assertTrue($this->object->assert($input));
}

/**
* @dataProvider providerForEmpty
* @expectedException Respect\Validation\Exceptions\NotEmptyException
Expand All @@ -27,6 +36,29 @@ public function testStringEmpty($input)
$this->assertFalse($this->object->assert($input));
}

/**
* @dataProvider providerForEmptyNoJuggle
* @expectedException Respect\Validation\Exceptions\NotEmptyException
*/
public function testStringEmptyNoJuggle($input)
{
$this->object = new NotEmpty(false);
$this->assertFalse($this->object->assert($input));
}

public function providerForNotEmptyNoJuggle()
{
return array(
array(1),
array('0'),
array(' 0 '),
array(' oi'),
array(array(5)),
array(array(0)),
array(new \stdClass)
);
}

public function providerForNotEmpty()
{
return array(
Expand All @@ -39,6 +71,20 @@ public function providerForNotEmpty()
}

public function providerForEmpty()
{
return array(
array(''),
array('0'),
array(' 0 '),
array(' '),
array("\n"),
array(false),
array(null),
array(array())
);
}

public function providerForEmptyNoJuggle()
{
return array(
array(''),
Expand Down
14 changes: 9 additions & 5 deletions tests/Rules/SfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public function testValidationWithAnExistingValidationConstraint()
$constraintName = 'Time';
$validConstraintValue = '04:20:00';
$invalidConstraintValue = 'yada';
$rule = new Sf($constraintName);
$this->assertTrue(
v::sf($constraintName)->validate($validConstraintValue),
$rule->validate($validConstraintValue),
sprintf('"%s" should be valid under "%s" constraint.', $validConstraintValue, $constraintName)
);
$this->assertFalse(
v::sf($constraintName)->validate($invalidConstraintValue),
$rule->validate($invalidConstraintValue),
sprintf('"%s" should be invalid under "%s" constraint.', $invalidConstraintValue, $constraintName)
);
}
Expand All @@ -28,8 +29,9 @@ public function testAssertionWithAnExistingValidationConstraint()
{
$constraintName = 'Time';
$validConstraintValue = '04:20:00';
$rule = new Sf($constraintName);
$this->assertTrue(
v::sf($constraintName)->assert($validConstraintValue),
$rule->assert($validConstraintValue),
sprintf('"%s" should be valid under "%s" constraint.', $validConstraintValue, $constraintName)
);
}
Expand All @@ -41,8 +43,9 @@ public function testAssertionMessageWithAnExistingValidationConstraint()
{
$constraintName = 'Time';
$invalidConstraintValue = '34:90:70';
$rule = new AllOf(new Sf($constraintName));
try {
v::sf($constraintName)->assert($invalidConstraintValue);
$rule->assert($invalidConstraintValue);
} catch (\Respect\Validation\Exceptions\AllOfException $exception) {
$fullValidationMessage = $exception->getFullMessage();
$expectedValidationException = <<<EOF
Expand All @@ -65,7 +68,8 @@ public function testAssertionMessageWithAnExistingValidationConstraint()
public function testValidationWithNonExistingConstraint()
{
$fantasyConstraintName = 'FluxCapacitor';
$rule = new Sf($fantasyConstraintName);
$fantasyValue = '8GW';
v::sf($fantasyConstraintName)->validate($fantasyValue);
$rule->validate($fantasyValue);
}
}

0 comments on commit 558fd61

Please sign in to comment.