BaseMethodTest::testCapture()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2017 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Test\Unit\Model\Methods;
28
29
use Magento\Payment\Model\Method\AbstractMethod;
30
use Magento\Sales\Model\Order;
31
use Payone\Core\Model\Methods\Paydirekt as ClassToTest;
32
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
33
use Payone\Core\Helper\Shop;
34
use Magento\Framework\App\Config\ScopeConfigInterface;
35
use Payone\Core\Model\Methods\Paydirekt;
36
use Payone\Core\Model\PayoneConfig;
37
use Magento\Payment\Model\Info;
38
use Payone\Core\Model\Api\Request\Authorization;
39
use Payone\Core\Model\Api\Request\Debit;
40
use Magento\Framework\Exception\LocalizedException;
41
use Payone\Core\Model\Api\Request\Capture;
42
use Payone\Core\Test\Unit\BaseTestCase;
43
use Payone\Core\Model\Test\PayoneObjectManager;
44
45
class BaseMethodTest extends BaseTestCase
46
{
47
    /**
48
     * @var ClassToTest
49
     */
50
    private $classToTest;
51
52
    /**
53
     * @var ObjectManager|PayoneObjectManager
54
     */
55
    private $objectManager;
56
57
    /**
58
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $shopHelper;
61
62
    /**
63
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $scopeConfig;
66
67
    /**
68
     * @var Authorization|\PHPUnit_Framework_MockObject_MockObject
69
     */
70
    private $authorizationRequest;
71
72
    /**
73
     * @var Debit|\PHPUnit_Framework_MockObject_MockObject
74
     */
75
    private $debitRequest;
76
77
    /**
78
     * PAYONE capture request model
79
     *
80
     * @var Capture|\PHPUnit_Framework_MockObject_MockObject
81
     */
82
    private $captureRequest;
83
84
    protected function setUp()
85
    {
86
        $this->objectManager = $this->getObjectManager();
87
88
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
89
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
90
        $this->authorizationRequest = $this->getMockBuilder(Authorization::class)->disableOriginalConstructor()->getMock();
91
        $this->debitRequest = $this->getMockBuilder(Debit::class)->disableOriginalConstructor()->getMock();
92
        $this->captureRequest = $this->getMockBuilder(Capture::class)->disableOriginalConstructor()->getMock();
93
94
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
95
            'shopHelper' => $this->shopHelper,
96
            'scopeConfig' => $this->scopeConfig,
97
            'authorizationRequest' => $this->authorizationRequest,
98
            'debitRequest' => $this->debitRequest,
99
            'captureRequest' => $this->captureRequest
100
        ]);
101
    }
102
103 View Code Duplication
    public function testGetInstructions()
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...
104
    {
105
        $expected = 'instruction text';
106
        $this->scopeConfig->method('getValue')->willReturn($expected);
107
        $result = $this->classToTest->getInstructions();
108
        $this->assertEquals($expected, $result);
109
    }
110
111
    public function testGetConfigPaymentAction()
112
    {
113
        $result = $this->classToTest->getConfigPaymentAction();
114
        $expected = AbstractMethod::ACTION_AUTHORIZE;
115
        $this->assertEquals($expected, $result);
116
    }
117
118
    public function testCanUseForCountry()
119
    {
120
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
121
            ->method('getConfigParam')
122
            ->willReturnMap([
123
                ['allowspecific', 'global', 'payone_general', null, 0],
124
                ['specificcountry', 'global', 'payone_general', null, 'DE,AT']
125
            ]);
126
        $result = $this->classToTest->canUseForCountry('DE');
127
        $this->assertTrue($result);
128
    }
129
130 View Code Duplication
    public function testAuthorize()
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...
131
    {
132
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
133
134
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
135
        $paymentInfo->method('getOrder')->willReturn($order);
136
137
        $aResponse = ['status' => 'REDIRECT', 'txid' => '12345', 'redirecturl' => 'http://testdomain.com'];
138
        $this->authorizationRequest->method('sendRequest')->willReturn($aResponse);
139
140
        $result = $this->classToTest->authorize($paymentInfo, 100);
141
        $this->assertInstanceOf(Paydirekt::class, $result);
142
    }
143
144 View Code Duplication
    public function testAuthorizeError()
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...
145
    {
146
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
147
148
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
149
        $paymentInfo->method('getOrder')->willReturn($order);
150
151
        $aResponse = ['status' => 'ERROR', 'errorcode' => '42', 'customermessage' => 'Test error'];
152
        $this->authorizationRequest->method('sendRequest')->willReturn($aResponse);
153
154
        $this->expectException(LocalizedException::class);
155
        $this->classToTest->authorize($paymentInfo, 100);
156
    }
