Passed
Push — master ( 5db30c...c5e9e9 )
by Sergei
07:12 queued 04:43
created

NestedTest.php$1 ➔ testWithCallbackAttribute()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\TestEnvironments\Php81\Rule;
6
7
use PHPUnit\Framework\TestCase;
8
use ReflectionProperty;
9
use Yiisoft\Validator\Rule\Nested;
10
use Yiisoft\Validator\Rule\Number;
11
use Yiisoft\Validator\Tests\Support\Data\NestedClassAttribute;
12
use Yiisoft\Validator\Tests\Support\Data\NestedWithCallbackAttribute;
13
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility;
14
use Yiisoft\Validator\Validator;
15
16
final class NestedTest extends TestCase
17
{
18
    public function dataHandler(): array
19
    {
20
        return [
21
            'object' => [
22
                new class () {
23
                    #[Nested(['number' => new Number(max: 7)])]
24
                    private ObjectWithDifferentPropertyVisibility $object;
25
26
                    public function __construct()
27
                    {
28
                        $this->object = new ObjectWithDifferentPropertyVisibility();
29
                    }
30
                },
31
                [
32
                    'object.number' => ['Value must be no greater than 7.'],
33
                ],
34
            ],
35
            'object-private-only' => [
36
                new class () {
37
                    #[Nested(
38
                        ['age' => new Number(min: 100, skipOnEmpty: true), 'number' => new Number(max: 7)],
39
                        propertyVisibility: ReflectionProperty::IS_PRIVATE
40
                    )]
41
                    private ObjectWithDifferentPropertyVisibility $object;
42
43
                    public function __construct()
44
                    {
45
                        $this->object = new ObjectWithDifferentPropertyVisibility();
46
                    }
47
                },
48
                [
49
                    'object.number' => ['Value must be no greater than 7.'],
50
                ],
51
            ],
52
        ];
53
    }
54
55
    /**
56
     * @dataProvider dataHandler
57
     */
58
    public function testHandler(
59
        object $data,
60
        array $expectedErrorMessagesIndexedByPath,
61
        ?bool $expectedIsValid = false
62
    ): void {
63
        $result = (new Validator())->validate($data);
64
65
        $this->assertSame($expectedIsValid, $result->isValid());
66
        if (!$expectedIsValid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedIsValid of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
67
            $this->assertSame($expectedErrorMessagesIndexedByPath, $result->getErrorMessagesIndexedByPath());
68
        }
69
    }
70
71
    public function testClassAttribute(): void
72
    {
73
        $result = (new Validator())->validate(new NestedClassAttribute());
74
75
        $this->assertSame(
76
            [
77
                'a' => ['Value must be no less than 7.'],
78
                'b' => ['Value must be no greater than 1.'],
79
            ],
80
            $result->getErrorMessagesIndexedByAttribute()
81
        );
82
    }
83
84
    public function testWithCallbackAttribute(): void
85
    {
86
        $result = (new Validator())->validate(new NestedWithCallbackAttribute());
87
88
        $this->assertSame(
89
            [
90
                'a' => ['Invalid A.'],
91
                'b' => ['Invalid B.'],
92
            ],
93
            $result->getErrorMessagesIndexedByAttribute()
94
        );
95
    }
96
}
97