Skip to content

Commit

Permalink
✔️ Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Dec 5, 2019
1 parent ba0d867 commit 7f65f43
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public function testValidatorStopOnError(): void
assertThat($validator->shouldStopOnError(), is(true));
}

public function testValidatorWithPartialValidation(): void
public function testValidatorWithJsonPartialValidation(): void
{
$rules = [
['email', EmailRule::class, EmailRule::REQUIRED => true],
['user', JsonRule::class, JsonRule::REQUIRED => true],
['user', JsonRule::class, JsonRule::REQUIRED => true, JsonRule::DECODE => true],
];

$data = [
'email' => 'elie29@gmail.com',
'user' => '{"name": "John Doe", "age": 25}',
'user' => '{"name": "John", "age": 25}',
];

$validator = new Validator($data, $rules);
Expand All @@ -81,19 +81,56 @@ public function testValidatorWithPartialValidation(): void

// In order to validate users json context
$validator->setRules([
['name', StringRule::class, MatchRule::class, MatchRule::PATTERN => '/^[a-z]{1, 20}$/i'],
['name', MatchRule::class, MatchRule::PATTERN => '/^[a-z]{1,20}$/i'],
['age', NumericRule::class, NumericRule::MAX => 80],
]);

$user = json_decode($validator->getValidatedContext()['user'], true);
$user = $validator->getValidatedContext()['user'];
$validator->setContext($user);

// Validate and merge context
assertThat($validator->validate(true), is(true));

$validatedPost = $validator->getValidatedContext();
assertThat($validatedPost, hasEntry('age', 25));
assertThat($validatedPost, hasKey('user'));
$data = $validator->getValidatedContext();
assertThat($data, hasEntry('age', 25));
assertThat($data, hasKey('user'));
}

public function testValidatorWithRawPartialValidation(): void
{
$rules = [
['email', EmailRule::class, EmailRule::REQUIRED => true],
['tags', ArrayRule::class, ArrayRule::REQUIRED => true],
];

$data = [
'email' => 'elie29@gmail.com',
'tags' => [
['code' => 12, 'slug' => 'one'],
['code' => 13, 'slug' => 'two'],
['code' => 15, 'slug' => 'three'],
],
];

$validator = new Validator($data, $rules);

assertThat($validator->validate(), is(true));

// In order to validate tags array context
$validator->setRules([
['code', NumericRule::class, NumericRule::MAX => 80],
['slug', MatchRule::class, MatchRule::PATTERN => '/^[a-z]+$/i'],
]);

$tags = $validator->getValidatedContext()['tags'];
$data = [];
foreach ($tags as $tag) {
$validator->setContext($tag);
assertThat($validator->validate(), is(true));
$data[] = $validator->getValidatedContext();
}

assertThat($data, arrayWithSize(3));
}

public function testValidatorGetImplodedErrors(): void
Expand Down

0 comments on commit 7f65f43

Please sign in to comment.