DatatransHandlerTest::providerProcess()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 222
Code Lines 153

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 153
dl 0
loc 222
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Handler;
6
7
use Application\Handler\DatatransHandler;
8
use Application\Model\User;
9
use ApplicationTest\Traits\TestWithTransactionAndUser;
10
use Laminas\Diactoros\ServerRequest;
11
use Mezzio\Template\TemplateRendererInterface;
12
use Money\Money;
13
use PHPUnit\Framework\TestCase;
14
15
class DatatransHandlerTest extends TestCase
16
{
17
    use TestWithTransactionAndUser;
18
19
    /**
20
     * @dataProvider providerProcess
21
     */
22
    public function testProcess(?array $data, ?int $accountId, Money $expectedAmount, array $expectedViewModel): void
23
    {
24
        $userId = $data['refno'] ?? null;
25
        $user = $this->getEntityManager()->getRepository(User::class)->getOneById((int) $userId);
26
        User::setCurrent($user);
27
28
        // Message always include input data
29
        $expectedViewModel['message']['detail'] = $data ?? [];
30
        $renderer = $this->createMock(TemplateRendererInterface::class);
31
        $renderer->expects(self::atLeastOnce())->method('render')->with('app::datatrans', $expectedViewModel)->willReturn('');
32
33
        $request = new ServerRequest();
34
        $request = $request->withParsedBody($data);
35
36
        $config = [
37
            'datatrans' => [
38
                'key' => '1a03b7bcf2752c8c8a1b46616b0c12658d2c7643403e655450bedb7c78bb2d2f659c2ff4e647e4ea72d37ef6745ebda6733c7b859439107069f291cda98f4844',
39
            ],
40
            'accounting' => [
41
                'bankAccountCode' => 1020,
42
            ],
43
        ];
44
45
        $handler = new DatatransHandler($this->getEntityManager(), $renderer, $config);
46
        $handler->handle($request);
47
48
        // Submit the same request again to make sure it is accounted only once
49
        $handler->handle($request);
50
51
        if (is_int($accountId)) {
52
            $actualBalance = $this->getEntityManager()->getConnection()->fetchOne('SELECT balance FROM account WHERE id = ' . $accountId);
53
            self::assertSame($expectedAmount->getAmount(), (string) $actualBalance);
54
        }
55
56
        // @phpstan-ignore-next-line
57
        self::assertTrue(true); // Workaround when we only assert via prophesize
58
    }
59
60
    public function providerProcess(): iterable
61
    {
62
        yield 'normal' => [
63
            [
64
                'uppTransactionId' => '123456789012345678',
65
                'status' => 'success',
66
                'refno' => '1007',
67
                'merchantId' => '123456789',
68
                'amount' => '10000',
69
                'currency' => 'CHF',
70
                'responseMessage' => 'Payment was successful',
71
                'sign' => 'e591bc45430b1a14ad7e1a3a14a8218fb9a5ae944557c96366ec98feae6b17f4',
72
            ],
73
            10096,
74
            Money::CHF(15000),
75
            [
76
                'message' => [
77
                    'status' => 'success',
78
                    'message' => 'Payment was successful',
79
                ],
80
            ],
81
        ];
82
        yield 'duplicate' => [
83
            [
84
                'uppTransactionId' => '123456789012345678',
85
                'status' => 'success',
86
                'refno' => '1007',
87
                'merchantId' => '123456789',
88
                'amount' => '10000',
89
                'currency' => 'CHF',
90
                'responseMessage' => 'Payment was successful',
91
                'sign' => 'e591bc45430b1a14ad7e1a3a14a8218fb9a5ae944557c96366ec98feae6b17f4',
92
            ],
93
            10096,
94
            Money::CHF(15000),
95
            [
96
                'message' => [
97
                    'status' => 'success',
98
                    'message' => 'Payment was successful',
99
                ],
100
            ],
101
        ];
102
        yield 'invalid HMAC signature' => [
103
            [
104
                'uppTransactionId' => '123456789012345678',
105
                'status' => 'success',
106
                'refno' => '1007',
107
                'merchantId' => '123456789',
108
                'amount' => '10000',
109
                'currency' => 'CHF',
110
                'responseMessage' => 'Payment was successful',
111
                'sign' => 'a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0',
112
            ],
113
            10096,
114
            Money::CHF(5000),
115
            [
116
                'message' => [
117
                    'status' => 'error',
118
                    'message' => 'Invalid HMAC signature',
119
                ],
120
            ],
121
        ];
122
        yield 'missing HMAC signature' => [
123
            [
124
                'uppTransactionId' => '123456789012345678',
125
                'status' => 'success',
126
                'refno' => '1007',
127
                'merchantId' => '123456789',
128
                'amount' => '10000',
129
                'currency' => 'CHF',
130
                'responseMessage' => 'Payment was successful',
131
            ],
132
            10096,
133
            Money::CHF(5000),
134
            [
135
                'message' => [
136
                    'status' => 'error',
137
                    'message' => 'Missing HMAC signature',
138
                ],
139
            ],
140
        ];
141
        yield 'user without account yet (child can credit householder)' => [
142
            [
143
                'uppTransactionId' => '123456789012345678',
144
                'status' => 'success',
145
                'refno' => '1008',
146
                'merchantId' => '123456789',
147
                'amount' => '10000',
148
                'currency' => 'CHF',
149
                'responseMessage' => 'Payment was successful',
150
                'sign' => '3005b015945fb625ee25d7d804a65cc17f9dacd4fcba72329d34c8081230c146',
151
            ],
152
            10096,
153
            Money::CHF(15000),
154
            [
155
                'message' => [
156
                    'status' => 'success',
157
                    'message' => 'Payment was successful',
158
                ],
159
            ],
160
        ];
161
        yield 'error' => [
162
            [
163
                'uppTransactionId' => '876543210987654321',
164
                'status' => 'error',
165
                'refno' => '1007',
166
                'merchantId' => '123456789',
167
                'errorMessage' => 'Dear Sir/Madam, Fire! fire! help me! All the best, Maurice Moss.',
168
                'sign' => '38151b15a4c680cccafe9dfff6edb236fa9bf1eeec65799b2720312ae9c4b233',
169
            ],
170
            10096,
171
            Money::CHF(5000),
172
            [
173
                'message' => [
174
                    'status' => 'error',
175
                    'message' => 'Dear Sir/Madam, Fire! fire! help me! All the best, Maurice Moss.',
176
                ],
177
            ],
178
        ];
179
        yield 'cancel' => [
180
            [
181
                'uppTransactionId' => '876543210987654321',
182
                'status' => 'cancel',
183
                'refno' => '1007',
184
                'merchantId' => '123456789',
185
                'sign' => '38151b15a4c680cccafe9dfff6edb236fa9bf1eeec65799b2720312ae9c4b233',
186
            ],
187
            10096,
188
            Money::CHF(5000),
189
            [
190
                'message' => [
191
                    'status' => 'cancel',
192
                    'message' => 'Cancelled',
193
                ],
194
            ],
195
        ];
196
        yield 'invalid body' => [
197
            null,
198
            null,
199
            Money::CHF(0),
200
            [
201
                'message' => [
202
                    'status' => 'error',
203
                    'message' => 'Parsed body is expected to be an array but got: NULL',
204
                ],
205
            ],
206
        ];
207
        yield 'invalid status' => [
208
            [
209
                'uppTransactionId' => '123456789012345678',
210
                'status' => 'non-existing-status',
211
                'refno' => '1007',
212
                'merchantId' => '123456789',
213
                'amount' => '10000',
214
                'currency' => 'CHF',
215
                'responseMessage' => 'Payment was successful',
216
                'sign' => 'e591bc45430b1a14ad7e1a3a14a8218fb9a5ae944557c96366ec98feae6b17f4',
217
            ],
218
            10096,
219
            Money::CHF(5000),
220
            [
221
                'message' => [
222
                    'status' => 'error',
223
                    'message' => 'Unsupported status in Datatrans data: non-existing-status',
224
                ],
225
            ],
226
        ];
227
        yield 'non-existing user' => [
228
            [
229
                'uppTransactionId' => '123456789012345678',
230
                'status' => 'success',
231
                'merchantId' => '123456789',
232
                'amount' => '10000',
233
                'currency' => 'CHF',
234
                'responseMessage' => 'Payment was successful',
235
                'sign' => 'f875347d4c66a4f82717ae88f13812289db79b4c1cab4df4fe1c7fdbaaacff05',
236
            ],
237
            null,
238
            Money::CHF(0),
239
            [
240
                'message' => [
241
                    'status' => 'error',
242
                    'message' => 'Cannot create transactions without a user',
243
                ],
244
            ],
245
        ];
246
        yield 'non-existing amount' => [
247
            [
248
                'uppTransactionId' => '123456789012345678',
249
                'status' => 'success',
250
                'merchantId' => '123456789',
251
                'refno' => '1007',
252
                'currency' => 'CHF',
253
                'responseMessage' => 'Payment was successful',
254
                'sign' => 'e2ca709347d5bec5fd169cd3e1243a95d2eae0abf23a34e26e57698d30b3645d',
255
            ],
256
            10096,
257
            Money::CHF(5000),
258
            [
259
                'message' => [
260
                    'status' => 'error',
261
                    'message' => 'Cannot create transactions without an amount',
262
                ],
263
            ],
264
        ];
265
        yield 'invalid currency' => [
266
            [
267
                'uppTransactionId' => '123456789012345678',
268
                'status' => 'success',
269
                'refno' => '1007',
270
                'merchantId' => '123456789',
271
                'amount' => '10000',
272
                'currency' => 'USD',
273
                'responseMessage' => 'Payment was successful',
274
                'sign' => 'a591b4bb76872f8fcb01f841e4b0cf092ae7c26561e93326243d7f48a9181849',
275
            ],
276
            10096,
277
            Money::CHF(5000),
278
            [
279
                'message' => [
280
                    'status' => 'error',
281
                    'message' => 'Can only create transactions for CHF, but got: USD',
282
                ],
283
            ],
284
        ];
285
    }
286
}
287