Passed
Push — master ( 2649cb...eabf67 )
by Alexander
01:30
created

ValidatorFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 97
rs 10
c 1
b 0
f 0
eloc 40
wmc 5

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ createDataSet() 0 18 1
A hp$0 ➔ __construct() 0 3 1
A createTranslatorMock() 0 11 2
A hp$0 ➔ hasAttribute() 0 3 1
A testCreateWithInvalidRule() 0 11 1
A testCreateWithTranslator() 0 21 1
A hp$0 ➔ getAttributeValue() 0 3 2
createDataSet() 0 18 ?
A testCreate() 0 22 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\I18n\TranslatorInterface;
9
use Yiisoft\Validator\DataSetInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\ValidatorFactory;
12
13
class ValidatorFactoryTest extends TestCase
14
{
15
    public function testCreate()
16
    {
17
        $validation = new ValidatorFactory();
18
19
        $attribute = 'test';
20
        $errorMessage = 'error message';
21
22
        $validator = $validation->create(
23
            [
24
                $attribute => [
25
                    static function () use ($errorMessage) {
26
                        $result = new Result();
27
                        $result->addError($errorMessage);
28
                        return $result;
29
                    }
30
                ]
31
            ]
32
        );
33
34
        $result = $validator->validate($this->createDataSet([$attribute => '']));
35
36
        $this->assertSame($errorMessage, $result->getResult($attribute)->getErrors()[0]);
37
    }
38
39
    public function testCreateWithTranslator()
40
    {
41
        $translatableMessage = 'test message';
42
        $validation = new ValidatorFactory($this->createTranslatorMock($translatableMessage));
43
44
        $attribute = 'test';
45
        $validator = $validation->create(
46
            [
47
                $attribute => [
48
                    static function () {
49
                        $result = new Result();
50
                        $result->addError('error');
51
                        return $result;
52
                    }
53
                ]
54
            ]
55
        );
56
57
        $result = $validator->validate($this->createDataSet([$attribute => '']));
58
59
        $this->assertSame($translatableMessage, $result->getResult($attribute)->getErrors()[0]);
60
    }
61
62
63
    public function testCreateWithInvalidRule()
64
    {
65
        $validation = new ValidatorFactory();
66
67
        $this->expectException(\InvalidArgumentException::class);
68
69
        $attribute = 'test';
70
        $validation->create(
71
            [
72
                $attribute => [
73
                    'invalid rule'
74
                ]
75
            ]
76
        );
77
    }
78
79
    private function createTranslatorMock(string $returnMessage = null): TranslatorInterface
80
    {
81
        $translator = $this->createMock(TranslatorInterface::class);
82
83
        if ($returnMessage) {
84
            $translator
85
                ->method('translate')
86
                ->willReturn($returnMessage);
87
        }
88
89
        return $translator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $translator returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Yiisoft\I18n\TranslatorInterface.
Loading history...
90
    }
91
92
    private function createDataSet(array $attributes): DataSetInterface
93
    {
94
        return new class($attributes) implements DataSetInterface {
95
            private array $attributes;
96
97
            public function __construct(array $attributes)
98
            {
99
                $this->attributes = $attributes;
100
            }
101
102
            public function getAttributeValue(string $attribute)
103
            {
104
                return $this->hasAttribute($attribute) ? $this->attributes[$attribute] : null;
105
            }
106
107
            public function hasAttribute(string $attribute): bool
108
            {
109
                return array_key_exists($attribute, $this->attributes);
110
            }
111
        };
112
    }
113
}
114