From a1cdeee2dc9b7f11fbc4619962478eef56c0070f Mon Sep 17 00:00:00 2001 From: davidepastore Date: Sat, 2 Jan 2016 20:41:15 +0100 Subject: [PATCH] Improve and rearrange ValidationTest --- tests/ValidationTest.php | 92 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index d7b8f9c..dd8b3ea 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -84,27 +84,30 @@ public function testValidationWithErrors() $this->assertEquals($validators, $mw->getValidators()); } - public function testValidationWithoutValidators() + public function testValidationNotExistingOptionalParameter() { - $mw = new Validation(); + $notExistingValidator = v::optional(v::alpha()); + $validators = array( + 'notExisting' => $notExistingValidator, + ); + $mw = new Validation($validators); $next = function ($req, $res) { return $res; }; $response = $mw($this->request, $this->response, $next); + $errors = array(); - $validators = []; $this->assertFalse($mw->hasErrors()); $this->assertEquals($errors, $mw->getErrors()); - $this->assertEquals($validators, $mw->getValidators()); } - public function testValidationWithoutErrorsForMandatory() + public function testValidationNotExistingParameter() { - $optionalValidator = v::stringType()->notEmpty(); + $notExistingValidator = v::alpha(); $validators = array( - 'optional' => $optionalValidator, + 'notExisting' => $notExistingValidator, ); $mw = new Validation($validators); @@ -114,30 +117,27 @@ public function testValidationWithoutErrorsForMandatory() $response = $mw($this->request, $this->response, $next); - $this->assertFalse($mw->hasErrors()); - $this->assertEquals($validators, $mw->getValidators()); + $errors = array( + 'notExisting' => array( + 'null must contain only letters (a-z)', + ), + ); + $this->assertTrue($mw->hasErrors()); + $this->assertEquals($errors, $mw->getErrors()); } - public function testValidationWithErrorsForMandatory() + public function testValidationWithoutValidators() { - $optionalValidator = v::stringType()->notEmpty()->length(1, 4); - $validators = array( - 'optional' => $optionalValidator, - ); - $mw = new Validation($validators); + $mw = new Validation(); $next = function ($req, $res) { return $res; }; $response = $mw($this->request, $this->response, $next); - - $this->assertTrue($mw->hasErrors()); - $errors = array( - 'optional' => array( - '"value" must have a length between 1 and 4', - ), - ); + $errors = array(); + $validators = []; + $this->assertFalse($mw->hasErrors()); $this->assertEquals($errors, $mw->getErrors()); $this->assertEquals($validators, $mw->getValidators()); } @@ -163,10 +163,10 @@ public function testMultipleValidationWithoutErrors() $this->assertEquals($validators, $mw->getValidators()); } - public function testSetValidators() + public function testMultipleValidationWithErrors() { - $usernameValidator = v::alnum()->noWhitespace()->length(1, 20); - $ageValidator = v::numeric()->positive()->between(1, 100); + $usernameValidator = v::alnum()->noWhitespace()->length(1, 5); + $ageValidator = v::numeric()->positive()->between(1, 60); $validators = array( 'username' => $usernameValidator, 'age' => $ageValidator, @@ -177,35 +177,25 @@ public function testSetValidators() return $res; }; - $newUsernameValidator = v::alnum()->noWhitespace()->length(1, 10); - $newAgeValidator = v::numeric()->positive()->between(1, 20); - $newValidators = array( - 'username' => $newUsernameValidator, - 'age' => $newAgeValidator, - ); - - $mw->setValidators($newValidators); - $response = $mw($this->request, $this->response, $next); + $this->assertTrue($mw->hasErrors()); $errors = array( 'username' => array( - '"davidepastore" must have a length between 1 and 10', + '"davidepastore" must have a length between 1 and 5', ), 'age' => array( - '"89" must be lower than or equals 20', + '"89" must be lower than or equals 60', ), ); - - $this->assertTrue($mw->hasErrors()); $this->assertEquals($errors, $mw->getErrors()); - $this->assertEquals($newValidators, $mw->getValidators()); + $this->assertEquals($validators, $mw->getValidators()); } - public function testMultipleValidationWithErrors() + public function testSetValidators() { - $usernameValidator = v::alnum()->noWhitespace()->length(1, 5); - $ageValidator = v::numeric()->positive()->between(1, 60); + $usernameValidator = v::alnum()->noWhitespace()->length(1, 20); + $ageValidator = v::numeric()->positive()->between(1, 100); $validators = array( 'username' => $usernameValidator, 'age' => $ageValidator, @@ -216,19 +206,29 @@ public function testMultipleValidationWithErrors() return $res; }; + $newUsernameValidator = v::alnum()->noWhitespace()->length(1, 10); + $newAgeValidator = v::numeric()->positive()->between(1, 20); + $newValidators = array( + 'username' => $newUsernameValidator, + 'age' => $newAgeValidator, + ); + + $mw->setValidators($newValidators); + $response = $mw($this->request, $this->response, $next); - $this->assertTrue($mw->hasErrors()); $errors = array( 'username' => array( - '"davidepastore" must have a length between 1 and 5', + '"davidepastore" must have a length between 1 and 10', ), 'age' => array( - '"89" must be lower than or equals 60', + '"89" must be lower than or equals 20', ), ); + + $this->assertTrue($mw->hasErrors()); $this->assertEquals($errors, $mw->getErrors()); - $this->assertEquals($validators, $mw->getValidators()); + $this->assertEquals($newValidators, $mw->getValidators()); } public function testValidationWithCallableTranslator()