|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the VisitorTest 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
|
|
|
namespace eZ\Publish\Core\REST\Common\Tests\Output; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\REST\Common; |
|
12
|
|
|
use eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher; |
|
13
|
|
|
use eZ\Publish\Core\REST\Common\Output\Generator; |
|
14
|
|
|
use eZ\Publish\Core\REST\Common\Output\Visitor; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use stdClass; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Visitor test. |
|
21
|
|
|
*/ |
|
22
|
|
|
class VisitorTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testVisitDocument() |
|
25
|
|
|
{ |
|
26
|
|
|
$data = new stdClass(); |
|
27
|
|
|
|
|
28
|
|
|
$generator = $this->getGeneratorMock(); |
|
29
|
|
|
$generator |
|
30
|
|
|
->expects($this->at(1)) |
|
31
|
|
|
->method('startDocument') |
|
32
|
|
|
->with($data); |
|
33
|
|
|
|
|
34
|
|
|
$generator |
|
35
|
|
|
->expects($this->at(2)) |
|
36
|
|
|
->method('isEmpty') |
|
37
|
|
|
->will($this->returnValue(false)); |
|
38
|
|
|
|
|
39
|
|
|
$generator |
|
40
|
|
|
->expects($this->at(3)) |
|
41
|
|
|
->method('endDocument') |
|
42
|
|
|
->with($data) |
|
43
|
|
|
->will($this->returnValue('Hello world!')); |
|
44
|
|
|
|
|
45
|
|
|
$visitor = $this->getMockBuilder(Visitor::class) |
|
46
|
|
|
->setMethods(array('visitValueObject')) |
|
47
|
|
|
->setConstructorArgs(array($generator, $this->getValueObjectDispatcherMock())) |
|
48
|
|
|
->getMock(); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals( |
|
51
|
|
|
new Response('Hello world!', 200, array()), |
|
52
|
|
|
$visitor->visit($data) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testVisitEmptyDocument() |
|
57
|
|
|
{ |
|
58
|
|
|
$data = new stdClass(); |
|
59
|
|
|
|
|
60
|
|
|
$generator = $this->getGeneratorMock(); |
|
61
|
|
|
$generator |
|
62
|
|
|
->expects($this->at(1)) |
|
63
|
|
|
->method('startDocument') |
|
64
|
|
|
->with($data); |
|
65
|
|
|
|
|
66
|
|
|
$generator |
|
67
|
|
|
->expects($this->at(2)) |
|
68
|
|
|
->method('isEmpty') |
|
69
|
|
|
->will($this->returnValue(true)); |
|
70
|
|
|
|
|
71
|
|
|
$generator |
|
72
|
|
|
->expects($this->never()) |
|
73
|
|
|
->method('endDocument'); |
|
74
|
|
|
|
|
75
|
|
|
$visitor = $this->getMockBuilder(Visitor::class) |
|
76
|
|
|
->setMethods(array('visitValueObject')) |
|
77
|
|
|
->setConstructorArgs(array($generator, $this->getValueObjectDispatcherMock())) |
|
78
|
|
|
->getMock(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals( |
|
81
|
|
|
new Response(null, 200, array()), |
|
82
|
|
|
$visitor->visit($data) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testVisitValueObject() |
|
87
|
|
|
{ |
|
88
|
|
|
$data = new stdClass(); |
|
89
|
|
|
|
|
90
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|Common\Output\Generator $generatorMock */ |
|
91
|
|
|
$generatorMock = $this->getGeneratorMock(); |
|
92
|
|
|
|
|
93
|
|
|
$valueObjectDispatcherMock = $this->getValueObjectDispatcherMock(); |
|
94
|
|
|
$valueObjectDispatcherMock |
|
95
|
|
|
->expects($this->once()) |
|
96
|
|
|
->method('visit') |
|
97
|
|
|
->with($data); |
|
98
|
|
|
|
|
99
|
|
|
$visitor = new Common\Output\Visitor($generatorMock, $valueObjectDispatcherMock); |
|
|
|
|
|
|
100
|
|
|
$visitor->visit($data); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testSetHeaders() |
|
104
|
|
|
{ |
|
105
|
|
|
$data = new stdClass(); |
|
106
|
|
|
|
|
107
|
|
|
$visitor = $this->getVisitorMock(); |
|
108
|
|
|
|
|
109
|
|
|
$visitor->setHeader('Content-Type', 'text/xml'); |
|
110
|
|
|
$this->assertEquals( |
|
111
|
|
|
new Response( |
|
112
|
|
|
null, |
|
113
|
|
|
200, |
|
114
|
|
|
array( |
|
115
|
|
|
'Content-Type' => 'text/xml', |
|
116
|
|
|
) |
|
117
|
|
|
), |
|
118
|
|
|
$visitor->visit($data) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @todo This is a test for a feature that needs refactoring. |
|
124
|
|
|
* |
|
125
|
|
|
* @see \eZ\Publish\Core\REST\Common\Output\Visitor::visit |
|
126
|
|
|
*/ |
|
127
|
|
|
public function testSetFilteredHeaders() |
|
128
|
|
|
{ |
|
129
|
|
|
$data = new stdClass(); |
|
130
|
|
|
|
|
131
|
|
|
$visitor = $this->getVisitorMock(); |
|
132
|
|
|
|
|
133
|
|
|
$visitor->setHeader('Content-Type', 'text/xml'); |
|
134
|
|
|
$visitor->setHeader('Accept-Patch', false); |
|
135
|
|
|
$this->assertEquals( |
|
136
|
|
|
new Response( |
|
137
|
|
|
null, |
|
138
|
|
|
200, |
|
139
|
|
|
array( |
|
140
|
|
|
'Content-Type' => 'text/xml', |
|
141
|
|
|
) |
|
142
|
|
|
), |
|
143
|
|
|
$visitor->visit($data) |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testSetHeadersNoOverwrite() |
|
148
|
|
|
{ |
|
149
|
|
|
$data = new stdClass(); |
|
150
|
|
|
|
|
151
|
|
|
$visitor = $this->getVisitorMock(); |
|
152
|
|
|
|
|
153
|
|
|
$visitor->setHeader('Content-Type', 'text/xml'); |
|
154
|
|
|
$visitor->setHeader('Content-Type', 'text/html'); |
|
155
|
|
|
$this->assertEquals( |
|
156
|
|
|
new Response( |
|
157
|
|
|
null, |
|
158
|
|
|
200, |
|
159
|
|
|
array( |
|
160
|
|
|
'Content-Type' => 'text/xml', |
|
161
|
|
|
) |
|
162
|
|
|
), |
|
163
|
|
|
$visitor->visit($data) |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testSetHeaderResetAfterVisit() |
|
168
|
|
|
{ |
|
169
|
|
|
$data = new stdClass(); |
|
170
|
|
|
|
|
171
|
|
|
$visitor = $this->getVisitorMock(); |
|
172
|
|
|
|
|
173
|
|
|
$visitor->setHeader('Content-Type', 'text/xml'); |
|
174
|
|
|
|
|
175
|
|
|
$visitor->visit($data); |
|
176
|
|
|
$result = $visitor->visit($data); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertEquals( |
|
179
|
|
|
new Response( |
|
180
|
|
|
null, |
|
181
|
|
|
200, |
|
182
|
|
|
array() |
|
183
|
|
|
), |
|
184
|
|
|
$result |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
View Code Duplication |
public function testSetStatusCode() |
|
189
|
|
|
{ |
|
190
|
|
|
$data = new stdClass(); |
|
191
|
|
|
|
|
192
|
|
|
$visitor = $this->getVisitorMock(); |
|
193
|
|
|
|
|
194
|
|
|
$visitor->setStatus(201); |
|
195
|
|
|
$this->assertEquals( |
|
196
|
|
|
new Response( |
|
197
|
|
|
null, |
|
198
|
|
|
201 |
|
199
|
|
|
), |
|
200
|
|
|
$visitor->visit($data) |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
View Code Duplication |
public function testSetStatusCodeNoOverride() |
|
205
|
|
|
{ |
|
206
|
|
|
$data = new stdClass(); |
|
207
|
|
|
|
|
208
|
|
|
$visitor = $this->getVisitorMock(); |
|
209
|
|
|
|
|
210
|
|
|
$visitor->setStatus(201); |
|
211
|
|
|
$visitor->setStatus(404); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertEquals( |
|
214
|
|
|
new Response( |
|
215
|
|
|
null, |
|
216
|
|
|
201 |
|
217
|
|
|
), |
|
218
|
|
|
$visitor->visit($data) |
|
219
|
|
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return Common\Output\ValueObjectVisitorDispatcher|\PHPUnit_Framework_MockObject_MockObject |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getValueObjectDispatcherMock() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->createMock(ValueObjectVisitorDispatcher::class); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
protected function getGeneratorMock() |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->createMock(Generator::class); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
protected function getVisitorMock() |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->getMockBuilder(Visitor::class) |
|
238
|
|
|
->setMethods(array('visitValueObject')) |
|
239
|
|
|
->setConstructorArgs( |
|
240
|
|
|
array( |
|
241
|
|
|
$this->getGeneratorMock(), |
|
242
|
|
|
$this->getValueObjectDispatcherMock(), |
|
243
|
|
|
) |
|
244
|
|
|
) |
|
245
|
|
|
->getMock(); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.