|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the RestContext for RestBundle. |
|
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\Bundle\EzPublishRestBundle\Features\Context; |
|
12
|
|
|
|
|
13
|
|
|
use Behat\Mink\Mink; |
|
14
|
|
|
use Behat\MinkExtension\Context\MinkAwareContext; |
|
15
|
|
|
use eZ\Publish\Core\REST\Client\Values\ErrorMessage; |
|
16
|
|
|
use EzSystems\BehatBundle\Context\Api\Context; |
|
17
|
|
|
use EzSystems\BehatBundle\Helper\Gherkin as GherkinHelper; |
|
18
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
|
19
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
20
|
|
|
use PHPUnit_Framework_Assert as Assertion; |
|
21
|
|
|
use Exception; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* RestContext is the core of the REST testing |
|
25
|
|
|
* All SubContext (traits), helpers are loaded here |
|
26
|
|
|
* Settings and client initializations is done here |
|
27
|
|
|
* Also it contains all REST generic actions. |
|
28
|
|
|
*/ |
|
29
|
|
|
class RestContext extends Context implements MinkAwareContext |
|
30
|
|
|
{ |
|
31
|
|
|
use SubContext\EzRest; |
|
32
|
|
|
use SubContext\Authentication; |
|
33
|
|
|
use SubContext\ContentTypeGroup; |
|
34
|
|
|
use SubContext\Exception; |
|
35
|
|
|
use SubContext\Views; |
|
36
|
|
|
use SubContext\User; |
|
37
|
|
|
|
|
38
|
|
|
const AUTHTYPE_BASICHTTP = 'http_basic'; |
|
39
|
|
|
const AUTHTYPE_SESSION = 'session'; |
|
40
|
|
|
|
|
41
|
|
|
const DEFAULT_URL = 'http://localhost/'; |
|
42
|
|
|
const DEFAULT_DRIVER = 'GuzzleDriver'; |
|
43
|
|
|
const DEFAULT_BODY_TYPE = 'json'; |
|
44
|
|
|
|
|
45
|
|
|
const DEFAULT_AUTH_TYPE = self::AUTHTYPE_SESSION; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Rest driver for all requests and responses. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\DriverInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $restDriver; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $url; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
private $driver; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var \Behat\Mink\Mink |
|
66
|
|
|
*/ |
|
67
|
|
|
private $mink; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
private $minkParameters; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Initialize class. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $url Base URL for REST calls |
|
|
|
|
|
|
78
|
|
|
* @param string $driver REST Driver to be used |
|
79
|
|
|
* @param string $json |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct( |
|
82
|
|
|
$driver = self::DEFAULT_DRIVER, |
|
83
|
|
|
$type = self::DEFAULT_BODY_TYPE, |
|
84
|
|
|
$authType = self::DEFAULT_AUTH_TYPE |
|
85
|
|
|
) { |
|
86
|
|
|
$this->driver = $driver; |
|
87
|
|
|
$this->restBodyType = $type; |
|
88
|
|
|
$this->authType = $authType; |
|
89
|
|
|
|
|
90
|
|
|
$this->setRestDriver($this->driver); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function setUrl($url) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->url = $url; |
|
96
|
|
|
if (isset($this->restDriver)) { |
|
97
|
|
|
$this->restDriver->setHost($this->url); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets Mink instance. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Mink $mink Mink session manager |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setMink(Mink $mink) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->mink = $mink; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Sets parameters provided for Mink. |
|
113
|
|
|
* While at it, take the base_url, and use it to build the one for the REST driver. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $parameters |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setMinkParameters(array $parameters) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->minkParameters = $parameters; |
|
120
|
|
|
$this->setUrl($parameters['base_url'] . '/api/ezp/v2/'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @BeforeScenario |
|
125
|
|
|
*/ |
|
126
|
|
|
private function resetDriver() |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
$this->setRestDriver($this->driver, $this->url); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Create and set the REST driver to be used. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $restDriver REST driver class name |
|
135
|
|
|
*/ |
|
136
|
|
|
private function setRestDriver($restDriver) |
|
137
|
|
|
{ |
|
138
|
|
|
$namespace = '\\' . __NAMESPACE__ . '\\RestClient\\'; |
|
139
|
|
|
$driver = $namespace . $restDriver; |
|
140
|
|
|
$parent = $namespace . 'DriverInterface'; |
|
141
|
|
|
|
|
142
|
|
|
if ( |
|
143
|
|
|
empty($restDriver) |
|
144
|
|
|
|| !class_exists($driver) |
|
145
|
|
|
|| !is_subclass_of($driver, $parent) |
|
|
|
|
|
|
146
|
|
|
) { |
|
147
|
|
|
throw new InvalidArgumentException('rest driver', $driver); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// create a new REST Driver |
|
151
|
|
|
$this->restDriver = new $driver(); |
|
152
|
|
|
if (isset($this->url)) { |
|
153
|
|
|
$this->restDriver->setHost($this->url); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @When I create a :type request to :resource (url) |
|
159
|
|
|
*/ |
|
160
|
|
|
public function createRequest($type, $resource) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->restDriver->setMethod($type); |
|
163
|
|
|
$this->restDriver->setResource( |
|
164
|
|
|
$this->changeMappedValuesOnUrl($resource) |
|
165
|
|
|
); |
|
166
|
|
|
$this->responseObject = null; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @When I send a :type request to :resource (url) |
|
171
|
|
|
*/ |
|
172
|
|
|
public function createAndSendRequest($type, $resource) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->createRequest($type, $resource); |
|
175
|
|
|
$this->restDriver->send(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @When I set :header header with :value (value) |
|
180
|
|
|
*/ |
|
181
|
|
|
public function setHeader($header, $value) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->restDriver->setHeader($header, $value); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @When I set headers: |
|
188
|
|
|
*/ |
|
189
|
|
|
public function setHeaders(TableNode $table) |
|
190
|
|
|
{ |
|
191
|
|
|
$headers = GherkinHelper::convertTableToArrayOfData($table); |
|
192
|
|
|
|
|
193
|
|
|
foreach ($headers as $header => $value) { |
|
|
|
|
|
|
194
|
|
|
$this->iAddHeaderWithValue($header, $value); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @When I send the request |
|
200
|
|
|
*/ |
|
201
|
|
|
public function sendRequest() |
|
202
|
|
|
{ |
|
203
|
|
|
$requestObject = $this->getRequestObject(); |
|
204
|
|
|
if (!empty($requestObject)) { |
|
205
|
|
|
$this->addObjectToRequestBody( |
|
206
|
|
|
$requestObject, |
|
207
|
|
|
$this->restBodyType |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
$this->restDriver->send(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @Then response status code is :code |
|
215
|
|
|
*/ |
|
216
|
|
|
public function assertStatusCode($code) |
|
217
|
|
|
{ |
|
218
|
|
|
$exceptionMessage = ''; |
|
219
|
|
|
if ($code != $this->restDriver->getStatusCode() && $code >= 200 && $code < 400) { |
|
220
|
|
|
$errorMessage = $this->getResponseObject(); |
|
221
|
|
|
if ($errorMessage instanceof ErrorMessage) { |
|
222
|
|
|
$exceptionMessage = <<< EOF |
|
223
|
|
|
|
|
224
|
|
|
Server Error ({$errorMessage->code}): {$errorMessage->description} |
|
225
|
|
|
|
|
226
|
|
|
{$errorMessage->file}:{$errorMessage->line} |
|
227
|
|
|
|
|
228
|
|
|
{$errorMessage->trace} |
|
229
|
|
|
EOF; |
|
230
|
|
|
} elseif ($errorMessage instanceof Exception) { |
|
231
|
|
|
$exceptionMessage = <<< EOF |
|
232
|
|
|
|
|
233
|
|
|
Client Exception ({$errorMessage->getCode()}): {$errorMessage->getMessage()} |
|
234
|
|
|
|
|
235
|
|
|
{$errorMessage->getFile()}:{$errorMessage->getLine()} |
|
236
|
|
|
EOF; |
|
237
|
|
|
// If previous exception is available it is most likely carrying info on server exception. |
|
238
|
|
|
if ($previous = $errorMessage->getPrevious()) { |
|
239
|
|
|
$exceptionName = get_class($previous); |
|
240
|
|
|
$exceptionMessage .= <<< EOF |
|
241
|
|
|
|
|
242
|
|
|
$exceptionName ({$previous->getCode()}): {$previous->getMessage()} |
|
243
|
|
|
|
|
244
|
|
|
{$previous->getFile()}:{$previous->getLine()} |
|
245
|
|
|
|
|
246
|
|
|
{$previous->getTraceAsString()} |
|
247
|
|
|
EOF; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
Assertion::assertEquals( |
|
253
|
|
|
$code, |
|
254
|
|
|
$this->restDriver->getStatusCode(), |
|
255
|
|
|
"Expected status code '$code' found '{$this->restDriver->getStatusCode()}'$exceptionMessage" |
|
256
|
|
|
); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @Then response status message is :message |
|
261
|
|
|
*/ |
|
262
|
|
|
public function assertStatusMessage($message) |
|
263
|
|
|
{ |
|
264
|
|
|
Assertion::assertEquals( |
|
265
|
|
|
strtolower($message), |
|
266
|
|
|
strtolower($this->restDriver->getStatusMessage()), |
|
267
|
|
|
"Expected status message '$message' found '{$this->restDriver->getStatusMessage()}'" |
|
268
|
|
|
); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @Then response header :header exist |
|
273
|
|
|
*/ |
|
274
|
|
|
public function existResponseHeader($header) |
|
275
|
|
|
{ |
|
276
|
|
|
Assertion::assertNotNull( |
|
277
|
|
|
$this->restDriver->getHeader($header), |
|
278
|
|
|
"Expected '$header' header not found" |
|
279
|
|
|
); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @Then response header :header don't exist |
|
284
|
|
|
*/ |
|
285
|
|
|
public function dontExistResponseHeader($header) |
|
286
|
|
|
{ |
|
287
|
|
|
Assertion::assertNull( |
|
288
|
|
|
$this->restDriver->getHeader($header), |
|
289
|
|
|
"Unexpected '$header' header found with '{$this->restDriver->getHeader($header)}' value" |
|
290
|
|
|
); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @Then response header :header have :value (value) |
|
295
|
|
|
*/ |
|
296
|
|
|
public function assertHeaderHaveValue($header, $value) |
|
297
|
|
|
{ |
|
298
|
|
|
Assertion::assertEquals( |
|
299
|
|
|
$value, |
|
300
|
|
|
$this->restDriver->getResponseHeader($header), |
|
|
|
|
|
|
301
|
|
|
"Expected '$header' header with '$value' found it with '{$this->restDriver->getHeader($header)}' value" |
|
302
|
|
|
); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @Then response header :header don't have :value (value) |
|
307
|
|
|
*/ |
|
308
|
|
|
public function assertHeaderDontHaveValue($header, $value) |
|
309
|
|
|
{ |
|
310
|
|
|
Assertion::assertNotEquals( |
|
311
|
|
|
$value, |
|
312
|
|
|
$this->restDriver->getResponseHeader($header), |
|
|
|
|
|
|
313
|
|
|
"Unexpected '$header' header found with '{$this->restDriver->getHeader($header)}' value" |
|
314
|
|
|
); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* @Then response body has :value (value) |
|
319
|
|
|
*/ |
|
320
|
|
|
public function responseBodyHasValue($value) |
|
321
|
|
|
{ |
|
322
|
|
|
Assertion::assertEquals( |
|
323
|
|
|
$value, |
|
324
|
|
|
$this->restDriver->getBody(), |
|
325
|
|
|
"Expected body isn't equal to the actual one." |
|
326
|
|
|
. "\nExpected: " |
|
327
|
|
|
. print_r($value, true) |
|
328
|
|
|
. "\nActual: " |
|
329
|
|
|
. print_r($this->restDriver->getBody(), true) |
|
330
|
|
|
); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.