CreditWorkLogCest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 195
rs 10
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 4 1
A index() 0 4 1
A fixtures() 0 6 1
A authToken() 0 3 1
A indexDataProvider() 0 8 1
A viewDataProvider() 0 9 1
A create() 0 11 2
A permission() 0 12 1
A updateDataProvider() 0 9 1
A recordJsonType() 0 4 1
A createDataProvider() 0 25 1
A getRoutePattern() 0 3 1
A update() 0 4 1
1
<?php
2
3
use app\fixtures\{CreditWorkLogFixture, OauthAccessTokensFixture};
4
use Codeception\{Example, Util\HttpCode};
5
use roaresearch\yii2\roa\test\AbstractResourceCest;
6
7
/**
8
 * Cest to stage resource.
9
 *
10
 * @author Carlos (neverabe) Llamosas <[email protected]>
11
 */
12
class CreditWorkLogCest extends AbstractResourceCest
13
{
14
    protected function authToken(ApiTester $I)
15
    {
16
        $I->amBearerAuthenticated(OauthAccessTokensFixture::SIMPLE_TOKEN);
17
    }
18
19
    /**
20
     * @depends CreditCest:fixtures
21
     */
22
    public function fixtures(ApiTester $I)
23
    {
24
        $I->haveFixtures([
25
            'credit_worklog' => [
26
                'class' => CreditWorkLogFixture::class,
27
                'depends' => [],
28
            ]
29
        ]);
30
    }
31
32
    /**
33
     * @param  ApiTester $I
34
     * @param  Example $example
35
     * @dataprovider indexDataProvider
36
     * @depends fixtures
37
     * @before authToken
38
     */
39
    public function index(ApiTester $I, Example $example)
40
    {
41
        $I->wantTo('Retrieve list of Credit Worklog records.');
42
        $this->internalIndex($I, $example);
43
    }
44
45
    /**
46
     * @return array<string,array> for test `index()`.
47
     */
48
    protected function indexDataProvider()
49
    {
50
        return [
51
            'list' => [
52
                'url' => '/v1/credit/4/worklog',
53
                'httpCode' => HttpCode::OK,
54
                'headers' => [
55
                    'X-Pagination-Total-Count' => 4,
56
                ],
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @param  ApiTester $I
63
     * @param  Example $example
64
     * @dataprovider viewDataProvider
65
     * @depends fixtures
66
     * @before authToken
67
     */
68
    public function view(ApiTester $I, Example $example)
69
    {
70
        $I->wantTo('Retrieve Credit Worklog single record.');
71
        $this->internalView($I, $example);
72
    }
73
74
    /**
75
     * @return array<string,array<string,string>> data for test `view()`.
76
     */
77
    protected function viewDataProvider()
78
    {
79
        return [
80
            'single record' => [
81
                'url' => '/v1/credit/1/worklog/1',
82
                'data' => [
83
                    'expand' => 'process',
84
                ],
85
                'httpCode' => HttpCode::OK,
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * @param  ApiTester $I
92
     * @param  Example $example
93
     * @dataprovider createDataProvider
94
     * @depends fixtures
95
     * @before authToken
96
     */
97
    public function create(ApiTester $I, Example $example)
98
    {
99
        $I->wantTo('Create a Credit Worklog record.');
100
        $this->internalCreate($I, $example);
101
        ($example['grabRecord']) ?
102
        $record = $I->grabRecord(
0 ignored issues
show
Unused Code introduced by
The assignment to $record is dead and can be removed.
Loading history...
103
            'app\models\CreditWorkLog',
104
            [
105
                'comment' => $example['data']['comment']
106
            ]
107
        ) : '';
108
    }
109
110
    /**
111
     * @return array<string,array> data for test `create()`.
112
     */
113
    protected function createDataProvider()
114
    {
115
        return [
116
            'create worklog' => [
117
                'grabRecord' => true,
118
                'urlParams' => [
119
                    'process_id' => 1
120
                ],
121
                'data' => [
122
                    'stage_id' => 5,
123
                    'comment' => 'Stage changed'
124
                ],
125
                'httpCode' => HttpCode::CREATED,
126
            ],
127
            'unprocessable worklog' => [
128
                'grabRecord' => false,
129
                'urlParams' => [
130
                    'process_id' => 1
131
                ],
132
                'data' => [
133
                    'stage_id' => 1
134
                ],
135
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
136
                'validationErrors' => [
137
                    'stage_id' => 'There is no transition for the current stage',
138
                ],
139
            ],
140
        ];
141
    }
142
143
    /**
144
     * @param  ApiTester $I
145
     * @depends create
146
     * @before authToken
147
     */
148
    public function permission(ApiTester $I)
149
    {
150
        $auth = Yii::$app->authManager;
151
        $adminRole = $auth->getRole('admin');
152
        $I->sendPOST('/v1/credit/1/worklog', ['stage_id' => 7]);
153
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
154
155
        $auth->assign($adminRole, 1);
156
        $I->sendPOST('/v1/credit/1/worklog', ['stage_id' => 7]);
157
        $I->seeResponseCodeIs(HttpCode::CREATED);
158
159
        $auth->revoke($adminRole, 1);
160
    }
161
162
    /**
163
     * @param  ApiTester $I
164
     * @param  Example $example
165
     * @dataprovider updateDataProvider
166
     * @depends create
167
     * @before authToken
168
     */
169
    public function update(ApiTester $I, Example $example)
170
    {
171
        $I->wantTo('Update a Credit Worklog record.');
172
        $this->internalUpdate($I, $example);
173
    }
174
175
    /**
176
     * @return array<string,array> data for test `update()`.
177
     */
178
    protected function updateDataProvider()
179
    {
180
        return [
181
            'update credit 1' => [
182
                'url' => '/v1/credit/1/worklog/1',
183
                'data' => [
184
                    'stage_id' => 3
185
                ],
186
                'httpCode' => HttpCode::OK,
187
            ],
188
        ];
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194
    protected function recordJsonType(): array
195
    {
196
        return [
197
            'id' => 'integer:>0',
198
        ];
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    protected function getRoutePattern(): string
205
    {
206
        return 'v1/credit/<process_id:\d+>/worklog';
207
    }
208
}
209