Passed
Push — master ( edd315...5c8f6b )
by Adrien
07:14
created

DatatransActionTest::providerProcess()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 139
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 88
nc 1
nop 0
dl 0
loc 139
rs 8.2617
c 0
b 0
f 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\Action;
6
7
use Application\Action\DatatransAction;
8
use ApplicationTest\Traits\TestWithTransaction;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Zend\Diactoros\ServerRequest;
12
use Zend\Expressive\Template\TemplateRendererInterface;
13
14
class DatatransActionTest extends TestCase
15
{
16
    use TestWithTransaction;
17
18
    /**
19
     * @dataProvider providerProcess
20
     */
21
    public function testProcess(?array $data, string $expectedAmount, array $expectedViewModel): void
22
    {
23
        // Message always include input data
24
        $expectedViewModel['message']['detail'] = $data;
25
        $renderer = $this->prophesize(TemplateRendererInterface::class); //; $container->get(TemplateRendererInterface::class);
26
        $renderer->render('app::datatrans', $expectedViewModel);
27
28
        $handler = $this->prophesize(RequestHandlerInterface::class);
29
30
        $request = new ServerRequest();
31
        $request = $request->withParsedBody($data);
32
33
        $action = new DatatransAction(_em(), $renderer->reveal());
34
        $action->process($request, $handler->reveal());
35
36
        $userId = $data['refno'] ?? null;
37
        if ($userId) {
38
            $actualBalance = _em()->getConnection()->fetchColumn('SELECT balance FROM account WHERE owner_id = ' . $userId);
39
            self::assertSame($expectedAmount, $actualBalance);
40
        }
41
42
        self::assertTrue(true); // Workaround when we only assert via prophesize
43
    }
44
45
    public function providerProcess(): array
46
    {
47
        return [
48
            'normal' => [
49
                [
50
                    'uppTransactionId' => '123456789012345678',
51
                    'status' => 'success',
52
                    'refno' => '1007',
53
                    'amount' => '10000',
54
                    'currency' => 'CHF',
55
                    'responseMessage' => 'Payment was successful',
56
                ],
57
                '100.00',
58
                [
59
                    'message' => [
60
                        'status' => 'success',
61
                        'message' => 'Payment was successful',
62
                    ],
63
                ],
64
            ],
65
            'user without account yet' => [
66
                [
67
                    'uppTransactionId' => '123456789012345678',
68
                    'status' => 'success',
69
                    'refno' => '1008',
70
                    'amount' => '10000',
71
                    'currency' => 'CHF',
72
                    'responseMessage' => 'Payment was successful',
73
                ],
74
                '100.00',
75
                [
76
                    'message' => [
77
                        'status' => 'success',
78
                        'message' => 'Payment was successful',
79
                    ],
80
                ],
81
            ],
82
            'error' => [
83
                [
84
                    'uppTransactionId' => '876543210987654321',
85
                    'status' => 'error',
86
                    'refno' => '1007',
87
                    'errorMessage' => 'Dear Sir/Madam, Fire! fire! help me! All the best, Maurice Moss.',
88
                ],
89
                '0.00',
90
                [
91
                    'message' => [
92
                        'status' => 'error',
93
                        'message' => 'Dear Sir/Madam, Fire! fire! help me! All the best, Maurice Moss.',
94
                    ],
95
                ],
96
            ],
97
            'cancel' => [
98
                [
99
                    'uppTransactionId' => '876543210987654321',
100
                    'status' => 'cancel',
101
                    'refno' => '1007',
102
                ],
103
                '0.00',
104
                [
105
                    'message' => [
106
                        'status' => 'cancel',
107
                        'message' => 'Cancelled',
108
                    ],
109
                ],
110
            ],
111
            'invalid body' => [
112
                null,
113
                '0.00',
114
                [
115
                    'message' => [
116
                        'status' => 'error',
117
                        'message' => 'Parsed body is expected to be an array but got: NULL',
118
                    ],
119
                ],
120
            ],
121
            'invalid status' => [
122
                [
123
                    'uppTransactionId' => '123456789012345678',
124
                    'status' => 'non-existing-status',
125
                    'refno' => '1007',
126
                    'amount' => '10000',
127
                    'currency' => 'CHF',
128
                    'responseMessage' => 'Payment was successful',
129
                ],
130
                '0.00',
131
                [
132
                    'message' => [
133
                        'status' => 'error',
134
                        'message' => 'Unsupported status in Datatrans data: non-existing-status',
135
                    ],
136
                ],
137
            ],
138
            'non-existing user' => [
139
                [
140
                    'uppTransactionId' => '123456789012345678',
141
                    'status' => 'success',
142
                    'amount' => '10000',
143
                    'currency' => 'CHF',
144
                    'responseMessage' => 'Payment was successful',
145
                ],
146
                '0.00',
147
                [
148
                    'message' => [
149
                        'status' => 'error',
150
                        'message' => 'Cannot create transactions without a user',
151
                    ],
152
                ],
153
            ],
154
            'non-existing amount' => [
155
                [
156
                    'uppTransactionId' => '123456789012345678',
157
                    'status' => 'success',
158
                    'refno' => '1007',
159
                    'currency' => 'CHF',
160
                    'responseMessage' => 'Payment was successful',
161
                ],
162
                '0.00',
163
                [
164
                    'message' => [
165
                        'status' => 'error',
166
                        'message' => 'Cannot create transactions without an amount',
167
                    ],
168
                ],
169
            ],
170
            'invalid currency' => [
171
                [
172
                    'uppTransactionId' => '123456789012345678',
173
                    'status' => 'success',
174
                    'refno' => '1007',
175
                    'amount' => '10000',
176
                    'currency' => 'USD',
177
                    'responseMessage' => 'Payment was successful',
178
                ],
179
                '0.00',
180
                [
181
                    'message' => [
182
                        'status' => 'error',
183
                        'message' => 'Can only create transactions for CHF, but got: USD',
184
                    ],
185
                ],
186
            ],
187
        ];
188
    }
189
}
190