Skip to content

Commit

Permalink
Merge f08f232 into 38d3fe0
Browse files Browse the repository at this point in the history
  • Loading branch information
vova07 committed Jan 27, 2018
2 parents 38d3fe0 + f08f232 commit a68ac63
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Chain.php
Expand Up @@ -450,7 +450,7 @@ public function validate(MessageStack $messageStack, Container $input, Container

$valid = $rule->isValid($this->key, $input) && $valid;

if ($rule->shouldBreakChain()) {
if (!$valid && $rule->shouldBreakChainOnError() || $rule->shouldBreakChain()) {
break;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Rule.php
Expand Up @@ -71,6 +71,16 @@ public function shouldBreakChain()
return false;
}

/**
* This indicates whether or not the rule should break the chain it's in on validation failure.
*
* @return bool
*/
public function shouldBreakChainOnError()
{
return false;
}

/**
* Registers the message stack to append errors to.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Boolean.php
Expand Up @@ -45,7 +45,7 @@ public function validate($value)
/**
* {@inheritdoc}
*/
public function shouldBreakChain()
public function shouldBreakChainOnError()
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Integer.php
Expand Up @@ -78,7 +78,7 @@ public function validate($value)
/**
* {@inheritdoc}
*/
public function shouldBreakChain()
public function shouldBreakChainOnError()
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/IsArray.php
Expand Up @@ -49,7 +49,7 @@ public function validate($value)
/**
* {@inheritdoc}
*/
public function shouldBreakChain()
public function shouldBreakChainOnError()
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/IsFloat.php
Expand Up @@ -49,7 +49,7 @@ public function validate($value)
/**
* {@inheritdoc}
*/
public function shouldBreakChain()
public function shouldBreakChainOnError()
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/IsString.php
Expand Up @@ -49,7 +49,7 @@ public function validate($value)
/**
* {@inheritdoc}
*/
public function shouldBreakChain()
public function shouldBreakChainOnError()
{
return true;
}
Expand Down
111 changes: 100 additions & 11 deletions tests/ChainTest.php
Expand Up @@ -3,10 +3,16 @@

use Particle\Validator\Rule;
use Particle\Validator\Rule\Boolean;
use Particle\Validator\Rule\Each;
use Particle\Validator\Rule\Email;
use Particle\Validator\Rule\GreaterThan;
use Particle\Validator\Rule\InArray;
use Particle\Validator\Rule\Integer;
use Particle\Validator\Rule\IsArray;
use Particle\Validator\Rule\IsFloat;
use Particle\Validator\Rule\IsString;
use Particle\Validator\Rule\LengthBetween;
use Particle\Validator\Rule\Required;
use Particle\Validator\Tests\Support\CustomRule;
use Particle\Validator\Validator;

Expand Down Expand Up @@ -41,19 +47,20 @@ public function testCanMountRulesOnChain()
}

/**
* @dataProvider providePrimitiveRulesData
* @dataProvider provideBreakChainData
*
* @param Rule $rule
* @param Rule $firstRule
* @param Rule $secondRule
* @param array $data
* @param array $expected
*/
public function testBreakChain($rule, $data, $expected)
public function testBreakChain($firstRule, $secondRule, $data, $expected)
{
$this
->validator
->required('foo')
->mount($rule)
->lengthBetween(1, 50);
->mount($firstRule)
->mount($secondRule);

$result = $this->validator->validate($data);

Expand All @@ -64,11 +71,12 @@ public function testBreakChain($rule, $data, $expected)
/**
* @return array
*/
public function providePrimitiveRulesData()
public function provideBreakChainData()
{
return [
[
'break boolean rule on error' => [
new Boolean(),
new InArray([true, false]),
[
'foo' => 'string',
],
Expand All @@ -78,8 +86,9 @@ public function providePrimitiveRulesData()
],
],
],
[
'break integer rule on error' => [
new Integer(),
new GreaterThan(10),
[
'foo' => 'string',
],
Expand All @@ -89,8 +98,12 @@ public function providePrimitiveRulesData()
],
],
],
[
'break isArray rule on error' => [
new IsArray(),
new Each(function ($v) {
/** @var Validator $v */
$v->required('bar')->email();
}),
[
'foo' => 'string',
],
Expand All @@ -100,8 +113,9 @@ public function providePrimitiveRulesData()
],
],
],
[
'break isFloat rule on error' => [
new IsFloat(),
new GreaterThan(20),
[
'foo' => 'string',
],
Expand All @@ -111,8 +125,9 @@ public function providePrimitiveRulesData()
],
],
],
[
'break isString rule on error' => [
new IsString(),
new LengthBetween(1, 3),
[
'foo' => ['array-value'],
],
Expand All @@ -122,6 +137,80 @@ public function providePrimitiveRulesData()
],
],
],
'break required rule' => [
new Boolean(),
new InArray([true, false]),
[],
[
'foo' => [
Required::NON_EXISTENT_KEY => 'foo must be provided, but does not exist',
],
],
],
'do not break boolean rule' => [
new Boolean(),
new InArray([false]),
[
'foo' => true,
],
[
'foo' => [
InArray::NOT_IN_ARRAY => 'foo must be in the defined set of values',
],
],
],
'do not break integer rule' => [
new Integer(),
new GreaterThan(10),
[
'foo' => 5,
],
[
'foo' => [
GreaterThan::NOT_GREATER_THAN => 'foo must be greater than 10',
],
],
],
'do not break isArray rule' => [
new IsArray(),
new Each(function (Validator $v) {
$v->required('bar')->email();
}),
[
'foo' => [
['bar' => 'invalid@email'],
],
],
[
'foo.0.bar' => [
Email::INVALID_FORMAT => 'bar must be a valid email address',
],
],
],
'do not break isFloat rule' => [
new IsFloat(),
new GreaterThan(20),
[
'foo' => 5.00,
],
[
'foo' => [
GreaterThan::NOT_GREATER_THAN => 'foo must be greater than 20',
],
],
],
'do not break isString rule' => [
new IsString(),
new LengthBetween(1, 3),
[
'foo' => 'abcdefg',
],
[
'foo' => [
LengthBetween::TOO_LONG => 'foo must be 3 characters or shorter',
],
],
],
];
}
}

0 comments on commit a68ac63

Please sign in to comment.