OrdersRestApiCest::authorizeCustomer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace PyzTest\Glue\Orders\RestApi;
11
12
use Codeception\Util\HttpCode;
13
use Generated\Shared\Transfer\CustomerTransfer;
14
use PyzTest\Glue\Orders\OrdersApiTester;
15
use Spryker\Glue\OrdersRestApi\OrdersRestApiConfig;
16
17
/**
18
 * Auto-generated group annotations
19
 *
20
 * @group PyzTest
21
 * @group Glue
22
 * @group Orders
23
 * @group RestApi
24
 * @group OrdersRestApiCest
25
 * Add your own group annotations below this line
26
 * @group EndToEnd
27
 */
28
class OrdersRestApiCest
29
{
30
    /**
31
     * @var \PyzTest\Glue\Orders\RestApi\OrdersRestApiFixtures
32
     */
33
    protected OrdersRestApiFixtures $fixtures;
34
35
    /**
36
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
37
     *
38
     * @return void
39
     */
40
    public function loadFixtures(OrdersApiTester $I): void
41
    {
42
        /** @var \PyzTest\Glue\Orders\RestApi\OrdersRestApiFixtures $fixtures */
43
        $fixtures = $I->loadFixtures(OrdersRestApiFixtures::class);
44
        $this->fixtures = $fixtures;
45
    }
46
47
    /**
48
     * @depends loadFixtures
49
     *
50
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
51
     *
52
     * @return void
53
     */
54
    public function requestGetEmptyListOfOrders(OrdersApiTester $I): void
55
    {
56
        // Arrange
57
        $this->authorizeCustomer($I, $this->fixtures->getCustomerWithoutOrders());
58
59
        // Act
60
        $I->sendGET(
61
            $I->formatUrl(OrdersRestApiConfig::RESOURCE_ORDERS),
62
        );
63
64
        // Assert
65
        $I->seeResponseCodeIs(HttpCode::OK);
66
        $I->seeResponseIsJson();
67
        $I->seeResponseMatchesOpenApiSchema();
68
69
        $I->amSure('The returned resource contains empty collection')
70
            ->whenI()
71
            ->seeResponseDataContainsEmptyCollection();
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsEmptyCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsEmptyCollection();
Loading history...
72
73
        $I->amSure('The returned resource has correct self-link')
74
            ->whenI()
75
            ->seeResponseLinksContainsSelfLink(
0 ignored issues
show
Bug introduced by
It seems like seeResponseLinksContainsSelfLink() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            ->/** @scrutinizer ignore-call */ seeResponseLinksContainsSelfLink(
Loading history...
76
                $I->formatFullUrl(OrdersRestApiConfig::RESOURCE_ORDERS),
77
            );
78
    }
79
80
    /**
81
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
82
     *
83
     * @return void
84
     */
85
    public function requestGetListOfOrdersWithSingleOrder(OrdersApiTester $I): void
86
    {
87
        // Arrange
88
        $this->authorizeCustomer($I, $this->fixtures->getCustomerWithOrders());
89
90
        // Act
91
        $I->sendGET(
92
            $I->formatUrl(OrdersRestApiConfig::RESOURCE_ORDERS),
93
        );
94
95
        // Assert
96
        $I->seeResponseCodeIs(HttpCode::OK);
97
        $I->seeResponseIsJson();
98
        $I->seeResponseMatchesOpenApiSchema();
99
100
        $I->amSure('The returned resource has correct self-link')
101
            ->whenI()
102
            ->seeResponseLinksContainsSelfLink(
103
                $I->formatFullUrl(OrdersRestApiConfig::RESOURCE_ORDERS),
104
            );
105
106
        $I->amSure('The returned resource has not empty collection')
107
            ->whenI()
108
            ->seeResponseDataContainsNonEmptyCollection();
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsNonEmptyCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsNonEmptyCollection();
Loading history...
109
110
        $I->amSure('The returned resource has correct resource collection type')
111
            ->whenI()
112
            ->seeResponseDataContainsResourceCollectionOfType('orders');
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsResourceCollectionOfType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsResourceCollectionOfType('orders');
Loading history...
113
114
        $I->amSure('The returned resource has correct size')
115
            ->whenI()
116
            ->seeResponseDataContainsResourceCollectionOfTypeWithSizeOf('orders', 1);
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsR...ctionOfTypeWithSizeOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsResourceCollectionOfTypeWithSizeOf('orders', 1);
Loading history...
117
    }
118
119
    /**
120
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
121
     *
122
     * @return void
123
     */
124
    public function requestGetOrderDetails(OrdersApiTester $I): void
125
    {
126
        // Arrange
127
        $this->authorizeCustomer($I, $this->fixtures->getCustomerWithOrders());
128
        $orderReference = $this->fixtures->geSaveOrderTransfer()->getOrderReference();
129
130
        // Act
131
        $I->sendGET(
132
            $I->formatUrl(
133
                '{orders}/{customerOrderReference}',
134
                [
135
                    'orders' => OrdersRestApiConfig::RESOURCE_ORDERS,
136
                    'customerOrderReference' => $orderReference,
137
                ],
138
            ),
139
        );
140
141
        // Assert
142
        $I->seeResponseCodeIs(HttpCode::OK);
143
        $I->seeResponseIsJson();
144
        $I->seeResponseMatchesOpenApiSchema();
145
146
        $I->amSure('The returned resource is of correct type')
147
            ->whenI()
148
            ->seeResponseDataContainsSingleResourceOfType('orders');
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsSingleResourceOfType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsSingleResourceOfType('orders');
Loading history...
149
150
        $I->amSure('The returned resource has correct id')
151
            ->whenI()
152
            ->seeSingleResourceIdEqualTo($orderReference);
0 ignored issues
show
Bug introduced by
It seems like seeSingleResourceIdEqualTo() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
            ->/** @scrutinizer ignore-call */ seeSingleResourceIdEqualTo($orderReference);
Loading history...
153
    }
154
155
    /**
156
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
157
     *
158
     * @return void
159
     */
160
    public function requestGetCustomerOrder(OrdersApiTester $I): void
161
    {
162
        // Arrange
163
        $customerTransfer = $this->fixtures->getCustomerWithOrders();
164
        $this->authorizeCustomer($I, $customerTransfer);
165
166
        // Act
167
        $I->sendGET(
168
            $I->formatUrl(
169
                '{customer}/{customerReference}/{orders}',
170
                [
171
                    'customer' => OrdersRestApiConfig::RESOURCE_CUSTOMERS,
172
                    'customerReference' => $customerTransfer->getCustomerReference(),
173
                    'orders' => OrdersRestApiConfig::RESOURCE_ORDERS,
174
                ],
175
            ),
176
        );
177
178
        // Assert
179
        $I->seeResponseCodeIs(HttpCode::OK);
180
        $I->seeResponseIsJson();
181
        $I->seeResponseMatchesOpenApiSchema();
182
    }
183
184
    /**
185
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
186
     *
187
     * @return void
188
     */
189
    public function requestGetCustomerOrderAuthorizationError(OrdersApiTester $I): void
190
    {
191
        // Arrange
192
        $this->authorizeCustomer($I, $this->fixtures->getCustomerWithOrders());
193
194
        // Act
195
        $I->sendGET(
196
            $I->formatUrl(
197
                '{customer}/{customerReference}/{orders}',
198
                [
199
                    'customer' => OrdersRestApiConfig::RESOURCE_CUSTOMERS,
200
                    'customerReference' => 'wrongCustomerReference',
201
                    'orders' => OrdersRestApiConfig::RESOURCE_ORDERS,
202
                ],
203
            ),
204
        );
205
206
        // Assert
207
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
208
        $I->seeResponseIsJson();
209
        $I->seeResponseMatchesOpenApiSchema();
210
    }
211
212
    /**
213
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
214
     *
215
     * @return void
216
     */
217
    public function requestGetListOfOrderWithoutAuthorizationToken(OrdersApiTester $I): void
218
    {
219
        // Act
220
        $I->sendGET(
221
            $I->formatUrl(OrdersRestApiConfig::RESOURCE_ORDERS),
222
        );
223
224
        // Assert
225
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
226
        $I->seeResponseIsJson();
227
        $I->seeResponseMatchesOpenApiSchema();
228
    }
229
230
    /**
231
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
232
     *
233
     * @return void
234
     */
235
    public function requestGetOrderDetailsWithoutAuthorizationToken(OrdersApiTester $I): void
236
    {
237
        // Arrange
238
        $orderReference = $this->fixtures->geSaveOrderTransfer()->getOrderReference();
239
240
        // Act
241
        $I->sendGET(
242
            $I->formatUrl(
243
                '{orders}/{customerOrderReference}',
244
                [
245
                    'orders' => OrdersRestApiConfig::RESOURCE_ORDERS,
246
                    'customerOrderReference' => $orderReference,
247
                ],
248
            ),
249
        );
250
251
        // Assert
252
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
253
        $I->seeResponseIsJson();
254
        $I->seeResponseMatchesOpenApiSchema();
255
    }
256
257
    /**
258
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
259
     *
260
     * @return void
261
     */
262
    public function requestGetOrderDetailsWithIncorrectOrderReference(OrdersApiTester $I): void
263
    {
264
        // Arrange
265
        $this->authorizeCustomer($I, $this->fixtures->getCustomerWithOrders());
266
267
        // Act
268
        $I->sendGET(
269
            $I->formatUrl(
270
                '{orders}/{customerOrderReference}',
271
                [
272
                    'orders' => OrdersRestApiConfig::RESOURCE_ORDERS,
273
                    'customerOrderReference' => 'test',
274
                ],
275
            ),
276
        );
277
278
        // Assert
279
        $I->seeResponseCodeIs(HttpCode::NOT_FOUND);
280
        $I->seeResponseIsJson();
281
        $I->seeResponseMatchesOpenApiSchema();
282
    }
283
284
    /**
285
     * @param \PyzTest\Glue\Orders\OrdersApiTester $I
286
     * @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
287
     *
288
     * @return void
289
     */
290
    protected function authorizeCustomer(OrdersApiTester $I, CustomerTransfer $customerTransfer): void
291
    {
292
        $token = $I->haveAuthorizationToGlue($customerTransfer)->getAccessToken();
293
294
        $I->amBearerAuthenticated($token);
295
    }
296
}
297