|
15 | 15 | use Symfony\Component\Validator\Constraints\Min;
|
16 | 16 | use Symfony\Component\Validator\Constraints\NotBlank;
|
17 | 17 | use Symfony\Component\Validator\Constraints\NotNull;
|
| 18 | +use Symfony\Component\Validator\Constraints\Required; |
| 19 | +use Symfony\Component\Validator\Constraints\Optional; |
18 | 20 | use Symfony\Component\Validator\Constraints\Collection;
|
19 | 21 | use Symfony\Component\Validator\Constraints\CollectionValidator;
|
20 | 22 |
|
@@ -309,6 +311,138 @@ public function testArrayObject() {
|
309 | 311 | $this->assertTrue($result);
|
310 | 312 | }
|
311 | 313 |
|
| 314 | + public function testOptionalFieldPresent() |
| 315 | + { |
| 316 | + $array = array( |
| 317 | + 'foo' => null, |
| 318 | + ); |
| 319 | + |
| 320 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 321 | + 'foo' => new Optional(), |
| 322 | + )))); |
| 323 | + } |
| 324 | + |
| 325 | + public function testOptionalFieldNotPresent() |
| 326 | + { |
| 327 | + $array = array( |
| 328 | + ); |
| 329 | + |
| 330 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 331 | + 'foo' => new Optional(), |
| 332 | + )))); |
| 333 | + } |
| 334 | + |
| 335 | + public function testOptionalFieldSingleConstraint() |
| 336 | + { |
| 337 | + $this->context->setGroup('MyGroup'); |
| 338 | + $this->context->setPropertyPath('bar'); |
| 339 | + |
| 340 | + $array = array( |
| 341 | + 'foo' => 5, |
| 342 | + ); |
| 343 | + |
| 344 | + $constraint = new Min(4); |
| 345 | + |
| 346 | + $this->walker->expects($this->once()) |
| 347 | + ->method('walkConstraint') |
| 348 | + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); |
| 349 | + |
| 350 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 351 | + 'foo' => new Optional($constraint), |
| 352 | + )))); |
| 353 | + } |
| 354 | + |
| 355 | + public function testOptionalFieldMultipleConstraints() |
| 356 | + { |
| 357 | + $this->context->setGroup('MyGroup'); |
| 358 | + $this->context->setPropertyPath('bar'); |
| 359 | + |
| 360 | + $array = array( |
| 361 | + 'foo' => 5, |
| 362 | + ); |
| 363 | + |
| 364 | + $constraints = array( |
| 365 | + new NotNull(), |
| 366 | + new Min(4), |
| 367 | + ); |
| 368 | + |
| 369 | + foreach ($constraints as $i => $constraint) { |
| 370 | + $this->walker->expects($this->at($i)) |
| 371 | + ->method('walkConstraint') |
| 372 | + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); |
| 373 | + } |
| 374 | + |
| 375 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 376 | + 'foo' => new Optional($constraints), |
| 377 | + )))); |
| 378 | + } |
| 379 | + |
| 380 | + public function testRequiredFieldPresent() |
| 381 | + { |
| 382 | + $array = array( |
| 383 | + 'foo' => null, |
| 384 | + ); |
| 385 | + |
| 386 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 387 | + 'foo' => new Required(), |
| 388 | + )))); |
| 389 | + } |
| 390 | + |
| 391 | + public function testRequiredFieldNotPresent() |
| 392 | + { |
| 393 | + $array = array( |
| 394 | + ); |
| 395 | + |
| 396 | + $this->assertFalse($this->validator->isValid($array, new Collection(array( |
| 397 | + 'foo' => new Required(), |
| 398 | + )))); |
| 399 | + } |
| 400 | + |
| 401 | + public function testRequiredFieldSingleConstraint() |
| 402 | + { |
| 403 | + $this->context->setGroup('MyGroup'); |
| 404 | + $this->context->setPropertyPath('bar'); |
| 405 | + |
| 406 | + $array = array( |
| 407 | + 'foo' => 5, |
| 408 | + ); |
| 409 | + |
| 410 | + $constraint = new Min(4); |
| 411 | + |
| 412 | + $this->walker->expects($this->once()) |
| 413 | + ->method('walkConstraint') |
| 414 | + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); |
| 415 | + |
| 416 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 417 | + 'foo' => new Required($constraint), |
| 418 | + )))); |
| 419 | + } |
| 420 | + |
| 421 | + public function testRequiredFieldMultipleConstraints() |
| 422 | + { |
| 423 | + $this->context->setGroup('MyGroup'); |
| 424 | + $this->context->setPropertyPath('bar'); |
| 425 | + |
| 426 | + $array = array( |
| 427 | + 'foo' => 5, |
| 428 | + ); |
| 429 | + |
| 430 | + $constraints = array( |
| 431 | + new NotNull(), |
| 432 | + new Min(4), |
| 433 | + ); |
| 434 | + |
| 435 | + foreach ($constraints as $i => $constraint) { |
| 436 | + $this->walker->expects($this->at($i)) |
| 437 | + ->method('walkConstraint') |
| 438 | + ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]')); |
| 439 | + } |
| 440 | + |
| 441 | + $this->assertTrue($this->validator->isValid($array, new Collection(array( |
| 442 | + 'foo' => new Required($constraints), |
| 443 | + )))); |
| 444 | + } |
| 445 | + |
312 | 446 | public function testObjectShouldBeLeftUnchanged()
|
313 | 447 | {
|
314 | 448 | $value = new \ArrayObject(array(
|
|
0 commit comments