From cd2a4b1733a9b96147ecf567a5490fdb7f09f449 Mon Sep 17 00:00:00 2001 From: Tomasz Ferfecki Date: Wed, 18 Oct 2017 14:30:47 +0200 Subject: [PATCH 1/2] Regex rule - unexpected warning with comma character --- src/Validation.php | 7 +++- tests/ValidatonTest.php | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tests/ValidatonTest.php diff --git a/src/Validation.php b/src/Validation.php index 94fe854..c98640b 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -344,7 +344,12 @@ protected function parseRule($rule) { $exp = explode(':', $rule, 2); $rulename = $exp[0]; - $params = isset($exp[1])? explode(',', $exp[1]) : []; + if ($rulename !== 'regex') { + $params = isset($exp[1])? explode(',', $exp[1]) : []; + } else { + $params = [$exp[1]]; + } + return [$rulename, $params]; } diff --git a/tests/ValidatonTest.php b/tests/ValidatonTest.php new file mode 100644 index 0000000..edf8086 --- /dev/null +++ b/tests/ValidatonTest.php @@ -0,0 +1,71 @@ +getMethod('parseRule'); + $method->setAccessible(true); + + $validationMock = $this->getMockBuilder(Validation::class) + ->disableOriginalConstructor() + ->setMethodsExcept(['parseRule']) + ->getMock(); + + $result = $method->invokeArgs($validationMock, [$rules]); + $this->assertSame($expectedResult, $result); + } + + /** + * @return array + */ + public function parseRuleProvider() + { + return [ + [ + 'email', + [ + 'email', + [], + ], + ], + [ + 'min:6', + [ + 'min', + ['6'], + ], + ], + [ + 'uploaded_file:0,500K,png,jpeg', + [ + 'uploaded_file', + ['0', '500K', 'png', 'jpeg'], + ], + ], + [ + 'same:password', + [ + 'same', + ['password'], + ], + ], + [ + 'regex:/^([a-zA-Z\,]*)$/', + [ + 'regex', + ['/^([a-zA-Z\,]*)$/'], + ], + ], + ]; + } +} From 204372cab4791928cee96bb6a87c3e27b16b95d1 Mon Sep 17 00:00:00 2001 From: Tomasz Ferfecki Date: Wed, 18 Oct 2017 14:48:52 +0200 Subject: [PATCH 2/2] Changed test to work with lower versions of php --- tests/ValidatonTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/ValidatonTest.php b/tests/ValidatonTest.php index edf8086..9f82e70 100644 --- a/tests/ValidatonTest.php +++ b/tests/ValidatonTest.php @@ -1,6 +1,7 @@ getMethod('parseRule'); $method->setAccessible(true); - $validationMock = $this->getMockBuilder(Validation::class) - ->disableOriginalConstructor() - ->setMethodsExcept(['parseRule']) - ->getMock(); + $validation = new Validation(new Validator(), [], []); - $result = $method->invokeArgs($validationMock, [$rules]); + $result = $method->invokeArgs($validation, [$rules]); $this->assertSame($expectedResult, $result); }