|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing a test 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\Server\Tests\Output\ValueObjectVisitor; |
|
12
|
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest; |
|
14
|
|
|
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor; |
|
15
|
|
|
use eZ\Publish\Core\REST\Common; |
|
16
|
|
|
|
|
17
|
|
|
class ExceptionTest extends ValueObjectVisitorBaseTest |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Test the Exception visitor. |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testVisit() |
|
25
|
|
|
{ |
|
26
|
|
|
$visitor = $this->getVisitor(); |
|
27
|
|
|
$generator = $this->getGenerator(); |
|
28
|
|
|
|
|
29
|
|
|
$generator->startDocument(null); |
|
30
|
|
|
|
|
31
|
|
|
$previousException = new \Exception('Sub-test'); |
|
32
|
|
|
$exception = new \Exception('Test', 0, $previousException); |
|
33
|
|
|
|
|
34
|
|
|
$this |
|
35
|
|
|
->getVisitorMock() |
|
36
|
|
|
->expects($this->once()) |
|
37
|
|
|
->method('visitValueObject') |
|
38
|
|
|
->with($previousException); |
|
39
|
|
|
|
|
40
|
|
|
$visitor->visit( |
|
41
|
|
|
$this->getVisitorMock(), |
|
|
|
|
|
|
42
|
|
|
$generator, |
|
43
|
|
|
$exception |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
$result = $generator->endDocument(null); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertNotNull($result); |
|
49
|
|
|
|
|
50
|
|
|
return $result; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Test if result contains ErrorMessage element and error code. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $result |
|
57
|
|
|
* |
|
58
|
|
|
* @depends testVisit |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testResultContainsErrorCode($result) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->assertXMLTag( |
|
63
|
|
|
array( |
|
64
|
|
|
'tag' => 'ErrorMessage', |
|
65
|
|
|
'descendant' => array( |
|
66
|
|
|
'tag' => 'errorCode', |
|
67
|
|
|
'content' => (string)$this->getExpectedStatusCode(), |
|
68
|
|
|
), |
|
69
|
|
|
), |
|
70
|
|
|
$result, |
|
71
|
|
|
'Invalid <ErrorMessage> element.' |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Test if result contains ErrorMessage element. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $result |
|
79
|
|
|
* |
|
80
|
|
|
* @depends testVisit |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testResultContainsErrorMessage($result) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertXMLTag( |
|
85
|
|
|
array( |
|
86
|
|
|
'tag' => 'ErrorMessage', |
|
87
|
|
|
'descendant' => array( |
|
88
|
|
|
'tag' => 'errorMessage', |
|
89
|
|
|
'content' => $this->getExpectedMessage(), |
|
90
|
|
|
), |
|
91
|
|
|
), |
|
92
|
|
|
$result, |
|
93
|
|
|
'Invalid <ErrorMessage> element.' |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Test if result contains ErrorMessage element and description. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $result |
|
101
|
|
|
* |
|
102
|
|
|
* @depends testVisit |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testResultContainsErrorDescription($result) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->assertXMLTag( |
|
107
|
|
|
array( |
|
108
|
|
|
'tag' => 'ErrorMessage', |
|
109
|
|
|
'descendant' => array( |
|
110
|
|
|
'tag' => 'errorDescription', |
|
111
|
|
|
), |
|
112
|
|
|
), |
|
113
|
|
|
$result, |
|
114
|
|
|
'Invalid <ErrorMessage> element.' |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Test if ErrorMessage element contains required attributes. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $result |
|
122
|
|
|
* |
|
123
|
|
|
* @depends testVisit |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testResultContainsExceptionAttributes($result) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->assertXMLTag( |
|
128
|
|
|
array( |
|
129
|
|
|
'tag' => 'ErrorMessage', |
|
130
|
|
|
'attributes' => array( |
|
131
|
|
|
'media-type' => 'application/vnd.ez.api.ErrorMessage+xml', |
|
132
|
|
|
), |
|
133
|
|
|
), |
|
134
|
|
|
$result, |
|
135
|
|
|
'Invalid <ErrorMessage> attributes.' |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Test if result contains ErrorMessage element. |
|
141
|
|
|
* |
|
142
|
|
|
* @depends testVisit |
|
143
|
|
|
*/ |
|
144
|
|
|
public function testResultContainsPreviousError($result) |
|
145
|
|
|
{ |
|
146
|
|
|
$dom = new \DOMDocument(); |
|
147
|
|
|
$dom->loadXml($result); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertXPath( |
|
150
|
|
|
$dom, |
|
151
|
|
|
'/ErrorMessage/Previous[@media-type="application/vnd.ez.api.ErrorMessage+xml"]' |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get expected status code. |
|
157
|
|
|
* |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getExpectedStatusCode() |
|
161
|
|
|
{ |
|
162
|
|
|
return 500; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get expected message. |
|
167
|
|
|
* |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function getExpectedMessage() |
|
171
|
|
|
{ |
|
172
|
|
|
return 'Internal Server Error'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Gets the exception. |
|
177
|
|
|
* |
|
178
|
|
|
* @return \Exception |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function getException() |
|
181
|
|
|
{ |
|
182
|
|
|
return new \Exception('Test'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Gets the exception visitor. |
|
187
|
|
|
* |
|
188
|
|
|
* @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\Exception |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function internalGetVisitor() |
|
191
|
|
|
{ |
|
192
|
|
|
return new ValueObjectVisitor\Exception(); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.