Completed
Push — master ( 6da558...92dcf1 )
by Łukasz
59:43 queued 31:01
created

RestContext::getRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
namespace eZ\Bundle\EzPublishRestBundle\Features\Context;
10
11
use Behat\Mink\Mink;
12
use Behat\MinkExtension\Context\RawMinkContext;
13
use Behat\Symfony2Extension\Context\KernelDictionary;
14
use eZ\Publish\API\Repository\Repository;
15
use eZ\Publish\Core\REST\Client\Values\ErrorMessage;
16
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
17
use EzSystems\Behat\API\Facade\UserFacade;
18
use PHPUnit\Framework\Assert as Assertion;
19
use Exception;
20
21
/**
22
 * RestContext is the core of the REST testing
23
 *   All SubContext (traits), helpers are loaded here
24
 *   Settings and client initializations is done here
25
 *   Also it contains all REST generic actions.
26
 */
27
class RestContext extends RawMinkContext
28
{
29
    use SubContext\EzRest;
30
    use SubContext\Authentication;
31
    use SubContext\ContentTypeGroup;
32
    use SubContext\Exception;
33
    use SubContext\Views;
34
    use SubContext\User;
35
    use KernelDictionary;
36
37
    const AUTHTYPE_BASICHTTP = 'http_basic';
38
    const AUTHTYPE_SESSION = 'session';
39
40
    const DEFAULT_URL = 'http://localhost/';
41
    const DEFAULT_DRIVER = 'GuzzleDriver';
42
    const DEFAULT_BODY_TYPE = 'json';
43
44
    const DEFAULT_AUTH_TYPE = self::AUTHTYPE_SESSION;
45
46
    /**
47
     * Rest driver for all requests and responses.
48
     *
49
     * @var \eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\DriverInterface
50
     */
51
    protected $restDriver;
52
53
    /**
54
     * @var string
55
     */
56
    private $url;
57
58
    /**
59
     * @var string
60
     */
61
    private $driver;
62
63
    /**
64
     * @var \Behat\Mink\Mink
65
     */
66
    private $mink;
67
68
    /**
69
     * @var array
70
     */
71
    private $minkParameters;
72
73
    /**
74
     * @var \EzSystems\Behat\API\Facade\UserFacade
75
     */
76
    private $userFacade;
77
78
    /**
79
     * @var \eZ\Publish\API\Repository\Repository
80
     */
81
    private $repository;
82
83
    /**
84
     * Initialize class.
85
     *
86
     * @param string $url    Base URL for REST calls
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
87
     * @param string $driver REST Driver to be used
88
     * @param string $json
0 ignored issues
show
Bug introduced by
There is no parameter named $json. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
89
     */
90
    public function __construct(
91
        UserFacade $userFacade,
92
        $driver = self::DEFAULT_DRIVER,
93
        $type = self::DEFAULT_BODY_TYPE,
94
        $authType = self::DEFAULT_AUTH_TYPE
95
    ) {
96
        $this->userFacade = $userFacade;
97
        $this->driver = $driver;
98
        $this->restBodyType = $type;
99
        $this->authType = $authType;
100
101
        $this->setRestDriver($this->driver);
102
    }
103
104
    private function setUrl($url)
105
    {
106
        $this->url = $url;
107
        if (isset($this->restDriver)) {
108
            $this->restDriver->setHost($this->url);
109
        }
110
    }
111
112
    /**
113
     * Sets Mink instance.
114
     *
115
     * @param Mink $mink Mink session manager
116
     */
117
    public function setMink(Mink $mink)
118
    {
119
        $this->mink = $mink;
120
    }
121
122
    /**
123
     * Sets parameters provided for Mink.
124
     * While at it, take the base_url, and use it to build the one for the REST driver.
125
     *
126
     * @param array $parameters
127
     */
128
    public function setMinkParameters(array $parameters)
129
    {
130
        $this->minkParameters = $parameters;
131
        $this->setUrl($parameters['base_url'] . '/api/ezp/v2/');
132
    }
133
134
    /**
135
     * @BeforeScenario
136
     */
137
    private function resetDriver()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
138
    {
139
        $this->setRestDriver($this->driver, $this->url);
140
    }
141
142
    /**
143
     * Create and set the REST driver to be used.
144
     *
145
     * @param string $restDriver REST driver class name
146
     */
147
    private function setRestDriver($restDriver)
148
    {
149
        $namespace = '\\' . __NAMESPACE__ . '\\RestClient\\';
150
        $driver = $namespace . $restDriver;
151
        $parent = $namespace . 'DriverInterface';
152
153
        if (
154
            empty($restDriver)
155
            || !class_exists($driver)
156
            || !is_subclass_of($driver, $parent)
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $parent can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
157
        ) {
158
            throw new InvalidArgumentException('rest driver', $driver);
159
        }
160
161
        // create a new REST Driver
162
        $this->restDriver = new $driver();
163
        if (isset($this->url)) {
164
            $this->restDriver->setHost($this->url);
165
        }
166
    }
167
168
    /**
169
     * @When I create a :type request to :resource (url)
170
     */
171
    public function createRequest($type, $resource)
172
    {
173
        $this->restDriver->setMethod($type);
174
        $this->restDriver->setResource($resource);
175
        $this->responseObject = null;
176
    }
177
178
    /**
179
     * @When I send a :type request to :resource (url)
180
     */
181
    public function createAndSendRequest($type, $resource)
182
    {
183
        $this->createRequest($type, $resource);
184
        $this->restDriver->send();
185
    }
186
187
    /**
188
     * @When I set :header header with :value (value)
189
     */
190
    public function setHeader($header, $value)
191
    {
192
        $this->restDriver->setHeader($header, $value);
193
    }
194
195
    /**
196
     * @When I send the request
197
     */
198
    public function sendRequest()
199
    {
200
        $requestObject = $this->getRequestObject();
201
        if (!empty($requestObject)) {
202
            $this->addObjectToRequestBody(
203
                $requestObject,
204
                $this->restBodyType
205
            );
206
        }
207
        $this->restDriver->send();
208
    }
209
210
    /**
211
     * @Then response status code is :code
212
     */
213
    public function assertStatusCode($code)
214
    {
215
        $exceptionMessage = '';
216
        if ($code != $this->restDriver->getStatusCode() && $code >= 200 && $code < 400) {
217
            $errorMessage = $this->getResponseObject();
218
            if ($errorMessage instanceof ErrorMessage) {
219
                $exceptionMessage = <<< EOF
220
221
Server Error ({$errorMessage->code}): {$errorMessage->message}
222
223
{$errorMessage->description}
224
225
In {$errorMessage->file}:{$errorMessage->line}
226
227
{$errorMessage->trace}
228
EOF;
229
            } elseif ($errorMessage instanceof Exception) {
230
                $exceptionMessage = <<< EOF
231
232
Client Exception ({$errorMessage->getCode()}): {$errorMessage->getMessage()}
233
234
In {$errorMessage->getFile()}:{$errorMessage->getLine()}
235
EOF;
236
                // If previous exception is available it is most likely carrying info on server exception.
237
                if ($previous = $errorMessage->getPrevious()) {
238
                    $exceptionName = get_class($previous);
239
                    $exceptionMessage .= <<< EOF
240
241
Previous Exception $exceptionName ({$previous->getCode()}): {$previous->getMessage()}
242
243
In {$previous->getFile()}:{$previous->getLine()}
244
245
{$previous->getTraceAsString()}
246
EOF;
247
                }
248
            }
249
        }
250
251
        Assertion::assertEquals(
252
            $code,
253
            $this->restDriver->getStatusCode(),
254
            "Expected status code '$code' found '{$this->restDriver->getStatusCode()}'$exceptionMessage"
255
        );
256
    }
257
258
    /**
259
     * @Then response status message is :message
260
     */
261
    public function assertStatusMessage($message)
262
    {
263
        Assertion::assertEquals(
264
            strtolower($message),
265
            strtolower($this->restDriver->getStatusMessage()),
266
            "Expected status message '$message' found '{$this->restDriver->getStatusMessage()}'"
267
        );
268
    }
269
270
    /**
271
     * @Then response header :header exist
272
     */
273
    public function existResponseHeader($header)
274
    {
275
        Assertion::assertNotNull(
276
            $this->restDriver->getHeader($header),
277
            "Expected '$header' header not found"
278
        );
279
    }
280
281
    /**
282
     * @Then response header :header don't exist
283
     */
284
    public function dontExistResponseHeader($header)
285
    {
286
        Assertion::assertNull(
287
            $this->restDriver->getHeader($header),
288
            "Unexpected '$header' header found with '{$this->restDriver->getHeader($header)}' value"
289
        );
290
    }
291
292
    /**
293
     * @Then response header :header have :value (value)
294
     */
295
    public function assertHeaderHaveValue($header, $value)
296
    {
297
        Assertion::assertEquals(
298
            $value,
299
            $this->restDriver->getResponseHeader($header),
0 ignored issues
show
Bug introduced by
The method getResponseHeader() does not seem to exist on object<eZ\Bundle\EzPubli...Client\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
300
            "Expected '$header' header with '$value' found it with '{$this->restDriver->getHeader($header)}' value"
301
        );
302
    }
303
304
    /**
305
     * @Then response header :header don't have :value (value)
306
     */
307
    public function assertHeaderDontHaveValue($header, $value)
308
    {
309
        Assertion::assertNotEquals(
310
            $value,
311
            $this->restDriver->getResponseHeader($header),
0 ignored issues
show
Bug introduced by
The method getResponseHeader() does not seem to exist on object<eZ\Bundle\EzPubli...Client\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
312
            "Unexpected '$header' header found with '{$this->restDriver->getHeader($header)}' value"
313
        );
314
    }
315
316
    /**
317
     * @Then response body has :value (value)
318
     */
319
    public function responseBodyHasValue($value)
320
    {
321
        Assertion::assertEquals(
322
            $value,
323
            $this->restDriver->getBody(),
324
            "Expected body isn't equal to the actual one."
325
            . "\nExpected: "
326
            . print_r($value, true)
327
            . "\nActual: "
328
            . print_r($this->restDriver->getBody(), true)
329
        );
330
    }
331
332
    private function getRepository()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
333
    {
334
        if ($this->repository === null) {
335
            $this->repository = $this->getContainer()->get('ezpublish.api.repository');
336
        }
337
338
        return $this->repository;
339
    }
340
}
341