6
6
use DigitalRevolution \SymfonyRequestValidation \Constraint \RequestConstraint ;
7
7
use DigitalRevolution \SymfonyRequestValidation \Constraint \RequestConstraintValidator ;
8
8
use PHPUnit \Framework \TestCase ;
9
+ use Symfony \Component \HttpFoundation \Exception \JsonException ;
9
10
use Symfony \Component \HttpFoundation \Request ;
10
11
use Symfony \Component \Validator \Constraints as Assert ;
11
12
use Symfony \Component \Validator \Context \ExecutionContext ;
@@ -45,6 +46,7 @@ public function testValidateUnexpectedTypeException(): void
45
46
46
47
/**
47
48
* @param array<mixed> $data
49
+ *
48
50
* @dataProvider \DigitalRevolution\SymfonyRequestValidation\Tests\DataProvider\Constraint\RequestConstraintValidatorDataProvider::dataProvider
49
51
* @covers ::validate
50
52
* @covers ::validateQuery
@@ -60,6 +62,7 @@ public function testValidateQuery(array $data, bool $success): void
60
62
61
63
/**
62
64
* @param array<mixed> $data
65
+ *
63
66
* @dataProvider \DigitalRevolution\SymfonyRequestValidation\Tests\DataProvider\Constraint\RequestConstraintValidatorDataProvider::dataProvider
64
67
* @covers ::validate
65
68
* @covers ::validateRequest
@@ -75,6 +78,42 @@ public function testValidateRequest(array $data, bool $success): void
75
78
76
79
/**
77
80
* @param array<mixed> $data
81
+ *
82
+ * @dataProvider \DigitalRevolution\SymfonyRequestValidation\Tests\DataProvider\Constraint\RequestConstraintValidatorDataProvider::dataProvider
83
+ * @covers ::validate
84
+ * @covers ::validateRequest
85
+ * @covers ::validateAndGetJsonBody
86
+ * @throws JsonException
87
+ */
88
+ public function testValidateJson (array $ data , bool $ success ): void
89
+ {
90
+ $ request = new Request ([], [], [], [], [], ['HTTP_CONTENT_TYPE ' => 'application/json ' ], json_encode ($ data , JSON_THROW_ON_ERROR ));
91
+ $ constraint = new RequestConstraint (['request ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())])]);
92
+ $ this ->context ->setConstraint ($ constraint );
93
+ $ this ->validator ->validate ($ request , $ constraint );
94
+ static ::assertCount ($ success ? 0 : 1 , $ this ->context ->getViolations ());
95
+ }
96
+
97
+ /**
98
+ * @covers ::validate
99
+ * @covers ::validateRequest
100
+ * @covers ::validateAndGetJsonBody
101
+ */
102
+ public function testValidateInvalidJson (): void
103
+ {
104
+ $ request = new Request ([], [], [], [], [], ['HTTP_CONTENT_TYPE ' => 'application/json ' ], '{invalid ' );
105
+ $ constraint = new RequestConstraint (['request ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())])]);
106
+ $ this ->context ->setConstraint ($ constraint );
107
+ $ this ->validator ->validate ($ request , $ constraint );
108
+
109
+ $ violations = $ this ->context ->getViolations ();
110
+ static ::assertCount (1 , $ violations );
111
+ static ::assertSame ('Request::content cant be decoded ' , $ violations ->get (0 )->getMessageTemplate ());
112
+ }
113
+
114
+ /**
115
+ * @param array<mixed> $data
116
+ *
78
117
* @dataProvider \DigitalRevolution\SymfonyRequestValidation\Tests\DataProvider\Constraint\RequestConstraintValidatorDataProvider::dataProvider
79
118
* @covers ::validate
80
119
* @covers ::validateAttributes
@@ -90,6 +129,7 @@ public function testValidateAttributes(array $data, bool $success): void
90
129
91
130
/**
92
131
* @param array<mixed> $data
132
+ *
93
133
* @dataProvider \DigitalRevolution\SymfonyRequestValidation\Tests\DataProvider\Constraint\RequestConstraintValidatorDataProvider::dataProvider
94
134
* @covers ::validate
95
135
* @covers ::validateQuery
@@ -99,19 +139,20 @@ public function testValidateAttributes(array $data, bool $success): void
99
139
public function testValidateQueryRequestAttributes (array $ data , bool $ success ): void
100
140
{
101
141
$ request = new Request ($ data , $ data , $ data );
102
- $ constraint = new RequestConstraint ([
103
- 'query ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())]),
104
- 'request ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())]),
105
- 'attributes ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())])
106
- ]);
142
+ $ constraint = new RequestConstraint (
143
+ [
144
+ 'query ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())]),
145
+ 'request ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())]),
146
+ 'attributes ' => new Assert \Collection (['email ' => new Assert \Required (new Assert \Email ())])
147
+ ]
148
+ );
107
149
$ this ->context ->setConstraint ($ constraint );
108
150
$ this ->validator ->validate ($ request , $ constraint );
109
151
static ::assertCount ($ success ? 0 : 3 , $ this ->context ->getViolations ());
110
152
}
111
153
112
154
/**
113
155
* Test that 'null' request should be ignored
114
- *
115
156
* @covers ::validate
116
157
*/
117
158
public function testValidateNullRequest (): void
@@ -125,7 +166,6 @@ public function testValidateNullRequest(): void
125
166
126
167
/**
127
168
* Test that 'null' request should be ignored
128
- *
129
169
* @covers ::validate
130
170
*/
131
171
public function testValidateWrongTypeViolation (): void
@@ -141,7 +181,6 @@ public function testValidateWrongTypeViolation(): void
141
181
142
182
/**
143
183
* Test that if no constraints have been specified. the request's query _must_ be empty
144
- *
145
184
* @covers ::validate
146
185
* @covers ::validateQuery
147
186
*/
@@ -158,14 +197,13 @@ public function testValidateEmptyConstraintsFilledQuery(): void
158
197
159
198
/**
160
199
* Test that if no constraints have been specified. the request's query _must_ not be empty
161
- *
162
200
* @covers ::validate
163
201
* @covers ::validateQuery
164
202
*/
165
203
public function testValidateEmptyConstraintsFilledQueryAllowed (): void
166
204
{
167
- $ request = new Request (['a ' ]);
168
- $ constraint = new RequestConstraint ();
205
+ $ request = new Request (['a ' ]);
206
+ $ constraint = new RequestConstraint ();
169
207
$ constraint ->allowExtraFields = true ;
170
208
$ this ->context ->setConstraint ($ constraint );
171
209
$ this ->validator ->validate ($ request , $ constraint );
@@ -175,7 +213,6 @@ public function testValidateEmptyConstraintsFilledQueryAllowed(): void
175
213
176
214
/**
177
215
* Test that if no constraints have been specified. the request's request _must_ be empty
178
- *
179
216
* @covers ::validate
180
217
* @covers ::validateRequest
181
218
*/
@@ -192,14 +229,13 @@ public function testValidateEmptyConstraintsFilledRequest(): void
192
229
193
230
/**
194
231
* Test that if no constraints have been specified, and extra fields are allowed. the request's request _must_ not be empty
195
- *
196
232
* @covers ::validate
197
233
* @covers ::validateRequest
198
234
*/
199
235
public function testValidateEmptyConstraintsFilledRequestAllowed (): void
200
236
{
201
- $ request = new Request ([], ['b ' ]);
202
- $ constraint = new RequestConstraint ();
237
+ $ request = new Request ([], ['b ' ]);
238
+ $ constraint = new RequestConstraint ();
203
239
$ constraint ->allowExtraFields = true ;
204
240
205
241
$ this ->context ->setConstraint ($ constraint );
0 commit comments