1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JBen87\ParsleyBundle\Tests\Constraint\Reader; |
4
|
|
|
|
5
|
|
|
use JBen87\ParsleyBundle\Constraint\Reader\EntityReader; |
6
|
|
|
use PHPUnit\Framework\MockObject\Matcher; |
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\Form\FormConfigInterface; |
10
|
|
|
use Symfony\Component\Form\FormInterface; |
11
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
12
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
13
|
|
|
use Symfony\Component\Validator\Exception\NoSuchMetadataException; |
14
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
15
|
|
|
use Symfony\Component\Validator\Mapping\GenericMetadata; |
16
|
|
|
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface; |
17
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
18
|
|
|
|
19
|
|
|
class EntityReaderTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MockObject|ValidatorInterface |
23
|
|
|
*/ |
24
|
|
|
private $validator; |
25
|
|
|
|
26
|
|
|
public function testReadNoDataClass(): void |
27
|
|
|
{ |
28
|
|
|
$form = $this->createMock(FormInterface::class); |
29
|
|
|
$this->setUpForm($form, $this->once(), null); |
30
|
|
|
|
31
|
|
|
$this->assertEmpty($this->createReader()->read($form)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param callable $setUpValidator |
36
|
|
|
* |
37
|
|
|
* @dataProvider provideReadNoMetadata |
38
|
|
|
*/ |
39
|
|
|
public function testReadNoMetadata(callable $setUpValidator): void |
40
|
|
|
{ |
41
|
|
|
$form = $this->createMock(FormInterface::class); |
42
|
|
|
$this->setUpForm($form); |
43
|
|
|
|
44
|
|
|
$setUpValidator($this->validator); |
45
|
|
|
|
46
|
|
|
$this->assertEmpty($this->createReader()->read($form)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function provideReadNoMetadata(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
[ |
56
|
|
|
function (MockObject $validator): void { |
57
|
|
|
$validator |
58
|
|
|
->expects($this->once()) |
59
|
|
|
->method('getMetadataFor') |
60
|
|
|
->with($this->isType('string')) |
61
|
|
|
->willThrowException(new NoSuchMetadataException()) |
62
|
|
|
; |
63
|
|
|
}, |
64
|
|
|
], |
65
|
|
|
[ |
66
|
|
|
function (MockObject $validator): void { |
67
|
|
|
$validator |
68
|
|
|
->expects($this->once()) |
69
|
|
|
->method('getMetadataFor') |
70
|
|
|
->with($this->isType('string')) |
71
|
|
|
->willReturn(new GenericMetadata()) |
72
|
|
|
; |
73
|
|
|
}, |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testReadNoPropertyMetadata(): void |
79
|
|
|
{ |
80
|
|
|
$form = $this->createMock(FormInterface::class); |
81
|
|
|
$this->setUpForm($form); |
82
|
|
|
|
83
|
|
|
$this->setUpValidator($this->validator, $this->never(), []); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$this->assertEmpty($this->createReader()->read($form)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testReadNoConstraints(): void |
89
|
|
|
{ |
90
|
|
|
$form = $this->createMock(FormInterface::class); |
91
|
|
|
$this->setUpForm($form); |
92
|
|
|
|
93
|
|
|
$propertyMetadatum = $this->createMock(PropertyMetadataInterface::class); |
94
|
|
|
$propertyMetadatum |
95
|
|
|
->expects($this->once()) |
96
|
|
|
->method('findConstraints') |
97
|
|
|
->with('Default') |
98
|
|
|
->willReturn([]) |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
$this->setUpValidator($this->validator, $this->once(), [$propertyMetadatum]); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->assertEmpty($this->createReader()->read($form)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testReadWithConstraints(): void |
107
|
|
|
{ |
108
|
|
|
$form = $this->createMock(FormInterface::class); |
109
|
|
|
$this->setUpForm($form); |
110
|
|
|
|
111
|
|
|
$propertyMetadatum1 = $this->createMock(PropertyMetadataInterface::class); |
112
|
|
|
$propertyMetadatum1 |
113
|
|
|
->expects($this->once()) |
114
|
|
|
->method('findConstraints') |
115
|
|
|
->with('Default') |
116
|
|
|
->willReturn([new NotBlank()]) |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
$propertyMetadatum2 = $this->createMock(PropertyMetadataInterface::class); |
120
|
|
|
$propertyMetadatum2 |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('findConstraints') |
123
|
|
|
->with('Default') |
124
|
|
|
->willReturn([new NotNull()]) |
125
|
|
|
; |
126
|
|
|
|
127
|
|
|
$this->setUpValidator($this->validator, $this->exactly(2), [$propertyMetadatum1, $propertyMetadatum2]); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$this->assertEquals([new NotBlank(), new NotNull()], $this->createReader()->read($form)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
*/ |
135
|
|
|
protected function setUp(): void |
136
|
|
|
{ |
137
|
|
|
$this->validator = $this->createMock(ValidatorInterface::class); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param MockObject $form |
142
|
|
|
* @param Matcher\InvokedCount|null $configMatcher |
143
|
|
|
* @param null|string $dataClass |
144
|
|
|
*/ |
145
|
|
|
private function setUpForm( |
146
|
|
|
MockObject $form, |
147
|
|
|
Matcher\InvokedCount $configMatcher = null, |
148
|
|
|
?string $dataClass = '\\stdClass' |
149
|
|
|
): void { |
150
|
|
|
if (null === $configMatcher) { |
151
|
|
|
$configMatcher = $this->exactly(2); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$config = $this->createMock(FormConfigInterface::class); |
155
|
|
|
$config |
156
|
|
|
->expects($configMatcher) |
157
|
|
|
->method('getDataClass') |
158
|
|
|
->willReturn($dataClass) |
159
|
|
|
; |
160
|
|
|
|
161
|
|
|
$rootForm = $this->createMock(FormInterface::class); |
162
|
|
|
$rootForm |
163
|
|
|
->expects($this->once()) |
164
|
|
|
->method('getConfig') |
165
|
|
|
->willReturn($config) |
166
|
|
|
; |
167
|
|
|
|
168
|
|
|
$form |
169
|
|
|
->expects($this->once()) |
170
|
|
|
->method('getRoot') |
171
|
|
|
->willReturn($rootForm) |
172
|
|
|
; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param MockObject $validator |
177
|
|
|
* @param Matcher\InvokedCount $groupMatcher |
178
|
|
|
* @param PropertyMetadataInterface[] $propertyMetadata |
179
|
|
|
*/ |
180
|
|
|
private function setUpValidator( |
181
|
|
|
MockObject $validator, |
182
|
|
|
Matcher\InvokedCount $groupMatcher, |
183
|
|
|
array $propertyMetadata |
184
|
|
|
): void { |
185
|
|
|
if (null === $groupMatcher) { |
186
|
|
|
$groupMatcher = $this->never(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
190
|
|
|
$metadata |
191
|
|
|
->expects($this->once()) |
192
|
|
|
->method('getPropertyMetadata') |
193
|
|
|
->with(null) |
194
|
|
|
->willReturn($propertyMetadata) |
195
|
|
|
; |
196
|
|
|
$metadata |
197
|
|
|
->expects($groupMatcher) |
198
|
|
|
->method('getDefaultGroup') |
199
|
|
|
->willReturn('Default') |
200
|
|
|
; |
201
|
|
|
|
202
|
|
|
$validator |
203
|
|
|
->expects($this->once()) |
204
|
|
|
->method('getMetadataFor') |
205
|
|
|
->with($this->isType('string')) |
206
|
|
|
->willReturn($metadata) |
207
|
|
|
; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return EntityReader |
212
|
|
|
*/ |
213
|
|
|
private function createReader(): EntityReader |
214
|
|
|
{ |
215
|
|
|
return new EntityReader($this->validator); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|