Skip to content

Commit 657c754

Browse files
RobQuistNLrick-nu
authored andcommitted
Fix issue with rusing validator rule NotEmpty and IsRequired (#192)
* Add testcase and fix bug for NotEmpty rule being reused * Fix the same for Required rule
1 parent ccf88c8 commit 657c754

3 files changed

Lines changed: 116 additions & 2 deletions

File tree

src/Rule/NotEmpty.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class NotEmpty extends Rule
4040
*
4141
* @var bool
4242
*/
43-
protected $shouldBreak;
43+
protected $shouldBreak = false;
4444

4545
/**
4646
* Indicates if the value can be empty.
@@ -89,6 +89,7 @@ public function shouldBreakChain()
8989
*/
9090
public function validate($value)
9191
{
92+
$this->shouldBreak = false;
9293
if ($this->isEmpty($value)) {
9394
$this->shouldBreak = true;
9495

src/Rule/Required.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Required extends Rule
4040
*
4141
* @var bool
4242
*/
43-
protected $shouldBreak;
43+
protected $shouldBreak = false;
4444

4545
/**
4646
* Indicates if the value is required.
@@ -101,6 +101,7 @@ public function validate($value)
101101
*/
102102
public function isValid($key, Container $input)
103103
{
104+
$this->shouldBreak = false;
104105
$this->required = $this->isRequired($input);
105106

106107
if (!$input->has($key)) {

tests/ReusableValidatorTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
namespace Particle\Validator\Tests;
3+
4+
use Particle\Validator\Validator;
5+
6+
class ReusableValidatorTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @var Validator
10+
*/
11+
protected $validator;
12+
13+
protected function configureValidator()
14+
{
15+
$this->validator = new Validator();
16+
17+
$this->validator->required('customerNr')->alnum();
18+
$this->validator->required('paymentType')->inArray([
19+
'downPaymentChangeBankAccount',
20+
'downPaymentHighSpend',
21+
]);
22+
$this->validator->required('reference')->alnum();
23+
$this->validator->required('description')->lengthBetween(1, 250);
24+
$this->validator->required('amountPaid')->float()->between(0.01, 10000);
25+
$this->validator->required('notificationDate')->datetime('Ymd');
26+
}
27+
28+
public function testCanUseNewValidatorEveryTime()
29+
{
30+
foreach ($this->getTestData() as $rowKey => $testRow) {
31+
$this->configureValidator();
32+
33+
$result = $this->validator->validate($testRow['data']);
34+
$this->assertEquals($testRow['valid'], $result->isValid());
35+
$this->assertEquals($testRow['messages'], $result->getMessages());
36+
}
37+
}
38+
39+
public function testCanReuseValidatorMultipleTimes()
40+
{
41+
$this->configureValidator();
42+
43+
foreach ($this->getTestData() as $rowKey => $testRow) {
44+
$result = $this->validator->validate($testRow['data']);
45+
$this->assertEquals($testRow['valid'], $result->isValid());
46+
$this->assertEquals($testRow['messages'], $result->getMessages());
47+
}
48+
}
49+
50+
private function getTestData()
51+
{
52+
return [
53+
[
54+
'data' => [
55+
'customerNr' => 'c000012340',
56+
'paymentType' => '',
57+
'reference' => '',
58+
'description' => 'Test payment 1',
59+
'amountPaid' => 1000.00,
60+
'notificationDate' => '20150401',
61+
],
62+
'valid' => false,
63+
'messages' => [
64+
'paymentType' => [
65+
'NotEmpty::EMPTY_VALUE' => 'paymentType must not be empty',
66+
],
67+
'reference' => [
68+
'NotEmpty::EMPTY_VALUE' => 'reference must not be empty',
69+
],
70+
],
71+
],
72+
73+
[
74+
'data' => [
75+
'customerNr' => 'c000001234',
76+
'paymentType' => 'gfh',
77+
'description' => 'Test payment 2',
78+
'amountPaid' => 25.00,
79+
'notificationDate' => '20150401',
80+
],
81+
'valid' => false,
82+
'messages' => [
83+
'paymentType' => [
84+
'InArray::NOT_IN_ARRAY' => 'paymentType must be in the defined set of values',
85+
],
86+
'reference' => [
87+
'Required::NON_EXISTENT_KEY' => 'reference must be provided, but does not exist',
88+
],
89+
],
90+
],
91+
[
92+
'data' => [
93+
'customerNr' => 'c000005678',
94+
'paymentType' => 'sdf ',
95+
'reference' => '',
96+
'description' => 'Test payment 3',
97+
'amountPaid' => 25.00,
98+
'notificationDate' => '20150401',
99+
],
100+
'valid' => false,
101+
'messages' => [
102+
'paymentType' => [
103+
'InArray::NOT_IN_ARRAY' => 'paymentType must be in the defined set of values',
104+
],
105+
'reference' => [
106+
'NotEmpty::EMPTY_VALUE' => 'reference must not be empty',
107+
],
108+
],
109+
],
110+
];
111+
}
112+
}

0 commit comments

Comments
 (0)