|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Yiisoft\Validator\AttributeTranslator\ArrayAttributeTranslator; |
|
10
|
|
|
use Yiisoft\Validator\AttributeTranslator\NullAttributeTranslator; |
|
11
|
|
|
use Yiisoft\Validator\DataSet\ArrayDataSet; |
|
12
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
13
|
|
|
use Yiisoft\Validator\Validator; |
|
14
|
|
|
|
|
15
|
|
|
final class ValidationContextTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testGetDataSetWithoutDataSet(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$context = new ValidationContext(); |
|
20
|
|
|
|
|
21
|
|
|
$this->expectException(RuntimeException::class); |
|
22
|
|
|
$this->expectExceptionMessage('Data set in validation context is not set.'); |
|
23
|
|
|
$context->getDataSet(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testConstructor(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$context = new ValidationContext(['key' => 42]); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertSame(42, $context->getParameter('key')); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testDataSet(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$dataSet = new ArrayDataSet(); |
|
36
|
|
|
|
|
37
|
|
|
$context = new ValidationContext(); |
|
38
|
|
|
$context->setDataSet($dataSet); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame($dataSet, $context->getDataSet()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testSetParameter(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$context = new ValidationContext(); |
|
46
|
|
|
$context->setParameter('key', 42); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertSame(42, $context->getParameter('key')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testGetParameter(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$context = new ValidationContext(['key' => 42]); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertSame(42, $context->getParameter('key')); |
|
56
|
|
|
$this->assertNull($context->getParameter('non-exists')); |
|
57
|
|
|
$this->assertSame(7, $context->getParameter('non-exists', 7)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testValidateWithoutValidator(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$context = new ValidationContext(); |
|
63
|
|
|
|
|
64
|
|
|
$this->expectException(RuntimeException::class); |
|
65
|
|
|
$this->expectExceptionMessage('Validator is not set in validation context.'); |
|
66
|
|
|
$context->validate(42); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testGetRawDataWithoutRawData(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$context = new ValidationContext(); |
|
72
|
|
|
|
|
73
|
|
|
$this->expectException(RuntimeException::class); |
|
74
|
|
|
$this->expectExceptionMessage('Validator is not set in validation context.'); |
|
75
|
|
|
$context->getRawData(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testSetContextDataOnce(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$validator = new Validator(); |
|
81
|
|
|
$data1 = ['1']; |
|
82
|
|
|
$data2 = ['2']; |
|
83
|
|
|
$dataSet1 = new ArrayDataSet($data1); |
|
84
|
|
|
$dataSet2 = new ArrayDataSet($data2); |
|
85
|
|
|
|
|
86
|
|
|
$context = (new ValidationContext()) |
|
87
|
|
|
->setContextDataOnce($validator, new NullAttributeTranslator(), $data1, $dataSet1) |
|
88
|
|
|
->setContextDataOnce($validator, new NullAttributeTranslator(), $data2, $dataSet2); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertSame($data1, $context->getRawData()); |
|
91
|
|
|
$this->assertSame($dataSet1, $context->getGlobalDataSet()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function dataTranslatedAttributeWithoutTranslator(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
|
|
'null' => [null], |
|
98
|
|
|
'string' => ['test'], |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @dataProvider dataTranslatedAttributeWithoutTranslator |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testTranslatedAttributeWithoutTranslator(?string $attribute): void |
|
106
|
|
|
{ |
|
107
|
|
|
$context = new ValidationContext(); |
|
108
|
|
|
$context->setAttribute($attribute); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertSame($attribute, $context->getTranslatedAttribute()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testSetAttributeTranslator(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$translator = new ArrayAttributeTranslator(['name' => 'Имя']); |
|
116
|
|
|
|
|
117
|
|
|
$context = (new ValidationContext()) |
|
118
|
|
|
->setAttributeTranslator($translator) |
|
119
|
|
|
->setAttribute('name'); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertSame('Имя', $context->getTranslatedAttribute()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testSetAttributeLabel(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$context = (new ValidationContext()) |
|
127
|
|
|
->setAttribute('name') |
|
128
|
|
|
->setAttributeLabel('First Name'); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertSame('First Name', $context->getAttributeLabel()); |
|
131
|
|
|
$this->assertSame('First Name', $context->getTranslatedAttribute()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testSetAttributeLabelWithTranslator(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$translator = new ArrayAttributeTranslator(['First Name' => 'Имя']); |
|
137
|
|
|
|
|
138
|
|
|
$context = (new ValidationContext()) |
|
139
|
|
|
->setAttributeTranslator($translator) |
|
140
|
|
|
->setAttribute('name') |
|
141
|
|
|
->setAttributeLabel('First Name'); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertSame('Имя', $context->getTranslatedAttribute()); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|