8000 [Validator] Tested the validation in a separate context · symfony/symfony@feb3d6f · GitHub
[go: up one dir, main page]

Skip to content

Commit feb3d6f

Browse files
committed
[Validator] Tested the validation in a separate context
1 parent 718601c commit feb3d6f

File tree

6 files changed

+206
-43
lines changed

6 files changed

+206
-43
lines changed

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,13 @@ public function popNode()
137137
return null;
138138
}
139139

140-
$poppedNode = $this->node;
140+
// Remove the current node from the stack
141+
$poppedNode = $this->nodeStack->pop();
141142

142-
// After removing the last node, the stack is empty and the node
143-
// is null
144-
if (1 === count($this->nodeStack)) {
145-
$this->nodeStack->pop();
146-
$this->node = null;
147-
148-
return $poppedNode;
149-
}
150-
151-
$this->nodeStack->pop();
152-
$this->node = $this->nodeStack->top();
143+
// Adjust the current node to the previous node
144+
$this->node = count($this->nodeStack) > 0
145+
? $this->nodeStack->top()
146+
: null;
153147

154148
return $poppedNode;
155149
}

src/Symfony/Component/Validator/Context/ExecutionContextManager.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,31 @@ public function startContext($root)
7272
// TODO error, call initialize() first
7373
}
7474

75-
if (null !== $this->currentContext) {
76-
$this->contextStack->push($this->currentContext);
77-
}
78-
7975
$this->currentContext = new LegacyExecutionContext(
8076
$root,
8177
$this->validator,
8278
$this->groupManager,
8379
$this->translator,
8480
$this->translationDomain
8581
);
82+
$this->contextStack->push($this->currentContext);
8683

8784
return $this->currentContext;
8885
}
8986

9087
public function stopContext()
9188
{
92-
$stoppedContext = $this->currentContext;
93-
9489
if (0 === count($this->contextStack)) {
95-
$this->currentContext = null;
96-
97-
return $stoppedContext;
90+
return null;
9891
}
9992

100-
if (1 === count($this->contextStack)) {
101-
$this->contextStack->pop();
102-
$this->currentContext = null;
103-
104-
return $stoppedContext;
105-
}
93+
// Remove the current context from the stack
94+
$stoppedContext = $this->contextStack->pop();
10695

107-
$this->contextStack->pop();
108-
$this->currentContext = $this->contextStack->top();
96+
// Adjust the current context to the previous context
97+
$this->currentContext = count($this->contextStack) > 0
98+
? $this->contextStack->top()
99+
: null;
109100

110101
return $stoppedContext;
111102
}
@@ -115,11 +106,6 @@ public function getCurrentContext()
115106
return $this->currentContext;
116107
}
117108

118-
public function afterTraversal(array $nodes)
119-
{
120-
$this->contextStack = new \SplStack();
121-
}
122-
123109
public function enterNode(Node $node)
124110
{
125111
if (null === $this->currentContext) {

src/Symfony/Component/Validator/NodeVisitor/NodeValidator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class NodeValidator extends AbstractVisitor implements GroupManagerInterface
4747

4848
private $currentGroup;
4949

50-
private $currentObjectHash;
51-
5250
private $objectHashStack;
5351

5452
public function __construct(NodeTraverserInterface $nodeTraverser, ConstraintValidatorFactoryInterface $validatorFactory)

src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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());

src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,45 @@
1414
use Symfony\Component\Validator\Constraints\Valid;
1515
use Symfony\Component\Validator\MetadataFactoryInterface;
1616
use Symfony\Component\Validator\Tests\Fixtures\Entity;
17-
use Symfony\Component\Validator\Validator;
17+
use Symfony\Component\Validator\Validator as LegacyValidator;
1818
use Symfony\Component\Validator\DefaultTranslator;
1919
use Symfony\Component\Validator\ConstraintValidatorFactory;
2020

2121
class LegacyValidatorTest extends AbstractValidatorTest
2222
{
2323
protected function createValidator(MetadataFactoryInterface $metadataFactory)
2424
{
25-
return new Validator($metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator());
25+
return new LegacyValidator($metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator());
2626
}
2727

2828
public function testNoDuplicateValidationIfConstraintInMultipleGroups()
2929
{
30-
$this->markTestSkipped('Currently not supported');
30+
$this->markTestSkipped('Not supported in the legacy API');
3131
}
3232

3333
public function testGroupSequenceAbortsAfterFailedGroup()
3434
{
35-
$this->markTestSkipped('Currently not supported');
35+
$this->markTestSkipped('Not supported in the legacy API');
3636
}
3737

3838
public function testGroupSequenceIncludesReferences()
3939
{
40-
$this->markTestSkipped('Currently not supported');
40+
$this->markTestSkipped('Not supported in the legacy API');
41+
}
42+
43+
public function testValidateInContext()
44+
{
45+
$this->markTestSkipped('Not supported in the legacy API');
46+
}
47+
48+
public function testValidateArrayInContext()
49+
{
50+
$this->markTestSkipped('Not supported in the legacy API');
51+
}
52+
53+
public function testValidateInSeparateContext()
54+
{
55+
$this->markTestSkipped('Not supported in the legacy API');
4156
}
4257

4358
/**

src/Symfony/Component/Validator/Tests/Validator/ValidatorTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@
2424
use Symfony\Component\Validator\NodeTraverser\NodeTraverser;
2525
use Symfony\Component\Validator\Tests\Fixtures\Entity;
2626
use Symfony\Component\Validator\Validator\LegacyValidator;
27+
use Symfony\Component\Validator\Validator\ValidatorInterface;
2728

2829
class ValidatorTest extends AbstractValidatorTest
2930
{
31+
/**
32+
* @var ValidatorInterface
33+
*/
34+
protected $validator;
35+
3036
protected function createValidator(MetadataFactoryInterface $metadataFactory)
3137
{
3238
$nodeTraverser = new NodeTraverser($metadataFactory);
@@ -50,7 +56,7 @@ protected function createValidator(MetadataFactoryInterface $metadataFactory)
5056
return $validator;
5157
}
5258

53-
public function testValidateValueAcceptsValid()
59+
public function testValidateAcceptsValid()
5460
{
5561
$test = $this;
5662
$entity = new Entity();
@@ -75,7 +81,7 @@ public function testValidateValueAcceptsValid()
7581
)));
7682

7783
// This is the same as when calling validateObject()
78-
$violations = $this->validator->validateValue($entity, new Valid(), 'Group');
84+
$violations = $this->validator->validate($entity, new Valid(), 'Group');
7985

8086
/** @var ConstraintViolationInterface[] $violations */
8187
$this->assertCount(1, $violations);

0 commit comments

Comments
 (0)
0