1 | <?php |
||
22 | abstract class ConstraintTestCase extends BaseTestCase |
||
23 | { |
||
24 | private $expectedExceptionClass; |
||
25 | private $expectedExceptionPath; |
||
26 | private $expectedExceptionTarget; |
||
27 | |||
28 | /** |
||
29 | * @dataProvider applyTestProvider |
||
30 | * |
||
31 | * @param string $file |
||
32 | * @param string $title |
||
33 | * @param mixed $instance |
||
34 | * @param \stdClass $schema |
||
35 | * @param bool $isInstanceValid |
||
36 | * @param array $expectedErrors |
||
37 | */ |
||
38 | 185 | public function testApply( |
|
39 | $file, |
||
40 | $title, |
||
41 | $instance, |
||
42 | \stdClass $schema, |
||
43 | $isInstanceValid, |
||
44 | array $expectedErrors |
||
45 | ) { |
||
46 | 185 | $constraint = $this->getConstraint(); |
|
47 | 185 | $schemaContext = new Context(); |
|
48 | 185 | $validationContext = new Context(); |
|
49 | 185 | $walker = new Walker(new Registry(), new Resolver()); |
|
50 | |||
51 | 185 | $pathBefore = $schemaContext->getCurrentPath(); |
|
52 | 185 | $constraint->normalize($schema, $schemaContext, $walker); |
|
53 | 185 | $this->assertSame($pathBefore, $schemaContext->getCurrentPath()); |
|
54 | |||
55 | 185 | $constraint->apply($instance, $schema, $validationContext, $walker); |
|
56 | 185 | $actualErrors = $validationContext->getViolations(); |
|
57 | |||
58 | 185 | $this->assertValidationResult( |
|
59 | 185 | $file, |
|
60 | 185 | $title, |
|
61 | 185 | $instance, |
|
62 | 185 | $schema, |
|
63 | 185 | $isInstanceValid, |
|
64 | 185 | $expectedErrors, |
|
65 | $actualErrors |
||
66 | 185 | ); |
|
67 | 185 | } |
|
68 | |||
69 | /** |
||
70 | * @codeCoverageIgnore (data provider is executed before test is launched) |
||
71 | * |
||
72 | * Provider of #testApply(). |
||
73 | */ |
||
74 | public function applyTestProvider() |
||
132 | |||
133 | /** |
||
134 | * Returns an instance of the constraint to be tested. |
||
135 | * |
||
136 | * @return Constraint |
||
137 | */ |
||
138 | abstract protected function getConstraint(); |
||
139 | |||
140 | /** |
||
141 | * Returns an array of case file names for #testApply(). |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | abstract protected function getCaseFileNames(); |
||
146 | |||
147 | /** |
||
148 | * Returns a mocked walker instance. |
||
149 | * |
||
150 | * @return \PHPUnit_Framework_MockObject_MockObject|Walker |
||
151 | */ |
||
152 | 83 | protected function mockWalker() |
|
156 | |||
157 | /** |
||
158 | * Asserts a constraint exception will be thrown at a given path |
||
159 | * and optionally on a given target. |
||
160 | * |
||
161 | * @param string $exceptionName |
||
162 | * @param string $path |
||
163 | * @param string $target |
||
164 | */ |
||
165 | 61 | protected function expectConstraintException($exceptionName, $path, $target = null) |
|
172 | |||
173 | /** |
||
174 | * Implements the default hook, asserting that the exception thrown |
||
175 | * is an instance of ConstraintException and that its path and target |
||
176 | * match the expectations. |
||
177 | * |
||
178 | * @param \Exception $ex |
||
179 | * |
||
180 | * @throws \Exception |
||
181 | */ |
||
182 | 61 | protected function exceptionHook(\Exception $ex) |
|
219 | } |
||
220 |