157
158
    public function testRefund()
159
    {
160
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->getMock();
161
162
        $aResponse = ['status' => 'APPROVED'];
163
        $this->debitRequest->method('sendRequest')->willReturn($aResponse);
164
165
        $result = $this->classToTest->refund($paymentInfo, 100);
166
        $this->assertInstanceOf(Paydirekt::class, $result);
167
    }
168
169
    public function testRefundError()
170
    {
171
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->getMock();
172
173
        $aResponse = ['status' => 'ERROR', 'errorcode' => '42', 'customermessage' => 'Test error'];
174
        $this->debitRequest->method('sendRequest')->willReturn($aResponse);
175
176
        $this->expectException(LocalizedException::class);
177
        $this->classToTest->refund($paymentInfo, 100);
178
    }
179
180
    public function testRefundNoResponse()
181
    {
182
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->getMock();
183
184
        $this->debitRequest->method('sendRequest')->willReturn(false);
185
186
        $this->expectException(LocalizedException::class);
187
        $this->classToTest->refund($paymentInfo, 100);
188
    }
189
190 View Code Duplication
    public function testCapture()
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...
191
    {
192
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
193
194
        $paymentInfo = $this->getMockBuilder(Info::class)
195
            ->disableOriginalConstructor()
196
            ->setMethods(['getOrder', 'getParentTransactionId'])
197
            ->getMock();
198
        $paymentInfo->method('getOrder')->willReturn($order);
199
        $paymentInfo->method('getParentTransactionId')->willReturn(true);
200
201
        $aResponse = ['status' => 'APPROVED'];
202
        $this->captureRequest->method('sendRequest')->willReturn($aResponse);
203
204
        $result = $this->classToTest->capture($paymentInfo, 100);
205
        $this->assertInstanceOf(Paydirekt::class, $result);
206
    }
207
208 View Code Duplication
    public function testCaptureError()
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...
209
    {
210
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
211
212
        $paymentInfo = $this->getMockBuilder(Info::class)
213
            ->disableOriginalConstructor()
214
            ->setMethods(['getOrder', 'getParentTransactionId'])
215
            ->getMock();
216
        $paymentInfo->method('getOrder')->willReturn($order);
217
        $paymentInfo->method('getParentTransactionId')->willReturn(true);
218
219
        $aResponse = ['status' => 'ERROR', 'errorcode' => '42', 'customermessage' => 'Test error'];
220
        $this->captureRequest->method('sendRequest')->willReturn($aResponse);
221
222
        $this->expectException(LocalizedException::class);
223
        $this->classToTest->capture($paymentInfo, 100);
224
    }
225
226
    public function testCaptureNoResponse()
227
    {
228
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
229
230
        $paymentInfo = $this->getMockBuilder(Info::class)
231
            ->disableOriginalConstructor()
232
            ->setMethods(['getOrder', 'getParentTransactionId'])
233
            ->getMock();
234
        $paymentInfo->method('getOrder')->willReturn($order);
235
        $paymentInfo->method('getParentTransactionId')->willReturn(true);
236
237
        $this->captureRequest->method('sendRequest')->willReturn(false);
238
239
        $this->expectException(LocalizedException::class);
240
        $this->classToTest->capture($paymentInfo, 100);
241
    }
242
243 View Code Duplication
    public function testCaptureAuth()
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...
244
    {
245
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
246
247
        $paymentInfo = $this->getMockBuilder(Info::class)
248
            ->disableOriginalConstructor()
249
            ->setMethods(['getOrder', 'getParentTransactionId'])
250
            ->getMock();
251
        $paymentInfo->method('getOrder')->willReturn($order);
252
        $paymentInfo->method('getParentTransactionId')->willReturn(false);
253
254
        $aResponse = ['status' => 'REDIRECT', 'txid' => '12345', 'redirecturl' => 'http://testdomain.com'];
255
        $this->authorizationRequest->method('sendRequest')->willReturn($aResponse);
256
257
        $result = $this->classToTest->capture($paymentInfo, 100);
258
        $this->assertInstanceOf(Paydirekt::class, $result);
259
    }
260
261
    public function testCanUseForCountryFalse()
262
    {
263
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
264
            ->method('getConfigParam')
265
            ->willReturnMap(
266
                [
267
                    ['allowspecific', 'global', 'payone_general', null, 0],
268
                    ['specificcountry', 'global', 'payone_general', null, 'DE,AT'],
269
                    ['use_global', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, '0'],
270
                    ['allowspecific', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, '1'],
271
                    ['specificcountry', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, 'NL,AT'],
272
                ]
273
            );
274
        $result = $this->classToTest->canUseForCountry('DE');
275
        $this->assertFalse($result);
276
    }
277
}
278