Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e81f3b

Browse files
committed
[Validator] Finished test coverage and documentation of ExecutionContextManager
1 parent feb3d6f commit 1e81f3b

File tree

5 files changed

+218
-11
lines changed

5 files changed

+218
-11
lines changed

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

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,29 @@
1212
namespace Symfony\Component\Validator\Context;
1313

1414
use Symfony\Component\Translation\TranslatorInterface;
15+
use Symfony\Component\Validator\Exception\RuntimeException;
1516
use Symfony\Component\Validator\Group\GroupManagerInterface;
1617
use Symfony\Component\Validator\Node\Node;
1718
use Symfony\Component\Validator\NodeVisitor\AbstractVisitor;
1819
use Symfony\Component\Validator\Validator\ValidatorInterface;
1920

2021
/**
21-
* @since %%NextVersion%%
22+
* The default implementation of {@link ExecutionContextManagerInterface}.
23+
*
24+
* This class implements {@link \Symfony\Component\Validator\NodeVisitor\NodeVisitorInterface}
25+
* and updates the current context with the current node of the validation
26+
* traversal.
27+
*
28+
* After creating a new instance, the method {@link initialize()} must be
29+
* called with a {@link ValidatorInterface} instance. Calling methods such as
30+
* {@link startContext()} or {@link enterNode()} without initializing the
31+
* manager first will lead to errors.
32+
*
33+
* @since 2.5
2234
* @author Bernhard Schussek <[email protected]>
35+
*
36+
* @see ExecutionContextManagerInterface
37+
* @see \Symfony\Component\Validator\NodeVisitor\NodeVisitorInterface
2338
*/
2439
class ExecutionContextManager extends AbstractVisitor implements ExecutionContextManagerInterface
2540
{
@@ -53,6 +68,17 @@ class ExecutionContextManager extends AbstractVisitor implements ExecutionContex
5368
*/
5469
private $translationDomain;
5570

71+
/**
72+
* Creates a new context manager.
73+
*
74+
* @param GroupManagerInterface $groupManager The manager for accessing
75+
* the currently validated
76+
* group
77+
* @param TranslatorInterface $translator The translator
78+
* @param string|null $translationDomain The translation domain to
79+
* use for translating
80+
* violation messages
81+
*/
5682
public function __construct(GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
5783
{
5884
$this->groupManager = $groupManager;
@@ -61,15 +87,27 @@ public function __construct(GroupManagerInterface $groupManager, TranslatorInter
6187
$this->contextStack = new \SplStack();
6288
}
6389

90+
/**
91+
* Initializes the manager with a validator.
92+
*
93+
* @param ValidatorInterface $validator The validator
94+
*/
6495
public function initialize(ValidatorInterface $validator)
6596
{
6697
$this->validator = $validator;
6798
}
6899

100+
/**
101+
* {@inheritdoc}
102+
*
103+
* @throws RuntimeException If {@link initialize()} wasn't called
104+
*/
69105
public function startContext($root)
70106
{
71107
if (null === $this->validator) {
72-
// TODO error, call initialize() first
108+
throw new RuntimeException(
109+
'initialize() must be called before startContext().'
110+
);
73111
}
74112

75113
$this->currentContext = new LegacyExecutionContext(
@@ -84,10 +122,18 @@ public function startContext($root)
84122
return $this->currentContext;
85123
}
86124

125+
/**
126+
* {@inheritdoc}
127+
*
128+
* @throws RuntimeException If {@link startContext()} wasn't called
129+
*/
87130
public function stopContext()
88131
{
89132
if (0 === count($this->contextStack)) {
90-
return null;
133+
throw new RuntimeException(
134+
'No context was started yet. Call startContext() before '.
135+
'stopContext().'
136+
);
91137
}
92138

93139
// Remove the current context from the stack
@@ -101,24 +147,43 @@ public function stopContext()
101147
return $stoppedContext;
102148
}
103149

150+
/**
151+
* {@inheritdoc}
152+
*/
104153
public function getCurrentContext()
105154
{
106155
return $this->currentContext;
107156
}
108157

158+
/**
159+
* {@inheritdoc}
160+
*
161+
* @throws RuntimeException If {@link initialize()} wasn't called
162+
*/
109163
public function enterNode(Node $node)
110164
{
111165
if (null === $this->currentContext) {
112-
// TODO error call startContext() first
166+
throw new RuntimeException(
167+
'No context was started yet. Call startContext() before '.
168+
'enterNode().'
169+
);
113170
}
114171

115172
$this->currentContext->pushNode($node);
116173
}
117174

175+
/**
176+
* {@inheritdoc}
177+
*
178+
* @throws RuntimeException If {@link initialize()} wasn't called
179+
*/
118180
public function leaveNode(Node $node)
119181
{
120182
if (null === $this->currentContext) {
121-
// error no context started
183+
throw new RuntimeException(
184+
'No context was started yet. Call startContext() before '.
185+
'leaveNode().'
186+
);
122187
}
123188

124189
$this->currentContext->popNode();

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,74 @@
1212
namespace Symfony\Component\Validator\Context;
1313

1414
/**
15-
* @since %%NextVersion%%
15+
* Manages the creation and deletion of {@link ExecutionContextInterface}
16+
* instances.
17+
*
18+
* Start a new context with {@link startContext()}. You can retrieve the context
19+
* with {@link getCurrentContext()} and stop it again with {@link stopContext()}.
20+
*
21+
* $contextManager->startContext();
22+
* $context = $contextManager->getCurrentContext();
23+
* $contextManager->stopContext();
24+
*
25+
* You can also start several nested contexts. The {@link getCurrentContext()}
26+
* method will always return the most recently started context.
27+
*
28+
* // Start context 1
29+
* $contextManager->startContext();
30+
*
31+
* // Start context 2
32+
* $contextManager->startContext();
33+
*
34+
* // Returns context 2
35+
* $context = $contextManager->getCurrentContext();
36+
*
37+
* // Stop context 2
38+
* $contextManager->stopContext();
39+
*
40+
* // Returns context 1
41+
* $context = $contextManager->getCurrentContext();
42+
*
43+
* See also {@link ExecutionContextInterface} for more information.
44+
*
45+
* @since 2.5
1646
* @author Bernhard Schussek <[email protected]>
47+
*
48+
* @see ExecutionContextInterface
1749
*/
1850
interface ExecutionContextManagerInterface
1951
{
2052
/**
21-
* @param mixed $root
53+
* Starts a new context.
54+
*
55+
* The newly started context is returned. You can subsequently access the
56+
* context with {@link getCurrentContext()}.
57+
*
58+
* @param mixed $root The root value of the object graph in the new context
2259
*
2360
* @return ExecutionContextInterface The started context
2461
*/
2562
public function startContext($root);
2663

2764
/**
65+
* Stops the current context.
66+
*
67+
* If multiple contexts have been started, the most recently started context
68+
* is stopped. The stopped context is returned from this method.
69+
*
70+
* After calling this method, {@link getCurrentContext()} will return the
71+
* context that was started before the stopped context.
72+
*
2873
* @return ExecutionContextInterface The stopped context
2974
*/
3075
public function stopContext();
3176

3277
/**
78+
* Returns the current context.
79+
*
80+
* If multiple contexts have been started, the current context refers to the
81+
* most recently started context.
82+
*
3383
* @return ExecutionContextInterface The current context
3484
*/
3585
public function getCurrentContext();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
2222

2323
/**
24-
* A backwards compatible execution context.
24+
* An execution context that is compatible with the legacy API (< 2.5).
2525
*
2626
* @since 2.5
2727
* @author Bernhard Schussek <[email protected]>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Context;
13+
14+
use Symfony\Component\Validator\Context\ExecutionContextManager;
15+
16+
/**
17+
* @since 2.5
18+
* @author Bernhard Schussek <[email protected]>
19+
*/
20+
class ExecutionContextManagerTest extends \PHPUnit_Framework_TestCase
21+
{
22+
const TRANSLATION_DOMAIN = '__TRANSLATION_DOMAIN__';
23+
24+
/**
25+
* @var \PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $validator;
28+
29+
/**
30+
* @var \PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $groupManager;
33+
34+
/**
35+
* @var \PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $translator;
38+
39+
/**
40+
* @var ExecutionContextManager
41+
*/
42+
private $contextManager;
43+
44+
protected function setUp()
45+
{
46+
$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
47+
$this->groupManager = $this->getMock('Symfony\Component\Validator\Group\GroupManagerInterface');
48+
$this->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
49+
$this->contextManager = new ExecutionContextManager(
50+
$this->groupManager,
51+
$this->translator,
52+
self::TRANSLATION_DOMAIN
53+
);
54+
}
55+
56+
/**
57+
* @expectedException \Symfony\Component\Validator\Exception\RuntimeException
58+
*/
59+
public function testInitializeMustBeCalledBeforeStartContext()
60+
{
61+
$this->contextManager->startContext('root');
62+
}
63+
64+
/**
65+
* @expectedException \Symfony\Component\Validator\Exception\RuntimeException
66+
*/
67+
public function testCannotStopContextIfNoneWasStarted()
68+
{
69+
$this->contextManager->stopContext();
70+
}
71+
72+
/**
73+
* @expectedException \Symfony\Component\Validator\Exception\RuntimeException
74+
*/
75+
public function testCannotEnterNodeWithoutActiveContext()
76+
{
77+
$node = $this->getMockBuilder('Symfony\Component\Validator\Node\Node')
78+
->disableOriginalConstructor()
79+
->getMock();
80+
81+
$this->contextManager->enterNode($node);
82+
}
83+
84+
/**
85+
* @expectedException \Symfony\Component\Validator\Exception\RuntimeException
86+
*/
87+
public function testCannotLeaveNodeWithoutActiveContext()
88+
{
89+
$node = $this->getMockBuilder('Symfony\Component\Validator\Node\Node')
90+
->disableOriginalConstructor()
91+
->getMock();
92+
93+
$this->contextManager->leaveNode($node);
94+
}
95+
}

src/Symfony/Component/Validator/Tests/Context/ExecutionContextTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
namespace Symfony\Component\Validator\Tests\Context;
1313

1414
use Symfony\Component\Validator\Context\ExecutionContext;
15-
use Symfony\Component\Validator\Mapping\GenericMetadata;
16-
use Symfony\Component\Validator\Mapping\ClassMetadata;
17-
use Symfony\Component\Validator\Node\ClassNode;
1815
use Symfony\Component\Validator\Node\GenericNode;
1916

2017
/**

0 commit comments

Comments
 (0)