Issues (3641)

GlueApplication/_support/GlueApplicationTester.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Glue\GlueApplication;
9
10
use Codeception\Actor;
11
use Codeception\Configuration;
12
use Generated\Shared\Transfer\ApiControllerConfigurationTransfer;
13
use Generated\Shared\Transfer\GlueErrorTransfer;
14
use Generated\Shared\Transfer\GlueRequestTransfer;
15
use Generated\Shared\Transfer\GlueResourceTransfer;
16
use Generated\Shared\Transfer\GlueResponseTransfer;
17
use Spryker\Glue\GlueApplication\GlueApplicationConfig;
18
use Symfony\Component\Finder\Finder;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * @method void wantToTest($text)
23
 * @method void wantTo($text)
24
 * @method void execute($callable)
25
 * @method void expectTo($prediction)
26
 * @method void expect($prediction)
27
 * @method void amGoingTo($argumentation)
28
 * @method void am($role)
29
 * @method void lookForwardTo($achieveValue)
30
 * @method void comment($description)
31
 * @method void pause()
32
 * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
33
 *
34
 * @SuppressWarnings(\SprykerTest\Glue\GlueApplication\PHPMD)
35
 */
36
class GlueApplicationTester extends Actor
37
{
38
    use _generated\GlueApplicationTesterActions;
39
40
    /**
41
     * @var string
42
     */
43
    public const FAKE_APPLICATION = 'FAKE_APPLICATION';
44
45
    /**
46
     * @var string
47
     */
48
    public const FAKE_CONTROLLER = 'FAKE_CONTROLLER';
49
50
    /**
51
     * @var string
52
     */
53
    public const FAKE_METHOD = 'FAKE_METHOD';
54
55
    /**
56
     * @var string
57
     */
58
    public const FAKE_PATH = 'FAKE_PATH';
59
60
    /**
61
     * @var string
62
     */
63
    public const FAKE_PARAMETER_FOO = 'FAKE_PARAMETER_FOO';
64
65
    /**
66
     * @var string
67
     */
68
    public const FAKE_PARAMETER_BAR = 'FAKE_PARAMETER_BAR';
69
70
    /**
71
     * @var string
72
     */
73
    protected const RESPONSE_STATUS = '200';
74
75
    /**
76
     * @var string
77
     */
78
    protected const RESPONSE_CONTENT = 'test';
79
80
    /**
81
     * @var string
82
     */
83
    protected const CONTENT_TYPE = 'application/json';
84
85
    /**
86
     * @var string
87
     */
88
    protected const META_KEY = 'content-type';
89
90
    /**
91
     * @var string
92
     */
93
    protected const PATH = '/foo/foo-id';
94
95
    /**
96
     * @var string
97
     */
98
    protected const ALLOWED_HEADER = 'allowed-header';
99
100
    /**
101
     * @var string
102
     */
103
    protected const RESOURCE_TYPE = 'foo';
104
105
    /**
106
     * @return array<string, array<string, \Generated\Shared\Transfer\ApiControllerConfigurationTransfer>>
107
     */
108
    public function haveApiControllerConfigurationTransfers(): array
109
    {
110
        return [
111
            static::FAKE_APPLICATION => [
112
                sprintf('%s:%s:%s', static::FAKE_CONTROLLER, static::FAKE_PATH, static::FAKE_METHOD) =>
113
                    (new ApiControllerConfigurationTransfer())
114
                        ->setApiApplication(static::FAKE_APPLICATION)
115
                        ->setController(static::FAKE_CONTROLLER)
116
                        ->setMethod(static::FAKE_METHOD)
117
                        ->setPath(static::FAKE_PATH)
118
                        ->setParameters([static::FAKE_PARAMETER_FOO, static::FAKE_PARAMETER_BAR]),
119
            ],
120
        ];
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function removeCacheFile(): void
127
    {
128
        if (file_exists(Configuration::dataDir() . DIRECTORY_SEPARATOR . GlueApplicationConfig::API_CONTROLLER_CACHE_FILENAME)) {
129
            $finder = new Finder();
130
            $finder->in(Configuration::dataDir())->name(GlueApplicationConfig::API_CONTROLLER_CACHE_FILENAME);
131
            if ($finder->count() > 0) {
132
                foreach ($finder as $fileInfo) {
133
                    unlink($fileInfo->getPathname());
134
                }
135
            }
136
        }
137
    }
138
139
    /**
140
     * @return \Generated\Shared\Transfer\GlueRequestTransfer
141
     */
142
    public function createGlueRequestTransfer(): GlueRequestTransfer
143
    {
144
        $glueRequestTransfer = (new GlueRequestTransfer())->setQueryFields([
145
            'include' => 'resource1,resource2',
146
            'fields' => [
147
                'items' => 'att1,att2,att3',
148
            ],
149
            'page' => [
150
                'limit' => 1,
151
                'offset' => 10,
152
            ],
153
            'sort' => 'field1,field2',
154
            'filter' => [
155
                'items.name' => 'item name',
156
            ],
157
        ])
158
            ->setPath(static::PATH)
159
            ->setMeta([static::META_KEY => [static::CONTENT_TYPE]])
160
            ->setResource($this->createGlueResourceTransfer());
161
162
        return $glueRequestTransfer;
163
    }
164
165
    /**
166
     * @return \Generated\Shared\Transfer\GlueResponseTransfer
167
     */
168
    public function createGlueResponseTransfer(): GlueResponseTransfer
169
    {
170
        $glueResponseTransfer = (new GlueResponseTransfer())
171
            ->addResource($this->createGlueResourceTransfer())
172
            ->setStatus(static::RESPONSE_STATUS)
173
            ->setContent(static::RESPONSE_CONTENT);
174
175
        return $glueResponseTransfer;
176
    }
177
178
    /**
179
     * @return \Generated\Shared\Transfer\GlueResponseTransfer
180
     */
181
    public function createErrorGlueResponseTransfer(): GlueResponseTransfer
182
    {
183
        $glueErrorTransfer = (new GlueErrorTransfer())
184
            ->setStatus(Response::HTTP_NOT_FOUND)
185
            ->setCode(GlueApplicationConfig::ERROR_CODE_METHOD_NOT_FOUND)
186
            ->setMessage(GlueApplicationConfig::ERROR_MESSAGE_METHOD_NOT_FOUND);
187
188
        return (new GlueResponseTransfer())
189
            ->setHttpStatus(Response::HTTP_NOT_FOUND)
190
            ->addError($glueErrorTransfer);
191
    }
192
193
    /**
194
     * @return array<mixed>
195
     */
196
    public function createFormattedData(): array
197
    {
198
        return json_decode(trim($this->loadJsonFile()), true);
199
    }
200
201
    /**
202
     * @return array
203
     */
204
    public function createOperation(): array
205
    {
206
        return [
207
            'operationId' => 'get-collection-of-tests',
208
            'summary' => 'Retrieves collection of tests.',
209
            'parameters' => [
210
                [
211
                    '$ref' => '#/components/parameters/acceptLanguage',
212
                ],
213
                [
214
                    'name' => 'q',
215
                    'in' => 'query',
216
                    'description' => 'Description.',
217
                    'required' => true,
218
                    'schema' => [
219
                        'type' => 'string',
220
                    ],
221
                ],
222
            ],
223
            'responses' => [
224
                [
225
                    'description' => 'Expected response to a valid request.',
226
                    'content' => [
227
                        'application/json' => [
228
                            'schema' => [
229
                                '$ref' => '#/components/schemas/TestsRestResponse',
230
                            ],
231
                        ],
232
                        'application/vnd.api+json' => [
233
                            'schema' => [
234
                                '$ref' => '#/components/schemas/TestsCollectionRestResponse',
235
                            ],
236
                        ],
237
                    ],
238
                ],
239
                'default' => [
240
                    'description' => 'Expected response to a bad request.',
241
                    'content' => [
242
                        'application/json' => [
243
                            'schema' => [
244
                                '$ref' => '#/components/schemas/TestsRestResponse',
245
                            ],
246
                        ],
247
                        'application/vnd.api+json' => [
248
                            'schema' => [
249
                                '$ref' => '#/components/schemas/JsonApiErrorMessage',
250
                            ],
251
                        ],
252
                    ],
253
                ],
254
            ],
255
        ];
256
    }
257
258
    /**
259
     * @return \Generated\Shared\Transfer\GlueResourceTransfer
260
     */
261
    protected function createGlueResourceTransfer(): GlueResourceTransfer
262
    {
263
        return (new GlueResourceTransfer())
264
            ->setType('articles')
265
            ->setId('1');
266
    }
267
268
    /**
269
     * @return string
270
     */
271
    protected function loadJsonFile(): string
272
    {
273
        return file_get_contents(codecept_data_dir() . 'schema.json.example');
0 ignored issues
show
The function codecept_data_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

273
        return file_get_contents(/** @scrutinizer ignore-call */ codecept_data_dir() . 'schema.json.example');
Loading history...
274
    }
275
}
276