|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing ValueObjectVisitorBaseTest class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @version //autogentag// |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace eZ\Publish\Core\REST\Common\Tests\Output; |
|
12
|
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Common\Tests\AssertXmlTagTrait; |
|
14
|
|
|
use eZ\Publish\Core\REST\Server\Tests; |
|
15
|
|
|
use eZ\Publish\Core\REST\Common\Output\Generator; |
|
16
|
|
|
use eZ\Publish\Core\REST\Common\RequestParser as RequestParser; |
|
17
|
|
|
|
|
18
|
|
|
abstract class ValueObjectVisitorBaseTest extends Tests\BaseTest |
|
19
|
|
|
{ |
|
20
|
|
|
use AssertXmlTagTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Visitor mock. |
|
24
|
|
|
* |
|
25
|
|
|
* @var \eZ\Publish\Core\REST\Common\Output\Visitor |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $visitorMock; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Output generator. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \eZ\Publish\Core\REST\Common\Output\Generator\Xml |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $generator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \eZ\Publish\Core\REST\Common\RequestParser |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $requestParser; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
|
43
|
|
|
*/ |
|
44
|
|
|
private $routerMock; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
|
48
|
|
|
*/ |
|
49
|
|
|
private $templatedRouterMock; |
|
50
|
|
|
|
|
51
|
|
|
/** @var int */ |
|
52
|
|
|
private $routerCallIndex = 0; |
|
53
|
|
|
|
|
54
|
|
|
/** @var int */ |
|
55
|
|
|
private $templatedRouterCallIndex = 0; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Gets the visitor mock. |
|
59
|
|
|
* |
|
60
|
|
|
* @return \eZ\Publish\Core\REST\Common\Output\Visitor|\PHPUnit_Framework_MockObject_MockObject |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
protected function getVisitorMock() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!isset($this->visitorMock)) { |
|
65
|
|
|
$this->visitorMock = $this->getMock( |
|
66
|
|
|
'\\eZ\\Publish\\Core\\REST\\Common\\Output\\Visitor', |
|
67
|
|
|
array(), |
|
68
|
|
|
array(), |
|
69
|
|
|
'', |
|
70
|
|
|
false |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$this->visitorMock |
|
74
|
|
|
->expects($this->any()) |
|
75
|
|
|
->method('getResponse') |
|
76
|
|
|
->will($this->returnValue($this->getResponseMock())); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->visitorMock; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return \Symfony\Component\HttpFoundation\Response|\PHPUnit_Framework_MockObject_MockObject |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getResponseMock() |
|
86
|
|
|
{ |
|
87
|
|
|
if (!isset($this->responseMock)) { |
|
88
|
|
|
$this->responseMock = $this->getMock('Symfony\Component\HttpFoundation\Response'); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->responseMock; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Gets the output generator. |
|
96
|
|
|
* |
|
97
|
|
|
* @return \eZ\Publish\Core\REST\Common\Output\Generator\Xml |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getGenerator() |
|
100
|
|
|
{ |
|
101
|
|
|
if (!isset($this->generator)) { |
|
102
|
|
|
$this->generator = new Generator\Xml( |
|
103
|
|
|
new Generator\Xml\FieldTypeHashGenerator() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->generator; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Asserts that the given $xpathExpression returns a non empty node set |
|
112
|
|
|
* with $domNode as its context. |
|
113
|
|
|
* |
|
114
|
|
|
* This method asserts that $xpathExpression results in a non-empty node |
|
115
|
|
|
* set in context of $domNode, by wrapping the "boolean()" function around |
|
116
|
|
|
* it and evaluating it on the document owning $domNode. |
|
117
|
|
|
* |
|
118
|
|
|
* @param \DOMNode $domNode |
|
119
|
|
|
* @param string $xpathExpression |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function assertXPath(\DOMNode $domNode, $xpathExpression) |
|
122
|
|
|
{ |
|
123
|
|
|
$ownerDocument = ($domNode instanceof \DOMDOcument |
|
124
|
|
|
? $domNode |
|
125
|
|
|
: $domNode->ownerDocument); |
|
126
|
|
|
|
|
127
|
|
|
$xpath = new \DOMXPath($ownerDocument); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertTrue( |
|
130
|
|
|
$xpath->evaluate("boolean({$xpathExpression})", $domNode), |
|
131
|
|
|
"XPath expression '{$xpathExpression}' resulted in an empty node set." |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
protected function getVisitor() |
|
136
|
|
|
{ |
|
137
|
|
|
$visitor = $this->internalGetVisitor(); |
|
138
|
|
|
$visitor->setRequestParser($this->getRequestParser()); |
|
|
|
|
|
|
139
|
|
|
$visitor->setRouter($this->getRouterMock()); |
|
|
|
|
|
|
140
|
|
|
$visitor->setTemplateRouter($this->getTemplatedRouterMock()); |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
return $visitor; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return \eZ\Publish\Core\REST\Common\RequestParser|\PHPUnit_Framework_MockObject_MockObject |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getRequestParser() |
|
149
|
|
|
{ |
|
150
|
|
|
if (!isset($this->requestParser)) { |
|
151
|
|
|
$this->requestParser = $this->getMock('eZ\\Publish\\Core\\REST\\Common\\RequestParser'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $this->requestParser; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getRouterMock() |
|
161
|
|
|
{ |
|
162
|
|
|
if (!isset($this->routerMock)) { |
|
163
|
|
|
$this->routerMock = $this->getMock('Symfony\\Component\\Routing\\RouterInterface'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this->routerMock; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Resets the router mock and its expected calls index & list. |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function resetRouterMock() |
|
173
|
|
|
{ |
|
174
|
|
|
$this->routerMock = null; |
|
175
|
|
|
$this->routerMockCallIndex = 0; |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Adds an expectation to the routerMock. Expectations must be added sequentially. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $routeName |
|
182
|
|
|
* @param array $arguments |
|
183
|
|
|
* @param string $returnValue |
|
184
|
|
|
*/ |
|
185
|
|
View Code Duplication |
protected function addRouteExpectation($routeName, $arguments, $returnValue) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->getRouterMock() |
|
188
|
|
|
->expects($this->at($this->routerCallIndex++)) |
|
189
|
|
|
->method('generate') |
|
190
|
|
|
->with( |
|
191
|
|
|
$this->equalTo($routeName), |
|
192
|
|
|
$this->equalTo($arguments) |
|
193
|
|
|
) |
|
194
|
|
|
->will($this->returnValue($returnValue)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Adds the expectated value object visit calls sequence. |
|
199
|
|
|
* |
|
200
|
|
|
* @param array $valueObjects Array of visited value object, or of phpunit matcher ($this->instanceOf(), etc...) . |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function setVisitValueObjectExpectations(array $valueObjects) |
|
203
|
|
|
{ |
|
204
|
|
|
$consecutiveCalls = []; |
|
205
|
|
|
foreach ($valueObjects as $valueObject) { |
|
206
|
|
|
$consecutiveCalls = [ |
|
207
|
|
|
$valueObject, |
|
208
|
|
|
$this->getGenerator(), |
|
209
|
|
|
$this->getVisitorMock(), |
|
210
|
|
|
]; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$mocker = $this->getVisitorMock()->expects($this->any())->method('visitValueObject'); |
|
214
|
|
|
call_user_func_array([$mocker, 'withConsecutive'], $consecutiveCalls); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function getTemplatedRouterMock() |
|
221
|
|
|
{ |
|
222
|
|
|
if (!isset($this->templatedRouterMock)) { |
|
223
|
|
|
$this->templatedRouterMock = $this->getMock('Symfony\\Component\\Routing\\RouterInterface'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $this->templatedRouterMock; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Adds an expectation to the templatedRouterMock. Expectations must be added sequentially. |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $routeName |
|
233
|
|
|
* @param array $arguments |
|
234
|
|
|
* @param string $returnValue |
|
235
|
|
|
*/ |
|
236
|
|
View Code Duplication |
protected function addTemplatedRouteExpectation($routeName, $arguments, $returnValue) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->getTemplatedRouterMock() |
|
239
|
|
|
->expects($this->at($this->templatedRouterCallIndex++)) |
|
240
|
|
|
->method('generate') |
|
241
|
|
|
->with( |
|
242
|
|
|
$this->equalTo($routeName), |
|
243
|
|
|
$this->equalTo($arguments) |
|
244
|
|
|
) |
|
245
|
|
|
->will($this->returnValue($returnValue)); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Must return an instance of the tested visitor object. |
|
250
|
|
|
* |
|
251
|
|
|
* @return \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitor |
|
252
|
|
|
*/ |
|
253
|
|
|
abstract protected function internalGetVisitor(); |
|
254
|
|
|
} |
|
255
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: