From 7848ccc71c4d2b87b0323030860042fb641db741 Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Sat, 14 Oct 2017 14:18:15 +0200 Subject: [PATCH 1/3] Remove old commented test --- tests/ValidationTest.php | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 84e484c..22f6b16 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -742,44 +742,4 @@ public function routeParamValidationProvider() ), ); } - - /* - public function testUseTemplate() - { - $this->setupGet(); - $hostnameValidator = v::regex('/^[a-zA-Z]([-.a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}$/')->setTemplate('Hostname {{name}} is not valid'); - $entryValidator = v::regex('/^[a-zA-Z]$/')->setTemplate('Entry {{name}} should contain only letters'); - $expectedValidators = array( - 'username' => $hostnameValidator, - 'age' => $entryValidator, - ); - $mw = new Validation($expectedValidators, null, ['useTemplate' => true]); - - $errors = null; - $hasErrors = null; - $validators = []; - $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) { - $errors = $req->getAttribute('errors'); - $hasErrors = $req->getAttribute('has_errors'); - $validators = $req->getAttribute('validators'); - - return $res; - }; - - $response = $mw($this->request, $this->response, $next); - - $expectedErrors = array( - 'username' => array( - 'Hostname "davidepastore" is not valid', - ), - 'age' => array( - 'Entry "89" should contain only letters', - ), - ); - - $this->assertTrue($hasErrors); - $this->assertEquals($expectedErrors, $errors); - $this->assertEquals($newValidators, $validators); - } - */ } From b0dffabf95bd8d7f222ffb1711fc0340d7104f81 Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Sat, 14 Oct 2017 15:52:35 +0200 Subject: [PATCH 2/3] Fix issue with $request->getAttribute('routeInfo')[2] and PHPUnit (closes #26) --- src/Validation.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Validation.php b/src/Validation.php index 669229b..dcf4ea3 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -99,7 +99,7 @@ public function __invoke($request, $response, $next) { $this->errors = []; $params = $request->getParams(); - $params = array_merge((array) $request->getAttribute('routeInfo')[2], $params); + $params = array_merge((array) $this->retrieveRouteInfo($request), $params); $this->validate($params, $this->validators); $request = $request->withAttribute($this->errors_name, $this->getErrors()); @@ -171,6 +171,21 @@ private function getNestedParam($params = [], $keys = []) } } + /** + * Retrieve the route info as an array if any. + * + * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request + * + * @return array An array with the array info. + */ + private function retrieveRouteInfo($request){ + if(isset($request->getAttribute('routeInfo')[2])){ + return (array) $request->getAttribute('routeInfo')[2]; + } else { + return []; + } + } + /** * Check if there are any errors. * From 990d04e3dc4c3900bf1d6e2e472a76b31f68269d Mon Sep 17 00:00:00 2001 From: Davide Pastore Date: Sun, 15 Oct 2017 16:08:28 +0200 Subject: [PATCH 3/3] Fix style --- src/Validation.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Validation.php b/src/Validation.php index 7483e97..05916bb 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -178,8 +178,9 @@ private function getNestedParam($params = [], $keys = []) * * @return array An array with the array info. */ - private function retrieveRouteInfo($request){ - if(isset($request->getAttribute('routeInfo')[2])){ + private function retrieveRouteInfo($request) + { + if (isset($request->getAttribute('routeInfo')[2])) { return (array) $request->getAttribute('routeInfo')[2]; } else { return [];