From fb54341fb22781c18766fc1eeb5c244f03d7c272 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Mon, 19 Jan 2015 20:12:07 -0200 Subject: [PATCH] Fix wrong behavior when calling `not()` rule --- library/Validator.php | 4 ++-- .../Respect/Validation/ValidatorTest.php | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/library/Validator.php b/library/Validator.php index a7c8746e7..3e8b39983 100644 --- a/library/Validator.php +++ b/library/Validator.php @@ -146,8 +146,8 @@ public static function buildRule($ruleSpec, $arguments = array()) */ public function __call($method, $arguments) { - if ('not' === $method) { - return $arguments ? static::buildRule($method, $arguments) : new Rules\Not($this); + if ('not' === $method && empty($arguments)) { + return new static(new Rules\Not($this)); } return $this->addRule(static::buildRule($method, $arguments)); diff --git a/tests/library/Respect/Validation/ValidatorTest.php b/tests/library/Respect/Validation/ValidatorTest.php index 5bf14897a..c2b8166f3 100644 --- a/tests/library/Respect/Validation/ValidatorTest.php +++ b/tests/library/Respect/Validation/ValidatorTest.php @@ -90,5 +90,24 @@ public function testKeysAsValidatorNames() \-"" must be a valid date', $e->getFullMessage()); } } -} + /** + * Regression test #174. + */ + public function testShouldReturnANewValidatorInstanceWhenTheNotRuleIsCalledWithoutAnyArgument() + { + $validator = new Validator(); + + $this->assertInstanceOf('Respect\Validation\Validator', $validator->not()); + } + + /** + * Regression test #174. + */ + public function testShouldReturnValidatorInstanceWhenTheNotRuleIsCalledWithArguments() + { + $validator = new Validator(); + + $this->assertSame($validator, $validator->not($validator->notEmpty())); + } +}