Skip to content

Commit

Permalink
Merge validator tests (#383)
Browse files Browse the repository at this point in the history
* Merge validator tests

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
arogachev and StyleCIBot committed Nov 21, 2022
1 parent 871ed8b commit 49aa33e
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 179 deletions.
179 changes: 0 additions & 179 deletions tests/ValidateLogicTest.php

This file was deleted.

165 changes: 165 additions & 0 deletions tests/ValidatorTest.php
Expand Up @@ -22,10 +22,15 @@
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\SimpleRuleHandlerContainer;
use Yiisoft\Validator\SkipOnEmptyCallback\SkipOnNull;
use Yiisoft\Validator\Tests\Support\Data\EachNestedObjects\Foo;
use Yiisoft\Validator\Tests\Support\Data\IteratorWithBooleanKey;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDataSetAndRulesProvider;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithPostValidationHook;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithRulesProvider;
use Yiisoft\Validator\Tests\Support\ValidatorFactory;
use Yiisoft\Validator\Tests\Support\Rule\NotNullRule\NotNull;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDataSet;
Expand Down Expand Up @@ -56,6 +61,166 @@ public function testBase(): void
);
}

public function dataDataAndRulesCombinations(): array
{
return [
'pure-object-and-array-of-rules' => [
[
'number' => ['Value must be no less than 77.'],
],
new ObjectWithDifferentPropertyVisibility(),
[
'age' => new Number(max: 100),
'number' => new Number(min: 77),
],
],
'pure-object-and-no-rules' => [
[
'name' => ['Value cannot be blank.'],
'age' => ['Value must be no less than 21.'],
],
new ObjectWithDifferentPropertyVisibility(),
null,
],
'dataset-object-and-array-of-rules' => [
[
'key1' => ['Value must be no less than 21.'],
],
new ObjectWithDataSet(),
[
'key1' => new Number(min: 21),
],
],
'dataset-object-and-no-rules' => [
[],
new ObjectWithDataSet(),
null,
],
'rules-provider-object-and-array-of-rules' => [
[
'number' => ['Value must be no greater than 7.'],
],
new ObjectWithRulesProvider(),
[
'age' => new Number(max: 100),
'number' => new Number(max: 7),
],
],
'rules-provider-object-and-no-rules' => [
[
'age' => ['Value must be equal to "25".'],
],
new ObjectWithRulesProvider(),
null,
],
'rules-provider-and-dataset-object-and-array-of-rules' => [
[
'key2' => ['Value must be no greater than 7.'],
],
new ObjectWithDataSetAndRulesProvider(),
[
'key2' => new Number(max: 7),
],
],
'rules-provider-and-dataset-object-and-no-rules' => [
[
'key2' => ['Value must be equal to "99".'],
],
new ObjectWithDataSetAndRulesProvider(),
null,
],
'array-and-array-of-rules' => [
[
'key2' => ['Value must be no greater than 7.'],
],
['key1' => 15, 'key2' => 99],
[
'key1' => new Number(max: 100),
'key2' => new Number(max: 7),
],
],
'array-and-no-rules' => [
[],
['key1' => 15, 'key2' => 99],
null,
],
'scalar-and-array-of-rules' => [
[
'' => ['Value must be no greater than 7.'],
],
42,
[
new Number(max: 7),
],
],
'scalar-and-no-rules' => [
[],
42,
null,
],
'array-and-rules-provider' => [
[
'age' => ['Value must be no less than 18.'],
],
[
'age' => 17,
],
new class () implements RulesProviderInterface {
public function getRules(): iterable
{
return [
'age' => [new Number(min: 18)],
];
}
},
],
'array-and-object' => [
[
'name' => ['Value not passed.'],
'bars' => ['Value must be array or iterable.'],
],
[],
new Foo(),
],
];
}

/**
* @dataProvider dataDataAndRulesCombinations
*/
public function testDataAndRulesCombinations(
array $expectedErrorMessages,
mixed $data,
iterable|object|string|null $rules,
): void {
$validator = ValidatorFactory::make();
$result = $validator->validate($data, $rules);
$this->assertSame($expectedErrorMessages, $result->getErrorMessagesIndexedByAttribute());
}

public function dataWithEmptyArrayOfRules(): array
{
return [
'pure-object-and-no-rules' => [new ObjectWithDifferentPropertyVisibility()],
'dataset-object-and-no-rules' => [new ObjectWithDataSet()],
'rules-provider-object' => [new ObjectWithRulesProvider()],
'rules-provider-and-dataset-object' => [new ObjectWithDataSetAndRulesProvider()],
'array' => [['key1' => 15, 'key2' => 99]],
'scalar' => [42],
];
}

/**
* @dataProvider dataWithEmptyArrayOfRules
*/
public function testWithEmptyArrayOfRules(mixed $data): void
{
$validator = ValidatorFactory::make();
$result = $validator->validate($data, []);

$this->assertTrue($result->isValid());
}

public function testAddingRulesViaConstructor(): void
{
$dataObject = new ArrayDataSet(['bool' => true, 'int' => 41]);
Expand Down

0 comments on commit 49aa33e

Please sign in to comment.