CreditCest::createDataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 40
rs 9.44
1
<?php
2
3
use app\fixtures\{
4
    CreditFixture,
5
    CreditWorkLogFixture,
6
    OauthAccessTokensFixture
7
};
8
use Codeception\{Example, Util\HttpCode};
9
use roaresearch\yii2\roa\test\AbstractResourceCest;
10
11
/**
12
 * Cest to stage resource.
13
 *
14
 * @author Carlos (neverabe) Llamosas <[email protected]>
15
 */
16
class CreditCest extends AbstractResourceCest
17
{
18
    protected function authToken(ApiTester $I)
19
    {
20
        $I->amBearerAuthenticated(OauthAccessTokensFixture::SIMPLE_TOKEN);
21
    }
22
23
    /**
24
     * @depends TransitionPermissionCest:fixtures
25
     */
26
    public function fixtures(ApiTester $I)
27
    {
28
        $I->haveFixtures([
29
            'credit' => [
30
                'class' => CreditFixture::class,
31
                'depends' => []
32
            ],
33
            'credit_worklog' => CreditWorkLogFixture::class,
34
        ]);
35
    }
36
37
    /**
38
     * @param  ApiTester $I
39
     * @param  Example $example
40
     * @dataprovider indexDataProvider
41
     * @depends fixtures
42
     * @before authToken
43
     */
44
    public function index(ApiTester $I, Example $example)
45
    {
46
        $I->wantTo('Retrieve list of Credit records.');
47
        $this->internalIndex($I, $example);
48
    }
49
50
    /**
51
     * @return array<string,array> for test `index()`.
52
     */
53
    protected function indexDataProvider()
54
    {
55
        return [
56
            'list' => [
57
                'url' => '/v1/credit',
58
                'httpCode' => HttpCode::OK,
59
                'headers' => [
60
                    'X-Pagination-Total-Count' => 7,
61
                ],
62
            ],
63
            'search integer' => [
64
                'url' => '/v1/credit?activeStage[]=1',
65
                'httpCode' => HttpCode::OK,
66
                'headers' => [
67
                    'X-Pagination-Total-Count' => 5,
68
                ],
69
            ],
70
            'search array' => [
71
                'url' => '/v1/credit?activeStage[]=1&activeStage[]=4',
72
                'httpCode' => HttpCode::OK,
73
                'headers' => [
74
                    'X-Pagination-Total-Count' => 6,
75
                ],
76
            ],
77
            'not found credit' => [
78
                'url' => '/v1/credit/15',
79
                'httpCode' => HttpCode::NOT_FOUND,
80
            ],
81
            'filter by author' => [
82
                'urlParams' => [
83
                    'created_by' => 1,
84
                ],
85
                'httpCode' => HttpCode::OK,
86
                'headers' => [
87
                    'X-Pagination-Total-Count' => 7,
88
                ],
89
            ],
90
            'rule created_by' => [
91
                'urlParams' => [
92
                    'created_by' => 'wo',
93
                ],
94
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
95
            ],
96
        ];
97
    }
98
99
    /**
100
     * @param  ApiTester $I
101
     * @param  Example $example
102
     * @dataprovider viewDataProvider
103
     * @depends fixtures
104
     * @before authToken
105
     */
106
    public function view(ApiTester $I, Example $example)
107
    {
108
        $I->wantTo('Retrieve Credit single record.');
109
        $this->internalView($I, $example);
110
    }
111
112
    /**
113
     * @return array<string,array<string,string>> data for test `view()`.
114
     */
115
    protected function viewDataProvider()
116
    {
117
        return [
118
            'single record' => [
119
                'urlParams' => [
120
                    'id' => 4,
121
                    'expand' => 'workLogs, activeWorkLog',
122
                ],
123
                'httpCode' => HttpCode::OK,
124
            ],
125
            'not found credit record' => [
126
                'url' => '/v1/credit/8',
127
                'httpCode' => HttpCode::NOT_FOUND,
128
            ],
129
        ];
130
    }
131
132
    /**
133
     * @param  ApiTester $I
134
     * @param  Example $example
135
     * @dataprovider createDataProvider
136
     * @depends CreditWorkLogCest:fixtures
137
     * @before authToken
138
     */
139
    public function create(ApiTester $I, Example $example)
140
    {
141
        $I->wantTo('Create a Credit record.');
142
        $this->internalCreate($I, $example);
143
        if (HttpCode::CREATED == $example['httpCode']) {
144
            $worklogRoute = $I->grabDataFromResponseByJsonPath(
145
                '$._links.worklog.href'
146
            )[0];
147
            $I->sendGET($worklogRoute);
148
            $I->seeHTTPHeader('X-PAGINATION-TOTAL-COUNT', 1);
149
            ($example['grabRecord']) ?
150
            $record = $I->grabRecord(
0 ignored issues
show
Unused Code introduced by
The assignment to $record is dead and can be removed.
Loading history...
151
                'app\models\CreditWorkLog',
152
                [
153
                    'comment' => $example['data']['comment']
154
                ]
155
            ) : '';
156
        }
157
    }
158
159
    /**
160
     * @return array<string,array> data for test `create()`.
161
     */
162
    protected function createDataProvider()
163
    {
164
        return [
165
            'create credit 1' => [
166
                'grabRecord' => true,
167
                'data' => [
168
                    'workflow_id' => 2,
169
                    'stage_id' => 4,
170
                    'comment' => 'Proccess Initiated'
171
                ],
172
                'httpCode' => HttpCode::CREATED,
173
            ],
174
            'stage doesn´t belong workflow' => [
175
                'grabRecord' => false,
176
                'data' => [
177
                    'workflow_id' => 1,
178
                    'stage_id' => 4,
179
                ],
180
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
181
                'validationErrors' => [
182
                    'stage_id' => 'Stage "4" does not belong to the workflow.'
183
                ],
184
            ],
185
            'dont exists' => [
186
                'grabRecord' => false,
187
                'data' => [
188
                    'workflow_id' => 123,
189
                    'stage_id' => 2,
190
                ],
191
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
192
                'validationErrors' => [
193
                    'workflow_id' => 'Workflow ID is invalid.',
194
                    'stage_id' => 'Not an initial stage for the workflow.'
195
                ],
196
            ],
197
            'not blank' => [
198
                'grabRecord' => false,
199
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
200
                'validationErrors' => [
201
                    'workflow_id' => 'Workflow ID cannot be blank.'
202
                ],
203
            ],
204
        ];
205
    }
206
207
    /**
208
     * @param  ApiTester $I
209
     * @param  Example $example
210
     * @dataprovider updateDataProvider
211
     * @depends fixtures
212
     * @before authToken
213
     */
214
    public function update(ApiTester $I, Example $example)
215
    {
216
        $I->wantTo('Update a Credit record.');
217
        $this->internalUpdate($I, $example);
218
    }
219
220
    /**
221
     * @return array<string,array> data for test `update()`.
222
     */
223
    protected function updateDataProvider()
224
    {
225
        return [
226
            'update credit 1' => [
227
                'url' => '/v1/credit/1',
228
                'data' => ['workflow_id' => 2],
229
                'httpCode' => HttpCode::OK,
230
            ],
231
            'update credit 10' => [
232
                'url' => '/v1/credit/10',
233
                'data' => ['workflow_id' => 2],
234
                'httpCode' => HttpCode::NOT_FOUND,
235
            ],
236
            'not exists' => [
237
                'url' => '/v1/credit/1',
238
                'data' => ['workflow_id' => 123],
239
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
240
                'validationErrors' => [
241
                    'workflow_id' => 'Workflow ID is invalid.',
242
                ],
243
            ],
244
            'not blank' => [
245
                'url' => '/v1/credit/1',
246
                'data' => ['workflow_id' => ''],
247
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
248
                'validationErrors' => [
249
                    'workflow_id' => 'Workflow ID cannot be blank.'
250
                ],
251
            ],
252
        ];
253
    }
254
255
    /**
256
     * @param  ApiTester $I
257
     * @param  Example $example
258
     * @dataprovider deleteDataProvider
259
     * @depends CreditWorkLogCest:create
260
     * @before authToken
261
     */
262
    public function delete(ApiTester $I, Example $example)
263
    {
264
        $I->wantTo('Delete a Credit record.');
265
        $this->internalDelete($I, $example);
266
    }
267
268
    /**
269
     * @return array[] data for test `delete()`.
270
     */
271
    protected function deleteDataProvider()
272
    {
273
        return [
274
            'credit not found' => [
275
                'url' => '/v1/credit/10',
276
                'httpCode' => HttpCode::NOT_FOUND,
277
            ],
278
            'delete credit 5' => [
279
                'url' => '/v1/credit/5',
280
                'httpCode' => HttpCode::NO_CONTENT,
281
            ],
282
            'not found' => [
283
                'url' => '/v1/credit/5',
284
                'httpCode' => HttpCode::NOT_FOUND,
285
                'validationErrors' => [
286
                    'name' => 'The record "5" does not exists.'
287
                ],
288
            ],
289
        ];
290
    }
291
    /**
292
     * @inheritdoc
293
     */
294
    protected function recordJsonType(): array
295
    {
296
        return [
297
            'id' => 'integer:>0',
298
        ];
299
    }
300
301
    /**
302
     * @inheritdoc
303
     */
304
    protected function getRoutePattern(): string
305
    {
306
        return 'v1/credit';
307
    }
308
}
309