Completed
Pull Request — master (#2)
by Angel
03:03
created

AbstractResourceCest::checkSelfLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace roaresearch\yii2\roa\test;
4
5
use Codeception\{Example, Util\HttpCode};
6
use roaresearch\yii2\roa\urlRules\Resource as ResourceUrlRule;
7
use yii\web\UrlManager;
8
9
abstract class AbstractResourceCest
10
{
11
    /**
12
     * @var UrlManager url manager used to parse Route's for the services.
13
     */
14
    protected $urlManager;
15
16
    /**
17
     * Initializes the `$urlManager` object
18
     */
19
    public function __construct()
20
    {
21
        $this->urlManager = new UrlManager([
22
            'enablePrettyUrl' => true,
23
            'showScriptName' => false,
24
            'cache' => false,
25
            'rules' => [
26
                $this->getRoutePattern() . '/<id:\d+>'=> 'test/action',
27
                $this->getRoutePattern() => 'test/action',
28
            ],
29
        ]);
30
    }
31
32
    /**
33
     * Authenticates a user identified by 'authUser' index in the `$example`.
34
     *
35
     * @param Tester $I
36
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
     * - tokenName: (optional) string name of the token used to authenticate.
38
     */
39
    protected function authUser(Tester $I, Example $example)
40
    {
41
        if (!empty($example['tokenName'])) {
42
            $I->amAuthByToken($example['tokenName']);
43
        }
44
    }
45
46
    /**
47
     * Parses the Route for the test using `$urlManager` and `getRoutePattern()`
48
     *
49
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
50
     * - url: (optional) string if provided it will be used directly.
51
     * - urlParams: (optional) string[] GET params to parse the url rule with
52
     *   `getRoutePattern()`.
53
     * @return string the url which will be used for the service.
54
     */
55
    protected function parseUrl(Example $example): string
56
    {
57
        if (isset($example['url'])) {
58
            return $example['url'];
59
        }
60
        $params = isset($example['urlParams']) ? $example['urlParams'] : [];
61
        $params[0] = 'test/action';
62
63
        return $this->urlManager->createUrl($params);
64
    }
65
66
    /**
67
     * Handles the internal logic when running a test on a collection resource.
68
     *
69
     * @param Tester $I
70
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
71
     * - tokenName: (optional) string name of the token used to authenticate.
72
     * - url: (optional) string if provided it will be used directly.
73
     * - urlParams: (optional) string[] GET params to parse the url rule with
74
     *   `getRoutePattern()`.
75
     * - headers: (optional) string[] pairs of header => value expected in the
76
     *   response.
77
     */
78 View Code Duplication
    protected function internalIndex(Tester $I, Example $example)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        // Authenticates configured user.
81
        $this->authUser($I, $example);
82
83
        // Send request
84
        $I->sendGET($this->parseUrl($example));
0 ignored issues
show
Bug introduced by
The method sendGET() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
85
86
        // Checks the response has the required headers and body.
87
        $this->checkResponse([
88
            HttpCode::OK => 'checkSuccessIndexResponse',
89
            HttpCode::UNPROCESSABLE_ENTITY => 'checkValidationResponse',
90
        ], $I, $example);
91
    }
92
93
    /**
94
     * Handles the internal logic when running a test on a creating a record.
95
     *
96
     * @param Tester $I
97
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
     * - tokenName: (optional) string name of the token used to authenticate.
99
     * - url: (optional) string if provided it will be used directly.
100
     * - urlParams: (optional) string[] GET params to parse the url rule with
101
     *   `getRoutePattern()`.
102
     * - data: (optional) POST data sent on the request.
103
     * - headers: (optional) string[] pairs of header => value expected in the
104
     *   response.
105
     */
106
    protected function internalCreate(Tester $I, Example $example)
107
    {
108
        // Authenticates configured user.
109
        $this->authUser($I, $example);
110
111
        // Send request
112
        $I->sendPOST(
0 ignored issues
show
Bug introduced by
The method sendPOST() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
113
            $this->parseUrl($example),
114
            isset($example['data']) ? $example['data'] : []
115
        );
116
117
        // Checks the response has the required headers and body.
118
        $this->checkResponse([
119
            HttpCode::CREATED => 'checkSuccessCreateResponse',
120
            HttpCode::UNPROCESSABLE_ENTITY => 'checkValidationResponse',
121
        ], $I, $example);
122
    }
123
124
    /**
125
     * Handles the internal logic when running a test on a record resource.
126
     *
127
     * @param Tester $I
128
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
129
     * - tokenName: (optional) string name of the token used to authenticate.
130
     * - url: string if provided it will be used directly.
131
     * - urlParams: string[] GET params to parse the url rule with
132
     *   `getRoutePattern()`.
133
     * - httpCode: integer expected Http Code response. If the httpCode is
134
     *   a key in `$responses` parameter then the method defined there will be
135
     *   used to analyze the response. Otherwise `checkErrorResponse()` will be
136
     *   used.
137
     * - error: string|string[] expected json being contained in the response.
138
     * - headers: (optional) string[] pairs of header => value expected in the
139
     *   response.
140
     */
141 View Code Duplication
    protected function internalView(Tester $I, Example $example)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        // Authenticates configured user.
144
        $this->authUser($I, $example);
145
146
        // Send request
147
        $I->sendGET($this->parseUrl($example));
0 ignored issues
show
Bug introduced by
The method sendGET() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
148
149
        // Checks the response has the required headers and body.
150
        $this->checkResponse([
151
            HttpCode::OK => 'checkSuccessViewResponse',
152
        ], $I, $example);
153
    }
154
155
    /**
156
     * Handles the internal logic when running a test on a updating a record.
157
     *
158
     * @param Tester $I
159
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
160
     * - tokenName: (optional) string name of the token used to authenticate.
161
     * - url: (optional) string if provided it will be used directly.
162
     * - urlParams: (optional) string[] GET params to parse the url rule with
163
     *   `getRoutePattern()`.
164
     * - data: (optional) POST data sent on the request.
165
     * - expected: (optional) expected in the JSON response body.
166
     * - headers: (optional) string[] pairs of header => value expected in the
167
     *   response.
168
     */
169
    protected function internalUpdate(Tester $I, Example $example)
170
    {
171
        // Authenticates configured user.
172
        $this->authUser($I, $example);
173
174
        // Send request
175
        $I->sendPATCH($this->parseUrl($example), $example['data']);
0 ignored issues
show
Bug introduced by
The method sendPATCH() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
176
177
        // Checks the response has the required headers and body.
178
        $this->checkResponse([
179
            HttpCode::OK => 'checkSuccessViewResponse',
180
            HttpCode::UNPROCESSABLE_ENTITY => 'checkValidationResponse',
181
        ], $I, $example);
182
183
        if (isset($example['expected'])) {
184
            $I->seeResponseContainsJson($example['expected']);
0 ignored issues
show
Bug introduced by
The method seeResponseContainsJson() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
185
        }
186
    }
187
188
    /**
189
     * Handles the internal logic when running a test on a updating a record.
190
     *
191
     * @param Tester $I
192
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
193
     * - tokenName: (optional) string name of the token used to authenticate.
194
     * - url: (optional) string if provided it will be used directly.
195
     * - urlParams: (optional) string[] GET params to parse the url rule with
196
     *   `getRoutePattern()`.
197
     * - httpCode: integer expected Http Code response.
198
     */
199
    protected function internalDelete(Tester $I, Example $example)
200
    {
201
        // Authenticates configured user.
202
        $this->authUser($I, $example);
203
204
        // Send request
205
        $I->sendDELETE($this->parseUrl($example));
0 ignored issues
show
Bug introduced by
The method sendDELETE() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
206
207
        // Check response code.
208
        $I->seeResponseCodeIs($example['httpCode']);
0 ignored issues
show
Bug introduced by
The method seeResponseCodeIs() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
209
    }
210
211
    /**
212
     * Checks the response Http code and the response based on the Http code and
213
     * a list of pairs 'httpCode' => 'responseMethod' provided in the
214
     * `$responses` parameter.
215
     *
216
     * If the Http Code in the response doesn't match any of the keys in
217
     * `$responses` then `checkErrorResponse` will be used instead.
218
     *
219
     * @param array $responses pairs of 'httpCode' => 'responseMethod' which
220
     * will determine how to check the response.
221
     * @param Tester $I
222
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
223
     * - httpCode: integer expected Http Code response. If the httpCode is
224
     *   a key in `$responses` parameter then the method defined there will be
225
     *   used to analyze the response. Otherwise `checkErrorResponse()` will be
226
     *   used.
227
     * - error: (optional) mixed expected json being contained in the response.
228
     * - headers: (optional) string[] pairs of header => value expected in the
229
     *   response.
230
     */
231
    protected function checkResponse(
232
        array $responses,
233
        Tester $I,
234
        Example $example
235
    ) {
236
        $I->seeResponseCodeIs($example['httpCode']);
0 ignored issues
show
Bug introduced by
The method seeResponseCodeIs() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
237
        if (isset($responses[$example['httpCode']])) {
238
            $responseMethod = $responses[$example['httpCode']];
239
            $this->$responseMethod($I, $example);
240
        } else {
241
            $this->checkErrorResponse($I, $example);
242
        }
243
        if (isset($example['headers'])) {
244
            foreach ($example['headers'] as $header => $value) {
245
                $I->seeHttpHeader($header, $value);
0 ignored issues
show
Bug introduced by
The method seeHttpHeader() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
246
            }
247
        }
248
    }
249
250
    /**
251
     * Checks an expected success collection response.
252
     *
253
     * @param Tester $I
254
     * @param Example $example
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
255
     */
256
    protected function checkSuccessIndexResponse(Tester $I, Example $example)
0 ignored issues
show
Unused Code introduced by
The parameter $example is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
257
    {
258
        $I->seeResponseMatchesJsonType($this->recordJsonType());
0 ignored issues
show
Bug introduced by
The method seeResponseMatchesJsonType() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
259
        $I->seeContentTypeHttpHeader();
260
        $I->seePaginationHttpHeaders();
261
    }
262
263
    /**
264
     * Checks an expected success record response.
265
     *
266
     * @param Tester $I
267
     * @param Example $example
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
268
     */
269
    protected function checkSuccessViewResponse(Tester $I, Example $example)
0 ignored issues
show
Unused Code introduced by
The parameter $example is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
270
    {
271
        $I->seeResponseMatchesJsonType($this->recordJsonType());
0 ignored issues
show
Bug introduced by
The method seeResponseMatchesJsonType() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
272
        $I->seeContentTypeHttpHeader();
273
    }
274
275
    /**
276
     * Checks an expected success record creation response.
277
     *
278
     * @param Tester $I
279
     * @param Example $example
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
280
     */
281
    protected function checkSuccessCreateResponse(Tester $I, Example $example)
282
    {
283
        $this->checkSuccessViewResponse($I, $example);
284
        $I->seeHttpHeaderOnce('Location');
0 ignored issues
show
Bug introduced by
The method seeHttpHeaderOnce() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
285
        $this->checkSelfLink($I, $I->grabHttpHeader('Location'));
0 ignored issues
show
Bug introduced by
The method grabHttpHeader() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
286
    }
287
288
    /**
289
     * Checks an expected response containing validation errors.
290
     *
291
     * @param Tester $I
292
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
293
     * - validationErrors: string[] (optional) pairs of field => message
294
     *   validation errors.
295
     */
296
    protected function checkValidationResponse(Tester $I, Example $example)
297
    {
298
        $I->seeResponseMatchesJsonType([
0 ignored issues
show
Bug introduced by
The method seeResponseMatchesJsonType() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
299
            'field' => 'string',
300
            'message' => 'string',
301
        ]);
302
        if (empty($example['validationErrors'])) {
303
            return;
304
        }
305
        $expected = [];
306
        foreach ($example['validationErrors'] as $field => $message) {
307
            $expected[] = [
308
                'field' => $field,
309
                'message' => $message,
310
            ];
311
        }
312
        $I->seeResponseContainsJson($expected);
0 ignored issues
show
Bug introduced by
The method seeResponseContainsJson() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
313
314
    }
315
316
    /**
317
     * Checks an expected error response from user input.
318
     *
319
     * @param Tester $I
320
     * @param Example $example data used for the testing. It uses the keys
0 ignored issues
show
Documentation introduced by
Should the type for parameter $example not be \Codeception\Example?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
321
     * - error: (optional) mixed expected json being contained in the response.
322
     */
323
    protected function checkErrorResponse(Tester $I, Example $example)
324
    {
325
        $I->seeResponseMatchesJsonType([
0 ignored issues
show
Bug introduced by
The method seeResponseMatchesJsonType() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
326
            'name' => 'string',
327
            'message' => 'string',
328
            'code' => 'integer',
329
        ]);
330
        if (isset($example['error'])) {
331
            $I->seeResponseContainsJson($example['error']);
0 ignored issues
show
Bug introduced by
The method seeResponseContainsJson() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
332
        }
333
    }
334
335
    /**
336
     * @param Tester $I
337
     * @param string $expected the expected self link.
338
     */
339
    protected function checkSelfLink(Tester $I, string $expected)
340
    {
341
        if ('' == $path = $this->selfLinkPath()) {
342
            return;
343
        }
344
        verify($I->grabDataFromResponseByJsonPath($path))
0 ignored issues
show
Bug introduced by
The method grabDataFromResponseByJsonPath() does not seem to exist on object<roaresearch\yii2\roa\test\Tester>.

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...
345
            ->contains($expected);
346
    }
347
348
    /**
349
     * @return string the path which will be used to obtain the self link of a
350
     * record. Return empty string to skip that check.
351
     */
352
    protected function selfLinkPath(): string
353
    {
354
        return '$._links.self.href';
355
    }
356
357
    /**
358
     * Expected Json Type for each resource.
359
     *
360
     * @return array
361
     * @see http://codeception.com/docs/modules/REST#seeResponseMatchesJsonType
362
     */
363
    abstract protected function recordJsonType(): array;
364
365
    /**
366
     * @return string route pattern to create the
367
     */
368
    abstract protected function getRoutePattern(): string;
369
}
370