Passed
Push — master ( 3e5289...db9510 )
by Pavel
02:34
created

PaytureInPayTerminalTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 177
Duplicated Lines 32.77 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 58
loc 177
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testPaymentInit() 0 29 1
A getInitSessionTypes() 0 13 1
A testPaymentCharge() 19 19 1
A testPaymentUnblock() 19 19 1
A testPaymentRefund() 20 20 1
A testPaymentStatus() 0 18 1
A testGetState() 0 23 1
A testCreatingPaymentUrl() 0 7 1
A setUp() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Lamoda\Payture\InPayClient\Tests\Unit;
4
5
use Lamoda\Payture\InPayClient\PaytureInPayTerminal;
6
use Lamoda\Payture\InPayClient\PaytureOperation;
7
use Lamoda\Payture\InPayClient\SessionType;
8
use Lamoda\Payture\InPayClient\TerminalConfiguration;
9
use Lamoda\Payture\InPayClient\TransportInterface;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers \Lamoda\Payture\InPayClient\PaytureInPayTerminal
14
 */
15
final class PaytureInPayTerminalTest extends TestCase
16
{
17
    private $config;
18
    private $transport;
19
20
    /** @var PaytureInPayTerminal */
21
    private $terminal;
22
23
    /**
24
     * @dataProvider getInitSessionTypes
25
     *
26
     * @throws \Lamoda\Payture\InPayClient\Exception\TransportException
27
     */
28
    public function testPaymentInit(SessionType $type, string $data): void
29
    {
30
        $this->transport->expects($this->once())
31
            ->method('request')
32
            ->with(
33
                PaytureOperation::INIT(),
34
                'apim',
35
                [
36
                    'Key' => 'MerchantKey',
37
                    'Data' => $data,
38
                ]
39
            )->willReturn('<Init Success="True" SessionId="external-id"/>');
40
41
        $response = $this->terminal->init(
42
            $type,
43
            'Order-123',
44
            'The order',
45
            10000,
46
            '127.0.0.1',
47
            'https://redirect-me.back/',
48
            'template',
49
            [
50
                'custom_data' => 'value',
51
            ]
52
        );
53
54
        self::assertTrue($response->isSuccess());
55
        self::assertEquals('external-id', $response->getSessionId());
56
    }
57
58
    public function getInitSessionTypes()
59
    {
60
        return [
61
            [
62
                SessionType::PAY(),
63
                'SessionType=Pay;OrderId=Order-123;Amount=10000;IP=127.0.0.1;Product=The+order;Url=https%3A%2F%2Fredirect-me.back%2F;TemplateTag=template;custom_data=value',
64
            ],
65
            [
66
                SessionType::BLOCK(),
67
                'SessionType=Block;OrderId=Order-123;Amount=10000;IP=127.0.0.1;Product=The+order;Url=https%3A%2F%2Fredirect-me.back%2F;TemplateTag=template;custom_data=value',
68
            ],
69
        ];
70
    }
71
72 View Code Duplication
    public function testPaymentCharge(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $this->transport->expects($this->once())
75
            ->method('request')
76
            ->with(
77
                PaytureOperation::CHARGE(),
78
                'apim',
79
                [
80
                    'Key' => 'MerchantKey',
81
                    'OrderId' => 'Order-123',
82
                    'Amount' => 10000,
83
                    'Password' => 'MerchantPassword',
84
                ]
85
            )->willReturn('<Charge Success="True" Amount="10000"/>');
86
87
        $response = $this->terminal->charge('Order-123', 10000);
88
89
        self::assertTrue($response->isSuccess());
90
    }
91
92 View Code Duplication
    public function testPaymentUnblock(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $this->transport->expects($this->once())
95
            ->method('request')
96
            ->with(
97
                PaytureOperation::UNBLOCK(),
98
                'apim',
99
                [
100
                    'Key' => 'MerchantKey',
101
                    'OrderId' => 'Order-123',
102
                    'Amount' => 10000,
103
                    'Password' => 'MerchantPassword',
104
                ]
105
            )->willReturn('<Unblock Success="True"/>');
106
107
        $response = $this->terminal->unblock('Order-123', 10000);
108
109
        self::assertTrue($response->isSuccess());
110
    }
111
112 View Code Duplication
    public function testPaymentRefund(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $this->transport->expects($this->once())
115
            ->method('request')
116
            ->with(
117
                PaytureOperation::REFUND(),
118
                'apim',
119
                [
120
                    'Key' => 'MerchantKey',
121
                    'OrderId' => 'Order-123',
122
                    'Amount' => 6000,
123
                    'Password' => 'MerchantPassword',
124
                ]
125
            )->willReturn('<Refund Success="True" NewAmount="4000"/>');
126
127
        $response = $this->terminal->refund('Order-123', 6000);
128
129
        self::assertTrue($response->isSuccess());
130
        self::assertEquals(4000, $response->getAmount());
131
    }
132
133
    public function testPaymentStatus(): void
134
    {
135
        $this->transport->expects($this->once())
136
            ->method('request')
137
            ->with(
138
                PaytureOperation::PAY_STATUS(),
139
                'apim',
140
                [
141
                    'Key' => 'MerchantKey',
142
                    'OrderId' => 'Order-123',
143
                ]
144
            )->willReturn('<PayStatus Success="True" State="Charged" Amount="10000"/>');
145
146
        $response = $this->terminal->payStatus('Order-123');
0 ignored issues
show
Deprecated Code introduced by
The method Lamoda\Payture\InPayClie...ayTerminal::payStatus() has been deprecated.

This method has been deprecated.

Loading history...
147
148
        self::assertTrue($response->isSuccess());
149
        self::assertTrue($response->isChargedState());
150
    }
151
152
    public function testGetState(): void
153
    {
154
        $rrn = '003770024290';
155
        $orderId = 'Order-123';
156
        $this->transport->expects($this->once())
157
            ->method('request')
158
            ->with(
159
                PaytureOperation::GET_STATE(),
160
                'apim',
161
                [
162
                    'Key' => 'MerchantKey',
163
                    'OrderId' => $orderId,
164
                ]
165
            )->willReturn('<GetState Success="True" OrderId="' . $orderId . '" State="Refunded"
166
                Forwarded="False" MerchantContract="Merchant" Amount="12461" RRN="' . $rrn . '"/>');
167
168
        $response = $this->terminal->getState($orderId);
169
170
        self::assertTrue($response->isSuccess());
171
        self::assertTrue($response->isRefundedState());
172
        self::assertEquals($rrn, $response->getRrn());
173
        self::assertEquals($orderId, $response->getOrderId());
174
    }
175
176
    public function testCreatingPaymentUrl(): void
177
    {
178
        self::assertEquals(
179
            'https://nowhere.payture.com/apim/Pay?SessionId=external-id',
180
            $this->terminal->createPaymentUrl('external-id')
181
        );
182
    }
183
184
    protected function setUp()
185
    {
186
        $this->config = new TerminalConfiguration('MerchantKey', 'MerchantPassword', 'https://nowhere.payture.com/');
187
        $this->transport = $this->createMock(TransportInterface::class);
188
189
        $this->terminal = new PaytureInPayTerminal($this->config, $this->transport);
0 ignored issues
show
Documentation introduced by
$this->transport is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Lamoda\Payture\In...ent\TransportInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
190
    }
191
}
192