1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the JVal package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JVal\Testing; |
11
|
|
|
|
12
|
|
|
use JVal\Constraint; |
13
|
|
|
use JVal\Context; |
14
|
|
|
use JVal\Exception\ConstraintException; |
15
|
|
|
use JVal\Registry; |
16
|
|
|
use JVal\Resolver; |
17
|
|
|
use JVal\Walker; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Test case for testing validation constraints. |
21
|
|
|
*/ |
22
|
|
|
abstract class ConstraintTestCase extends DataTestCase |
23
|
|
|
{ |
24
|
|
|
private $expectedExceptionClass; |
25
|
|
|
private $expectedExceptionPath; |
26
|
|
|
private $expectedExceptionTarget; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @dataProvider fileDataProvider |
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
|
|
|
* Returns an instance of the constraint to be tested. |
71
|
|
|
* |
72
|
|
|
* @return Constraint |
73
|
|
|
*/ |
74
|
|
|
abstract protected function getConstraint(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @codeCoverageIgnore (called from a data provider, before test execution) |
78
|
|
|
* |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
protected function getDataDirectory() |
82
|
|
|
{ |
83
|
|
|
return __DIR__.'/../../tests/Data/constraints'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns a mocked walker instance. |
88
|
|
|
* |
89
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Walker |
90
|
|
|
*/ |
91
|
83 |
|
protected function mockWalker() |
92
|
|
|
{ |
93
|
83 |
|
return $this->mock('JVal\Walker'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Asserts a constraint exception will be thrown at a given path |
98
|
|
|
* and optionally on a given target. |
99
|
|
|
* |
100
|
|
|
* @param string $exceptionName |
101
|
|
|
* @param string $path |
102
|
|
|
* @param string $target |
103
|
|
|
*/ |
104
|
61 |
|
protected function expectConstraintException($exceptionName, $path, $target = null) |
105
|
|
|
{ |
106
|
61 |
|
$this->expectedExceptionClass = "JVal\\Exception\\Constraint\\{$exceptionName}"; |
107
|
61 |
|
$this->expectedExceptionPath = $path; |
108
|
61 |
|
$this->expectedExceptionTarget = $target; |
109
|
61 |
|
$this->expectException(); |
110
|
61 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Implements the default hook, asserting that the exception thrown |
114
|
|
|
* is an instance of ConstraintException and that its path and target |
115
|
|
|
* match the expectations. |
116
|
|
|
* |
117
|
|
|
* @param \Exception $ex |
118
|
|
|
* |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
61 |
|
protected function exceptionHook(\Exception $ex) |
122
|
|
|
{ |
123
|
|
|
// @codeCoverageIgnoreStart |
124
|
|
|
if (empty($this->expectedExceptionClass)) { |
125
|
|
|
throw $ex; |
126
|
|
|
} |
127
|
|
|
// @codeCoverageIgnoreEnd |
128
|
|
|
|
129
|
61 |
|
$this->assertThat( |
130
|
61 |
|
$ex, |
131
|
61 |
|
new \PHPUnit_Framework_Constraint_Exception( |
132
|
61 |
|
$this->expectedExceptionClass |
133
|
61 |
|
) |
134
|
61 |
|
); |
135
|
|
|
|
136
|
61 |
|
if ($ex instanceof ConstraintException) { |
137
|
61 |
|
$this->assertEquals( |
138
|
61 |
|
$this->expectedExceptionPath, |
139
|
61 |
|
$ex->getPath(), |
140
|
|
|
'Exception was not thrown at the expected path.' |
141
|
61 |
|
); |
142
|
|
|
|
143
|
61 |
|
if (!empty($this->expectedExceptionTarget)) { |
144
|
2 |
|
$this->assertEquals( |
145
|
2 |
|
$this->expectedExceptionTarget, |
146
|
2 |
|
$ex->getTarget(), |
147
|
|
|
'Exception does not have the expected target.' |
148
|
2 |
|
); |
149
|
2 |
|
} |
150
|
61 |
|
} else { |
151
|
|
|
// @codeCoverageIgnoreStart |
152
|
|
|
$this->fail('Exception thrown is not a ConstraintException'); |
153
|
|
|
// @codeCoverageIgnoreEnd |
154
|
|
|
} |
155
|
61 |
|
} |
156
|
|
|
} |
157
|
|
|
|