Passed
Pull Request — master (#809)
by Denys
06:35
created

requestGetAssetsCollectionWithAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 13
c 1
b 1
f 1
dl 0
loc 23
rs 9.8333
cc 1
nc 1
nop 1
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\SelfServicePortal\RestApi;
11
12
use Codeception\Util\HttpCode;
13
use Generated\Shared\Transfer\RestErrorMessageTransfer;
14
use PyzTest\Glue\SelfServicePortal\RestApi\Fixtures\SspAssetsRestApiFixtures;
15
use PyzTest\Glue\SelfServicePortal\SelfServicePortalApiTester;
16
17
/**
18
 * Auto-generated group annotations
19
 *
20
 * @group PyzTest
21
 * @group Glue
22
 * @group SelfServicePortal
23
 * @group RestApi
24
 * @group SspAssetsRestApiCest
25
 * Add your own group annotations below this line
26
 * @group EndToEnd
27
 */
28
class SspAssetsRestApiCest
29
{
30
    protected const RESOURCE_SSP_ASSETS = 'ssp-assets';
31
32
    protected const RESPONSE_DETAIL_ASSET_NOT_FOUND = 'Asset not found';
33
34
    protected SspAssetsRestApiFixtures $fixtures;
35
36
    public function loadFixtures(SelfServicePortalApiTester $I): void
37
    {
38
        /** @var \PyzTest\Glue\SelfServicePortal\RestApi\Fixtures\SspAssetsRestApiFixtures $fixtures */
39
        $fixtures = $I->loadFixtures(SspAssetsRestApiFixtures::class);
40
        $this->fixtures = $fixtures;
41
    }
42
43
    public function requestGetAssetsCollectionWithAuthorization(SelfServicePortalApiTester $I): void
44
    {
45
        // Arrange
46
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
47
48
        // Act
49
        $I->sendGet($I->getSspAssetsUrl());
50
51
        // Assert
52
        $I->seeResponseCodeIs(HttpCode::OK);
53
        $I->seeResponseIsJson();
54
        $I->seeResponseMatchesOpenApiSchema();
55
56
        $I->amSure('The returned resource is of correct type')
57
            ->whenI()
58
            ->seeResponseDataContainsResourceCollectionOfType(static::RESOURCE_SSP_ASSETS);
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

58
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsResourceCollectionOfType(static::RESOURCE_SSP_ASSETS);
Loading history...
59
60
        $I->seeResponseJsonMatchesJsonPath('$.data[*].attributes.name');
61
        $I->seeResponseJsonMatchesJsonPath('$.data[*].attributes.reference');
62
        $I->seeResponseJsonMatchesJsonPath('$.data[*].attributes.serialNumber');
63
        $I->seeResponseJsonMatchesJsonPath('$.data[*].attributes.externalImageUrl');
64
65
        $I->assertResponseMatchAssetReference($this->fixtures->getAssetTransfer()->getReferenceOrFail());
66
    }
67
68
    public function requestGetSingleAssetWithValidId(SelfServicePortalApiTester $I): void
69
    {
70
        // Arrange
71
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
72
        $assetId = $this->fixtures->getAssetTransfer()->getReferenceOrFail();
73
74
        // Act
75
        $I->sendGet($I->getSspAssetUrl((string)$assetId));
76
77
        // Assert
78
        $I->seeResponseCodeIs(HttpCode::OK);
79
        $I->seeResponseIsJson();
80
        $I->seeResponseMatchesOpenApiSchema();
81
82
        $I->amSure('The returned resource is of correct type')
83
            ->whenI()
84
            ->seeResponseDataContainsSingleResourceOfType(static::RESOURCE_SSP_ASSETS);
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

84
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsSingleResourceOfType(static::RESOURCE_SSP_ASSETS);
Loading history...
85
86
        $I->assertEquals($assetId, $I->getDataFromResponseByJsonPath('$.data.id'));
87
        $I->seeResponseJsonMatchesJsonPath('$.data.attributes.name');
88
        $I->seeResponseJsonMatchesJsonPath('$.data.attributes.reference');
89
        $I->seeResponseJsonMatchesJsonPath('$.data.attributes.serialNumber');
90
        $I->seeResponseJsonMatchesJsonPath('$.data.attributes.externalImageUrl');
91
    }
92
93
    public function requestGetSingleAssetWithInvalidId(SelfServicePortalApiTester $I): void
94
    {
95
        // Arrange
96
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
97
        $invalidAssetId = '999999';
98
99
        // Act
100
        $I->sendGet($I->getSspAssetUrl($invalidAssetId));
101
102
        // Assert
103
        $I->seeResponseCodeIs(HttpCode::NOT_FOUND);
104
        $I->seeResponseIsJson();
105
        $I->seeResponseMatchesOpenApiSchema();
106
107
        $errors = $I->getDataFromResponseByJsonPath('$.errors[0]');
108
        $I->assertEquals($errors[RestErrorMessageTransfer::STATUS], HttpCode::NOT_FOUND);
109
        $I->assertEquals($errors[RestErrorMessageTransfer::DETAIL], static::RESPONSE_DETAIL_ASSET_NOT_FOUND);
110
    }
111
112
    public function requestCreateAssetWithValidData(SelfServicePortalApiTester $I): void
113
    {
114
        // Arrange
115
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
116
117
        $requestPayload = [
118
            'data' => [
119
                'type' => static::RESOURCE_SSP_ASSETS,
120
                'attributes' => [
121
                    'name' => 'New Test Asset from Glue',
122
                    'serialNumber' => 'serialNumberAsset1API',
123
                    'externalImageUrl' => 'https://example.com/new-asset.pdf',
124
                ],
125
            ],
126
        ];
127
128
        // Act
129
        $I->sendPost($I->getSspAssetsUrl(), $requestPayload);
130
131
        // Assert
132
        $I->seeResponseCodeIs(HttpCode::CREATED);
133
        $I->seeResponseIsJson();
134
        $I->seeResponseMatchesOpenApiSchema();
135
136
        $I->amSure('The returned resource is of correct type')
137
            ->whenI()
138
            ->seeResponseDataContainsSingleResourceOfType(static::RESOURCE_SSP_ASSETS);
139
140
        $I->assertEquals('New Test Asset from Glue', $I->getDataFromResponseByJsonPath('$.data.attributes.name'));
141
        $I->assertEquals('serialNumberAsset1API', $I->getDataFromResponseByJsonPath('$.data.attributes.serialNumber'));
142
        $I->assertEquals('https://example.com/new-asset.pdf', $I->getDataFromResponseByJsonPath('$.data.attributes.externalImageUrl'));
143
    }
144
145
    public function requestCreateAssetWithMissingRequiredFields(SelfServicePortalApiTester $I): void
146
    {
147
        // Arrange
148
        $I->authorizeCustomerToGlue($this->fixtures->getCustomerTransfer());
149
150
        $requestPayload = [
151
            'data' => [
152
                'type' => static::RESOURCE_SSP_ASSETS,
153
                'attributes' => [
154
                    // Missing required fields: name
155
                    'serialNumber' => 'serialNumberAsset5API',
156
                ],
157
            ],
158
        ];
159
160
        // Act
161
        $I->sendPost($I->getSspAssetsUrl(), $requestPayload);
162
163
        // Assert
164
        $I->seeResponseCodeIs(HttpCode::UNPROCESSABLE_ENTITY);
165
        $I->seeResponseMatchesOpenApiSchema();
166
167
        $errors = $I->getDataFromResponseByJsonPath('$.errors[0]');
168
        $I->assertEquals(HttpCode::UNPROCESSABLE_ENTITY, $errors[RestErrorMessageTransfer::STATUS]);
169
        $I->assertEquals('Asset name must be provided', $errors[RestErrorMessageTransfer::DETAIL]);
170
    }
171
}
172