@@ -1423,6 +1423,58 @@ public function testValidateInContext()
14231423 $ entity = new Entity ();
14241424 $ entity ->reference = new Reference ();
14251425
1426+ $ callback1 = function ($ value , ExecutionContextInterface $ context ) {
1427+ $ context
1428+ ->getValidator ()
1429+ ->inContext ($ context )
1430+ ->atPath ('subpath ' )
1431+ ->validateObject ($ value ->reference )
1432+ ;
1433+ };
1434+
1435+ $ callback2 = function ($ value , ExecutionContextInterface $ context ) use ($ test , $ entity ) {
1436+ $ test ->assertSame ($ test ::REFERENCE_CLASS , $ context ->getClassName ());
1437+ $ test ->assertNull ($ context ->getPropertyName ());
1438+ $ test ->assertSame ('subpath ' , $ context ->getPropertyPath ());
1439+ $ test ->assertSame ('Group ' , $ context ->getGroup ());
1440+ $ test ->assertSame ($ test ->referenceMetadata , $ context ->getMetadata ());
1441+ $ test ->assertSame ($ test ->metadataFactory , $ context ->getMetadataFactory ());
1442+ $ test ->assertSame ($ entity , $ context ->getRoot ());
1443+ $ test ->assertSame ($ entity ->reference , $ context ->getValue ());
1444+ $ test ->assertSame ($ entity ->reference , $ value );
1445+
1446+ $ context ->addViolation ('Message %param% ' , array ('%param% ' => 'value ' ));
1447+ };
1448+
1449+ $ this ->metadata ->addConstraint (new Callback (array (
1450+ 'callback ' => $ callback1 ,
1451+ 'groups ' => 'Group ' ,
1452+ )));
1453+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
1454+ 'callback ' => $ callback2 ,
1455+ 'groups ' => 'Group ' ,
1456+ )));
1457+
1458+ $ violations = $ this ->validator ->validate ($ entity , 'Group ' );
1459+
1460+ /** @var ConstraintViolationInterface[] $violations */
1461+ $ this ->assertCount (1 , $ violations );
1462+ $ this ->assertSame ('Message value ' , $ violations [0 ]->getMessage ());
1463+ $ this ->assertSame ('Message %param% ' , $ violations [0 ]->getMessageTemplate ());
1464+ $ this ->assertSame (array ('%param% ' => 'value ' ), $ violations [0 ]->getMessageParameters ());
1465+ $ this ->assertSame ('subpath ' , $ violations [0 ]->getPropertyPath ());
1466+ $ this ->assertSame ($ entity , $ violations [0 ]->getRoot ());
1467+ $ this ->assertSame ($ entity ->reference , $ violations [0 ]->getInvalidValue ());
1468+ $ this ->assertNull ($ violations [0 ]->getMessagePluralization ());
1469+ $ this ->assertNull ($ violations [0 ]->getCode ());
1470+ }
1471+
1472+ public function testValidateInContextLegacyApi ()
1473+ {
1474+ $ test = $ this ;
1475+ $ entity = new Entity ();
1476+ $ entity ->reference = new Reference ();
1477+
14261478 $ callback1 = function ($ value , ExecutionContextInterface $ context ) {
14271479 $ context ->validate ($ value ->reference , 'subpath ' );
14281480 };
@@ -1470,6 +1522,58 @@ public function testValidateArrayInContext()
14701522 $ entity = new Entity ();
14711523 $ entity ->reference = new Reference ();
14721524
1525+ $ callback1 = function ($ value , ExecutionContextInterface $ context ) {
1526+ $ context
1527+ ->getValidator ()
1528+ ->inContext ($ context )
1529+ ->atPath ('subpath ' )
1530+ ->validateCollection (array ('key ' => $ value ->reference ))
1531+ ;
1532+ };
1533+
1534+ $ callback2 = function ($ value , ExecutionContextInterface $ context ) use ($ test , $ entity ) {
1535+ $ test ->assertSame ($ test ::REFERENCE_CLASS , $ context ->getClassName ());
1536+ $ test ->assertNull ($ context ->getPropertyName ());
1537+ $ test ->assertSame ('subpath[key] ' , $ context ->getPropertyPath ());
1538+ $ test ->assertSame ('Group ' , $ context ->getGroup ());
1539+ $ test ->assertSame ($ test ->referenceMetadata , $ context ->getMetadata ());
1540+ $ test ->assertSame ($ test ->metadataFactory , $ context ->getMetadataFactory ());
1541+ $ test ->assertSame ($ entity , $ context ->getRoot ());
1542+ $ test ->assertSame ($ entity ->reference , $ context ->getValue ());
1543+ $ test ->assertSame ($ entity ->reference , $ value );
1544+
1545+ $ context ->addViolation ('Message %param% ' , array ('%param% ' => 'value ' ));
1546+ };
1547+
1548+ $ this ->metadata ->addConstraint (new Callback (array (
1549+ 'callback ' => $ callback1 ,
1550+ 'groups ' => 'Group ' ,
1551+ )));
1552+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
1553+ 'callback ' => $ callback2 ,
1554+ 'groups ' => 'Group ' ,
1555+ )));
1556+
1557+ $ violations = $ this ->validator ->validate ($ entity , 'Group ' );
1558+
1559+ /** @var ConstraintViolationInterface[] $violations */
1560+ $ this ->assertCount (1 , $ violations );
1561+ $ this ->assertSame ('Message value ' , $ violations [0 ]->getMessage ());
1562+ $ this ->assertSame ('Message %param% ' , $ violations [0 ]->getMessageTemplate ());
1563+ $ this ->assertSame (array ('%param% ' => 'value ' ), $ violations [0 ]->getMessageParameters ());
1564+ $ this ->assertSame ('subpath[key] ' , $ violations [0 ]->getPropertyPath ());
1565+ $ this ->assertSame ($ entity , $ violations [0 ]->getRoot ());
1566+ $ this ->assertSame ($ entity ->reference , $ violations [0 ]->getInvalidValue ());
1567+ $ this ->assertNull ($ violations [0 ]->getMessagePluralization ());
1568+ $ this ->assertNull ($ violations [0 ]->getCode ());
1569+ }
1570+
1571+ public function testValidateArrayInContextLegacyApi ()
1572+ {
1573+ $ test = $ this ;
1574+ $ entity = new Entity ();
1575+ $ entity ->reference = new Reference ();
1576+
14731577 $ callback1 = function ($ value , ExecutionContextInterface $ context ) {
14741578 $ context ->validate (array ('key ' => $ value ->reference ), 'subpath ' );
14751579 };
@@ -1511,6 +1615,66 @@ public function testValidateArrayInContext()
15111615 $ this ->assertNull ($ violations [0 ]->getCode ());
15121616 }
15131617
1618+ public function testValidateInSeparateContext ()
1619+ {
1620+ $ test = $ this ;
1621+ $ entity = new Entity ();
1622+ $ entity ->reference = new Reference ();
1623+
1624+ $ callback1 = function ($ value , ExecutionContextInterface $ context ) use ($ test , $ entity ) {
1625+ $ violations = $ context
1626+ ->getValidator ()
1627+ // Since the validator is not context aware, the group must
1628+ // be passed explicitly
1629+ ->validateObject ($ value ->reference , 'Group ' )
1630+ ;
1631+
1632+ /** @var ConstraintViolationInterface[] $violations */
1633+ $ test ->assertCount (1 , $ violations );
1634+ $ test ->assertSame ('Message value ' , $ violations [0 ]->getMessage ());
1635+ $ test ->assertSame ('Message %param% ' , $ violations [0 ]->getMessageTemplate ());
1636+ $ test ->assertSame (array ('%param% ' => 'value ' ), $ violations [0 ]->getMessageParameters ());
1637+ $ test ->assertSame ('' , $ violations [0 ]->getPropertyPath ());
1638+ // The root is different as we're in a new context
1639+ $ test ->assertSame ($ entity ->reference , $ violations [0 ]->getRoot ());
1640+ $ test ->assertSame ($ entity ->reference , $ violations [0 ]->getInvalidValue ());
1641+ $ test ->assertNull ($ violations [0 ]->getMessagePluralization ());
1642+ $ test ->assertNull ($ violations [0 ]->getCode ());
1643+
1644+ // Verify that this method is called
1645+ $ context ->addViolation ('Separate violation ' );
1646+ };
1647+
1648+ $ callback2 = function ($ value , ExecutionContextInterface $ context ) use ($ test , $ entity ) {
1649+ $ test ->assertSame ($ test ::REFERENCE_CLASS , $ context ->getClassName ());
1650+ $ test ->assertNull ($ context ->getPropertyName ());
1651+ $ test ->assertSame ('' , $ context ->getPropertyPath ());
1652+ $ test ->assertSame ('Group ' , $ context ->getGroup ());
1653+ $ test ->assertSame ($ test ->referenceMetadata , $ context ->getMetadata ());
1654+ $ test ->assertSame ($ test ->metadataFactory , $ context ->getMetadataFactory ());
1655+ $ test ->assertSame ($ entity ->reference , $ context ->getRoot ());
1656+ $ test ->assertSame ($ entity ->reference , $ context ->getValue ());
1657+ $ test ->assertSame ($ entity ->reference , $ value );
1658+
1659+ $ context ->addViolation ('Message %param% ' , array ('%param% ' => 'value ' ));
1660+ };
1661+
1662+ $ this ->metadata ->addConstraint (new Callback (array (
1663+ 'callback ' => $ callback1 ,
1664+ 'groups ' => 'Group ' ,
1665+ )));
1666+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
1667+ 'callback ' => $ callback2 ,
1668+ 'groups ' => 'Group ' ,
1669+ )));
1670+
1671+ $ violations = $ this ->validator ->validate ($ entity , 'Group ' );
1672+
1673+ /** @var ConstraintViolationInterface[] $violations */
1674+ $ this ->assertCount (1 , $ violations );
1675+ $ test ->assertSame ('Separate violation ' , $ violations [0 ]->getMessage ());
1676+ }
1677+
15141678 public function testGetMetadataFactory ()
15151679 {
15161680 $ this ->assertSame ($ this ->metadataFactory , $ this ->validator ->getMetadataFactory ());
0 commit comments