Skip to content

Commit

Permalink
Improve and rearrange ValidationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidePastore committed Jan 2, 2016
1 parent 60e3c66 commit a1cdeee
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit a1cdeee

Please sign in to comment.