Test Failed
Pull Request — master (#2)
by Angel
04:11
created

CreditWorkLogCest::authToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    }
102
103
    /**
104
     * @return array<string,array> data for test `create()`.
105
     */
106
    protected function createDataProvider()
107
    {
108
        return [
109
            'create worklog' => [
110
                'urlParams' => [
111
                    'process_id' => 1
112
                ],
113
                'data' => [
114
                    'stage_id' => 5
115
                ],
116
                'httpCode' => HttpCode::CREATED,
117
            ],
118
            'unprocessable worklog' => [
119
                'urlParams' => [
120
                    'process_id' => 1
121
                ],
122
                'data' => [
123
                    'stage_id' => 1
124
                ],
125
                'httpCode' => HttpCode::UNPROCESSABLE_ENTITY,
126
                'validationErrors' => [
127
                    'stage_id' => 'There is no transition for the current stage',
128
                ],
129
            ],
130
        ];
131
    }
132
133
    /**
134
     * @param  ApiTester $I
135
     * @depends create
136
     * @before authToken
137
     */
138
    public function permission(ApiTester $I)
139
    {
140
        $auth = Yii::$app->authManager;
141
        $adminRole = $auth->getRole('admin');
142
        $I->sendPOST('/v1/credit/1/worklog', ['stage_id' => 7]);
143
        $I->seeResponseCodeIs(HttpCode::FORBIDDEN);
144
145
        $auth->assign($adminRole, 1);
146
        $I->sendPOST('/v1/credit/1/worklog', ['stage_id' => 7]);
147
        $I->seeResponseCodeIs(HttpCode::CREATED);
148
149
        $auth->revoke($adminRole, 1);
150
    }
151
152
    /**
153
     * @param  ApiTester $I
154
     * @param  Example $example
155
     * @dataprovider updateDataProvider
156
     * @depends create
157
     * @before authToken
158
     */
159
    public function update(ApiTester $I, Example $example)
160
    {
161
        $I->wantTo('Update a Credit Worklog record.');
162
        $this->internalUpdate($I, $example);
163
    }
164
165
    /**
166
     * @return array<string,array> data for test `update()`.
167
     */
168
    protected function updateDataProvider()
169
    {
170
        return [
171
            'update credit 1' => [
172
                'url' => '/v1/credit/1/worklog/1',
173
                'data' => [
174
                    'stage_id' => 3
175
                ],
176
                'httpCode' => HttpCode::OK,
177
            ],
178
        ];
179
    }
180
181
    /**
182
     * @inheritdoc
183
     */
184
    protected function recordJsonType(): array
185
    {
186
        return [
187
            'id' => 'integer:>0',
188
        ];
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194
    protected function getRoutePattern(): string
195
    {
196
        return 'v1/credit/<process_id:\d+>/worklog';
197
    }
198
}
199