Passed
Pull Request — master (#4)
by
unknown
06:16
created

PaytureInPayTerminalTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 180
Duplicated Lines 32.22 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 58
loc 180
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
     * @param SessionType $type
27
     * @param string $data
28
     *
29
     * @throws \Lamoda\Payture\InPayClient\Exception\TransportException
30
     */
31
    public function testPaymentInit(SessionType $type, string $data): void
32
    {
33
        $this->transport->expects($this->once())
34
            ->method('request')
35
            ->with(
36
                PaytureOperation::INIT(),
37
                'apim',
38
                [
39
                    'Key' => 'MerchantKey',
40
                    'Data' => $data,
41
                ]
42
            )->willReturn('<Init Success="True" SessionId="external-id"/>');
43
44
        $response = $this->terminal->init(
45
            $type,
46
            'Order-123',
47
            'The order',
48
            10000,
49
            '127.0.0.1',
50
            'https://redirect-me.back/',
51
            'template',
52
            [
53
                'custom_data' => 'value',
54
            ]
55
        );
56
57
        self::assertTrue($response->isSuccess());
58
        self::assertEquals('external-id', $response->getSessionId());
59
    }
60
61
    public function getInitSessionTypes()
62
    {
63
        return [
64
            [
65
                SessionType::PAY(),
66
                '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',
67
            ],
68
            [
69
                SessionType::BLOCK(),
70
                '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',
71
            ],
72
        ];
73
    }
74
75 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...
76
    {
77
        $this->transport->expects($this->once())
78
            ->method('request')
79
            ->with(
80
                PaytureOperation::CHARGE(),
81
                'apim',
82
                [
83
                    'Key' => 'MerchantKey',
84
                    'OrderId' => 'Order-123',
85
                    'Amount' => 10000,
86
                    'Password' => 'MerchantPassword',
87
                ]
88
            )->willReturn('<Charge Success="True" Amount="10000"/>');
89
90
        $response = $this->terminal->charge('Order-123', 10000);
91
92
        self::assertTrue($response->isSuccess());
93
    }
94
95 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...
96
    {
97
        $this->transport->expects($this->once())
98
            ->method('request')
99
            ->with(
100
                PaytureOperation::UNBLOCK(),
101
                'apim',
102
                [
103
                    'Key' => 'MerchantKey',
104
                    'OrderId' => 'Order-123',
105
                    'Amount' => 10000,
106
                    'Password' => 'MerchantPassword',
107
                ]
108
            )->willReturn('<Unblock Success="True"/>');
109
110
        $response = $this->terminal->unblock('Order-123', 10000);
111
112
        self::assertTrue($response->isSuccess());
113
    }
114
115 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...
116
    {
117
        $this->transport->expects($this->once())
118
            ->method('request')
119
            ->with(
120
                PaytureOperation::REFUND(),
121
                'apim',
122
                [
123
                    'Key' => 'MerchantKey',
124
                    'OrderId' => 'Order-123',
125
                    'Amount' => 6000,
126
                    'Password' => 'MerchantPassword',
127
                ]
128
            )->willReturn('<Refund Success="True" NewAmount="4000"/>');
129
130
        $response = $this->terminal->refund('Order-123', 6000);
131
132
        self::assertTrue($response->isSuccess());
133
        self::assertEquals(4000, $response->getAmount());
134
    }
135
136
    public function testPaymentStatus(): void
137
    {
138
        $this->transport->expects($this->once())
139
            ->method('request')
140
            ->with(
141
                PaytureOperation::PAY_STATUS(),
142
                'apim',
143
                [
144
                    'Key' => 'MerchantKey',
145
                    'OrderId' => 'Order-123',
146
                ]
147
            )->willReturn('<PayStatus Success="True" State="Charged" Amount="10000"/>');
148
149
        $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...
150
151
        self::assertTrue($response->isSuccess());
152
        self::assertTrue($response->isChargedState());
153
    }
154
155
    public function testGetState(): void
156
    {
157
        $rrn = '003770024290';
158
        $orderId = 'Order-123';
159
        $this->transport->expects($this->once())
160
            ->method('request')
161
            ->with(
162
                PaytureOperation::GET_STATE(),
163
                'apim',
164
                [
165
                    'Key' => 'MerchantKey',
166
                    'OrderId' => $orderId,
167
                ]
168
            )->willReturn('<GetState Success="True" OrderId="' . $orderId . '" State="Refunded"
169
                Forwarded="False" MerchantContract="Merchant" Amount="12461" RRN="' . $rrn . '"/>');
170
171
        $response = $this->terminal->getState($orderId);
172
173
        self::assertTrue($response->isSuccess());
174
        self::assertTrue($response->isRefundedState());
175
        self::assertEquals($rrn, $response->getRrn());
176
        self::assertEquals($orderId, $response->getOrderId());
177
    }
178
179
    public function testCreatingPaymentUrl(): void
180
    {
181
        self::assertEquals(
182
            'https://nowhere.payture.com/apim/Pay?SessionId=external-id',
183
            $this->terminal->createPaymentUrl('external-id')
184
        );
185
    }
186
187
    protected function setUp()
188
    {
189
        $this->config = new TerminalConfiguration('MerchantKey', 'MerchantPassword', 'https://nowhere.payture.com/');
190
        $this->transport = $this->createMock(TransportInterface::class);
191
192
        $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...
193
    }
194
}
195