|
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
|
|
|
* {@inheritDoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getDataDirectory() |
|
80
|
|
|
{ |
|
81
|
|
|
return __DIR__.'/../../tests/Data/constraints'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns a mocked walker instance. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Walker |
|
88
|
|
|
*/ |
|
89
|
83 |
|
protected function mockWalker() |
|
90
|
|
|
{ |
|
91
|
83 |
|
return $this->mock('JVal\Walker'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Asserts a constraint exception will be thrown at a given path |
|
96
|
|
|
* and optionally on a given target. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $exceptionName |
|
99
|
|
|
* @param string $path |
|
100
|
|
|
* @param string $target |
|
101
|
|
|
*/ |
|
102
|
61 |
|
protected function expectConstraintException($exceptionName, $path, $target = null) |
|
103
|
|
|
{ |
|
104
|
61 |
|
$this->expectedExceptionClass = "JVal\\Exception\\Constraint\\{$exceptionName}"; |
|
105
|
61 |
|
$this->expectedExceptionPath = $path; |
|
106
|
61 |
|
$this->expectedExceptionTarget = $target; |
|
107
|
61 |
|
$this->expectException(); |
|
108
|
61 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Implements the default hook, asserting that the exception thrown |
|
112
|
|
|
* is an instance of ConstraintException and that its path and target |
|
113
|
|
|
* match the expectations. |
|
114
|
|
|
* |
|
115
|
|
|
* @param \Exception $ex |
|
116
|
|
|
* |
|
117
|
|
|
* @throws \Exception |
|
118
|
|
|
*/ |
|
119
|
61 |
|
protected function exceptionHook(\Exception $ex) |
|
120
|
|
|
{ |
|
121
|
|
|
// @codeCoverageIgnoreStart |
|
122
|
|
|
if (empty($this->expectedExceptionClass)) { |
|
123
|
|
|
throw $ex; |
|
124
|
|
|
} |
|
125
|
|
|
// @codeCoverageIgnoreEnd |
|
126
|
|
|
|
|
127
|
61 |
|
$this->assertThat( |
|
128
|
61 |
|
$ex, |
|
129
|
61 |
|
new \PHPUnit_Framework_Constraint_Exception( |
|
130
|
61 |
|
$this->expectedExceptionClass |
|
131
|
61 |
|
) |
|
132
|
61 |
|
); |
|
133
|
|
|
|
|
134
|
61 |
|
if ($ex instanceof ConstraintException) { |
|
135
|
61 |
|
$this->assertEquals( |
|
136
|
61 |
|
$this->expectedExceptionPath, |
|
137
|
61 |
|
$ex->getPath(), |
|
138
|
|
|
'Exception was not thrown at the expected path.' |
|
139
|
61 |
|
); |
|
140
|
|
|
|
|
141
|
61 |
|
if (!empty($this->expectedExceptionTarget)) { |
|
142
|
2 |
|
$this->assertEquals( |
|
143
|
2 |
|
$this->expectedExceptionTarget, |
|
144
|
2 |
|
$ex->getTarget(), |
|
145
|
|
|
'Exception does not have the expected target.' |
|
146
|
2 |
|
); |
|
147
|
2 |
|
} |
|
148
|
|
|
|
|
149
|
61 |
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// @codeCoverageIgnoreStart |
|
153
|
|
|
$this->fail('Exception thrown is not a ConstraintException'); |
|
154
|
|
|
// @codeCoverageIgnoreEnd |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|