|
15 | 15 | use Symfony\Component\Validator\Constraints\Callback;
|
16 | 16 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
17 | 17 | use Symfony\Component\Validator\ConstraintViolationInterface;
|
| 18 | +use Symfony\Component\Validator\Context\ExecutionContext; |
18 | 19 | use Symfony\Component\Validator\ExecutionContextInterface;
|
19 | 20 | use Symfony\Component\Validator\MetadataFactoryInterface;
|
20 | 21 | use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
|
@@ -1416,6 +1417,100 @@ public function testReplaceDefaultGroupWithArrayFromGroupSequenceProvider()
|
1416 | 1417 | $this->assertSame('Violation in Group 2', $violations[0]->getMessage());
|
1417 | 1418 | }
|
1418 | 1419 |
|
| 1420 | + public function testValidateInContext() |
| 1421 | + { |
| 1422 | + $test = $this; |
| 1423 | + $entity = new Entity(); |
| 1424 | + $entity->reference = new Reference(); |
| 1425 | + |
| 1426 | + $callback1 = function ($value, ExecutionContextInterface $context) { |
| 1427 | + $context->validate($value->reference, 'subpath'); |
| 1428 | + }; |
| 1429 | + |
| 1430 | + $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) { |
| 1431 | + $test->assertSame($test::REFERENCE_CLASS, $context->getClassName()); |
| 1432 | + $test->assertNull($context->getPropertyName()); |
| 1433 | + $test->assertSame('subpath', $context->getPropertyPath()); |
| 1434 | + $test->assertSame('Group', $context->getGroup()); |
| 1435 | + $test->assertSame($test->referenceMetadata, $context->getMetadata()); |
| 1436 | + $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); |
| 1437 | + $test->assertSame($entity, $context->getRoot()); |
| 1438 | + $test->assertSame($entity->reference, $context->getValue()); |
| 1439 | + $test->assertSame($entity->reference, $value); |
| 1440 | + |
| 1441 | + $context->addViolation('Message %param%', array('%param%' => 'value')); |
| 1442 | + }; |
| 1443 | + |
| 1444 | + $this->metadata->addConstraint(new Callback(array( |
| 1445 | + 'callback' => $callback1, |
| 1446 | + 'groups' => 'Group', |
| 1447 | + ))); |
| 1448 | + $this->referenceMetadata->addConstraint(new Callback(array( |
| 1449 | + 'callback' => $callback2, |
| 1450 | + 'groups' => 'Group', |
| 1451 | + ))); |
| 1452 | + |
| 1453 | + $violations = $this->validator->validate($entity, 'Group'); |
| 1454 | + |
| 1455 | + /** @var ConstraintViolationInterface[] $violations */ |
| 1456 | + $this->assertCount(1, $violations); |
| 1457 | + $this->assertSame('Message value', $violations[0]->getMessage()); |
| 1458 | + $this->assertSame('Message %param%', $violations[0]->getMessageTemplate()); |
| 1459 | + $this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters()); |
| 1460 | + $this->assertSame('subpath', $violations[0]->getPropertyPath()); |
| 1461 | + $this->assertSame($entity, $violations[0]->getRoot()); |
| 1462 | + $this->assertSame($entity->reference, $violations[0]->getInvalidValue()); |
| 1463 | + $this->assertNull($violations[0]->getMessagePluralization()); |
| 1464 | + $this->assertNull($violations[0]->getCode()); |
| 1465 | + } |
| 1466 | + |
| 1467 | + public function testValidateArrayInContext() |
| 1468 | + { |
| 1469 | + $test = $this; |
| 1470 | + $entity = new Entity(); |
| 1471 | + $entity->reference = new Reference(); |
| 1472 | + |
| 1473 | + $callback1 = function ($value, ExecutionContextInterface $context) { |
| 1474 | + $context->validate(array('key' => $value->reference), 'subpath'); |
| 1475 | + }; |
| 1476 | + |
| 1477 | + $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) { |
| 1478 | + $test->assertSame($test::REFERENCE_CLASS, $context->getClassName()); |
| 1479 | + $test->assertNull($context->getPropertyName()); |
| 1480 | + $test->assertSame('subpath[key]', $context->getPropertyPath()); |
| 1481 | + $test->assertSame('Group', $context->getGroup()); |
| 1482 | + $test->assertSame($test->referenceMetadata, $context->getMetadata()); |
| 1483 | + $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); |
| 1484 | + $test->assertSame($entity, $context->getRoot()); |
| 1485 | + $test->assertSame($entity->reference, $context->getValue()); |
| 1486 | + $test->assertSame($entity->reference, $value); |
| 1487 | + |
| 1488 | + $context->addViolation('Message %param%', array('%param%' => 'value')); |
| 1489 | + }; |
| 1490 | + |
| 1491 | + $this->metadata->addConstraint(new Callback(array( |
| 1492 | + 'callback' => $callback1, |
| 1493 | + 'groups' => 'Group', |
| 1494 | + ))); |
| 1495 | + $this->referenceMetadata->addConstraint(new Callback(array( |
| 1496 | + 'callback' => $callback2, |
| 1497 | + 'groups' => 'Group', |
| 1498 | + ))); |
| 1499 | + |
| 1500 | + $violations = $this->validator->validate($entity, 'Group'); |
| 1501 | + |
| 1502 | + /** @var ConstraintViolationInterface[] $violations */ |
| 1503 | + $this->assertCount(1, $violations); |
| 1504 | + $this->assertSame('Message value', $violations[0]->getMessage()); |
| 1505 | + $this->assertSame('Message %param%', $violations[0]->getMessageTemplate()); |
| 1506 | + $this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters()); |
| 1507 | + $this->assertSame('subpath[key]', $violations[0]->getPropertyPath()); |
| 1508 | + $this->assertSame($entity, $violations[0]->getRoot()); |
| 1509 | + $this->assertSame($entity->reference, $violations[0]->getInvalidValue()); |
| 1510 | + $this->assertNull($violations[0]->getMessagePluralization()); |
| 1511 | + $this->assertNull($violations[0]->getCode()); |
| 1512 | + } |
| 1513 | + |
1419 | 1514 | public function testGetMetadataFactory()
|
1420 | 1515 | {
|
1421 | 1516 | $this->assertSame($this->metadataFactory, $this->validator->getMetadataFactory());
|
|
0 commit comments