Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

EzRest.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext;
8
9 use eZ\Publish\Core\REST\Client\Values\ViewInput;
10 use EzSystems\BehatBundle\Helper\ValueObject as ValueObjectHelper;
11 use eZ\Publish\API\Repository\Values\ValueObject;
12 use eZ\Publish\Core\REST\Server\Values\SessionInput;
13 use eZ\Publish\Core\REST\Common\Message;
14 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
15 use PHPUnit\Framework\Assert as Assertion;
16
20 trait EzRest
21 {
23 private $restBodyType = 'json';
24
31 public $requestObject;
32
39 public $responseObject;
40
44 public function setHeaderWithObject($header, $object)
45 {
46 $value = $this->makeObjectHeader($object);
47 $this->restDriver->setHeader($header, $value);
48 }
49
53 public function assertHeaderHasObject($header, $object)
54 {
55 Assertion::assertEquals(
56 $this->decomposeObjectHeader($this->restDriver->getHeader($header)),
57 $object,
58 $this->restDriver->getBody()
59 );
60 }
61
65 public function assertResponseErrorStatusCode($code)
66 {
67 $errorStatusCode = $this->getResponseError('code');
68
69 Assertion::assertEquals(
70 $code,
71 $errorStatusCode,
72 "Expected '$code' status code found '$errorStatusCode' with message:" . $this->restDriver->getStatusMessage()
73 );
74 }
75
79 public function assertResponseErrorDescription($errorDescriptionRegEx)
80 {
81 $errorDescription = $this->getResponseError('description');
82
83 Assertion::assertEquals(
84 preg_match($errorDescriptionRegEx, $errorDescription),
85 1,
86 "Expected to find a description that matched '$errorDescriptionRegEx' RegEx but found '$errorDescription'"
87 );
88 }
89
95 public function makeObject($type)
96 {
97 $this->createRequestObject($type);
98 }
99
103 public function setFieldToValue($field, $value)
104 {
105 // normally fields are defined in lower camelCase
106 $field = lcfirst($field);
107
108 ValueObjectHelper::setProperty($this->getRequestObject(), $field, $value);
109 }
110
114 public function setFieldToEmptyArray($field)
115 {
116 $this->setFieldToValue($field, []);
117 }
118
122 public function assertObjectFieldHasValue($field, $value)
123 {
124 $responseObject = $this->getResponseObject();
125 $actualValue = ValueObjectHelper::getProperty($responseObject, $field);
126
127 Assertion::assertEquals(
128 $actualValue,
129 $value,
130 "Expected '$field' property to have '$value' value but found '$actualValue' value"
131 );
132 }
133
139 public function assertResponseObject($object)
140 {
141 $responseObject = $this->getResponseObject();
142
143 Assertion::assertTrue(
144 $responseObject instanceof $object,
145 "Expect body object to be an instance of '$object' but got a '" . get_class($responseObject) . "'"
146 );
147 }
148
152 public function iSetTheContentTypeHeaderToInVersion($contentType, $version)
153 {
154 $this->restDriver->setHeader('Content-Type', "$contentType+$this->restBodyType; version=$version");
155 }
156
162 protected function setRestBodyType($type)
163 {
164 $type = strtolower($type);
165 switch ($type) {
166 case 'json':
167 case 'xml':
168 $this->restBodyType = $type;
169 break;
170
171 default:
172 throw new \Exception("REST body type '$type' not suported");
173 }
174 }
175
185 protected function getResponseError($property)
186 {
187 $exception = $this->getResponseObject();
188
189 if (!$exception instanceof \Exception) {
190 throw new InvalidArgumentException('response object', 'is not an exception');
191 }
192
193 switch ($property) {
194 case 'code':
195 return $exception->getCode();
196
197 case 'description':
198 case 'message':
199 return $exception->getMessage();
200 }
201
202 throw new InvalidArgumentException($property, 'is invalid');
203 }
204
208 protected function makeObjectHeader($object)
209 {
210 return 'application/vnd.ez.api.' . $object . '+' . $this->restBodyType;
211 }
212
218 protected function decomposeObjectHeader($header)
219 {
220 preg_match('/application\/vnd\.ez\.api\.(.*)\+(?:json|xml)/i', $header, $match);
221
222 return empty($match) ? false : $match[1];
223 }
224
230 public function getResponseObject()
231 {
232 if (empty($this->responseObject)) {
233 $this->responseObject = $this->convertResponseBodyToObject(
234 $this->restDriver->getBody(),
235 $this->restDriver->getHeader('content-type')
236 );
237 }
238
239 return $this->responseObject;
240 }
241
247 public function getRequestObject()
248 {
249 return $this->requestObject;
250 }
251
258 protected function addObjectToRequestBody(ValueObject $object, $type)
259 {
260 $request = $this->convertObjectTo($object, $type);
261
262 $this->restDriver->setBody($request->getContent());
263 }
264
275 protected function convertObjectTo(ValueObject $object, $type)
276 {
277 $type = strtolower($type);
278 switch ($type) {
279 case 'json':
280 case 'xml':
281 $visitor = $this->getKernel()->getContainer()->get('ezpublish_rest.output.visitor.' . $type);
282 break;
283
284 default:
285 throw new InvalidArgumentException('rest body type', $type);
286 }
287
288 return $visitor->visit($object);
289 }
290
299 public function convertResponseBodyToObject($responseBody, $contentTypeHeader)
300 {
301 try {
302 $this->responseObject = $this->getKernel()->getContainer()->get('ezpublish_rest.input.dispatcher')->parse(
303 new Message(
304 ['Content-Type' => $contentTypeHeader],
305 $responseBody
306 )
307 );
308 } catch (\Exception $e) {
309 // when errors/exceptions popup on form the response we need also to
310 // test/verify them
311 $this->responseObject = $e;
312 }
313
314 return $this->responseObject;
315 }
316
324 protected function createRequestObject($objectType)
325 {
326 $repository = $this->getRepository();
327
328 switch ($objectType) {
329 case 'SessionInput':
330 $this->requestObject = new SessionInput();
331 break;
332 case 'ContentTypeGroupCreateStruct':
333 $this->requestObject = $repository
334 ->getContentTypeService()
335 ->newContentTypeGroupCreateStruct('identifier');
336 break;
337
338 case 'ContentTypeGroupUpdateStruct':
339 $this->requestObject = $repository
340 ->getContentTypeService()
341 ->newContentTypeGroupUpdateStruct();
342 break;
343
344 case 'ViewInput':
345 $this->requestObject = new ViewInput();
346 break;
347
348 default:
349 throw new InvalidArgumentException($objectType, 'type is not defined yet');
350 }
351 }
352 }
eZ\Publish\API\Repository\Values\ValueObject
Definition: API/Repository/Values/ValueObject.php:21
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\REST\Client\Values\ViewInput
Definition: Client/Values/ViewInput.php:12
eZ\Publish\Core\REST\Common\Message
Simple response struct.
Definition: Core/REST/Common/Message.php:13
eZ\Publish\Core\REST\Server\Values\SessionInput
SessionInput view model.
Definition: Server/Values/SessionInput.php:15
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext
Definition: Authentication.php:7
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\addObjectToRequestBody
addObjectToRequestBody(ValueObject $object, $type)
Convert an object and add it to the body/content of the request.
Definition: EzRest.php:258
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\convertObjectTo
convertObjectTo(ValueObject $object, $type)
Convert an object to a request.
Definition: EzRest.php:275
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\createRequestObject
createRequestObject($objectType)
Create an object of the specified type.
Definition: EzRest.php:324
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\getRequestObject
getRequestObject()
Get the request object.
Definition: EzRest.php:247
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\setFieldToEmptyArray
setFieldToEmptyArray($field)
@When I set field :field to an empty array
Definition: EzRest.php:114
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\$requestObject
$requestObject
Definition: EzRest.php:31
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\setFieldToValue
setFieldToValue($field, $value)
@When I set field :field to :value
Definition: EzRest.php:103
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\getResponseError
getResponseError($property)
Get property from the returned Exception.
Definition: EzRest.php:185
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\assertResponseErrorDescription
assertResponseErrorDescription($errorDescriptionRegEx)
@Then response error description is :errorDescriptionRegEx
Definition: EzRest.php:79
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\decomposeObjectHeader
decomposeObjectHeader($header)
Decompose the header to get only the object type of the accept/conten-type headers.
Definition: EzRest.php:218
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\assertObjectFieldHasValue
assertObjectFieldHasValue($field, $value)
@Then response object has field :field with :value
Definition: EzRest.php:122
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\makeObject
makeObject($type)
@When I make a(n) :type object
Definition: EzRest.php:95
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\convertResponseBodyToObject
convertResponseBodyToObject($responseBody, $contentTypeHeader)
Convert the body/content of a response into an object.
Definition: EzRest.php:299
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\setHeaderWithObject
setHeaderWithObject($header, $object)
@When I set header :header with/for :object object
Definition: EzRest.php:44
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\assertResponseObject
assertResponseObject($object)
@Then response has/contains a(n) :object object
Definition: EzRest.php:139
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\setRestBodyType
setRestBodyType($type)
Set body type of requests and responses.
Definition: EzRest.php:162
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\getResponseObject
getResponseObject()
Get the response object (if it's not converted do the conversion also).
Definition: EzRest.php:230
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\assertHeaderHasObject
assertHeaderHasObject($header, $object)
@Then response header :header has/contains :object (object)
Definition: EzRest.php:53
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\$responseObject
$responseObject
Definition: EzRest.php:39
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\EzRest
trait EzRest
EzRest is the responsible to have all the specific REST actions of eZ Publish.
Definition: EzRest.php:21
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\assertResponseErrorStatusCode
assertResponseErrorStatusCode($code)
@Then response error status code is :code
Definition: EzRest.php:65
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\makeObjectHeader
makeObjectHeader($object)
Make content-type/accept header with prefix and type.
Definition: EzRest.php:208
eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext\iSetTheContentTypeHeaderToInVersion
iSetTheContentTypeHeaderToInVersion($contentType, $version)
@When I set the Content-Type header to :contentType in version :version
Definition: EzRest.php:152
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.