Completed
Pull Request — master (#72)
by Mario
05:41
created

BaseMethodTest::testCaptureAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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
43
class BaseMethodTest extends \PHPUnit_Framework_TestCase
44
{
45
    /**
46
     * @var ClassToTest
47
     */
48
    private $classToTest;
49
50
    /**
51
     * @var ObjectManager
52
     */
53
    private $objectManager;
54
55
    /**
56
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $shopHelper;
59
60
    /**
61
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
62
     */
63
    private $scopeConfig;
64
65
    /**
66
     * @var Authorization|\PHPUnit_Framework_MockObject_MockObject
67
     */
68
    private $authorizationRequest;
69
70
    /**
71
     * @var Debit|\PHPUnit_Framework_MockObject_MockObject
72
     */
73
    private $debitRequest;
74
75
    /**
76
     * PAYONE capture request model
77
     *
78
     * @var Capture|\PHPUnit_Framework_MockObject_MockObject
79
     */
80
    private $captureRequest;
81
82
    protected function setUp()
83
    {
84
        $this->objectManager = new ObjectManager($this);
85
86
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
87
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
88
        $this->authorizationRequest = $this->getMockBuilder(Authorization::class)->disableOriginalConstructor()->getMock();
89
        $this->debitRequest = $this->getMockBuilder(Debit::class)->disableOriginalConstructor()->getMock();
90
        $this->captureRequest = $this->getMockBuilder(Capture::class)->disableOriginalConstructor()->getMock();
91
92
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
93
            'shopHelper' => $this->shopHelper,
94
            'scopeConfig' => $this->scopeConfig,
95
            'authorizationRequest' => $this->authorizationRequest,
96
            'debitRequest' => $this->debitRequest,
97
            'captureRequest' => $this->captureRequest
98
        ]);
99
    }
100
101
    public function testGetInstructions()
102
    {
103
        $expected = 'instruction text';
104
        $this->scopeConfig->method('getValue')->willReturn($expected);
105
        $result = $this->classToTest->getInstructions();
106
        $this->assertEquals($expected, $result);
107
    }
108
109
    public function testGetConfigPaymentAction()
110
    {
111
        $result = $this->classToTest->getConfigPaymentAction();
112
        $expected = AbstractMethod::ACTION_AUTHORIZE;
113
        $this->assertEquals($expected, $result);
114
    }
115
116
    public function testCanUseForCountry()
117
    {
118
        $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...
119
            ->method('getConfigParam')
120
            ->willReturnMap([
121
                ['allowspecific', 'global', 'payone_general', null, 0],
122
                ['specificcountry', 'global', 'payone_general', null, 'DE,AT']
123
            ]);
124
        $result = $this->classToTest->canUseForCountry('DE');
125
        $this->assertTrue($result);
126
    }
127
128 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...
129
    {
130
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
131
132
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
133
        $paymentInfo->method('getOrder')->willReturn($order);
134
135
        $aResponse = ['status' => 'REDIRECT', 'txid' => '12345', 'redirecturl' => 'http://testdomain.com'];
136
        $this->authorizationRequest->method('sendRequest')->willReturn($aResponse);
137
138
        $result = $this->classToTest->authorize($paymentInfo, 100);
139
        $this->assertInstanceOf(Paydirekt::class, $result);
140
    }
141
142 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...
143
    {
144
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
145
146
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock();
147
        $paymentInfo->method('getOrder')->willReturn($order);
148
149
        $aResponse = ['status' => 'ERROR', 'errorcode' => '42', 'customermessage' => 'Test error'];
150
        $this->authorizationRequest->method('sendRequest')->willReturn($aResponse);
151
152
        $this->setExpectedException(LocalizedException::class);
153
        $this->classToTest->authorize($paymentInfo, 100);
154
    }
155
156
    public function testRefund()
157
    {
158
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->getMock();
159
160
        $aResponse = ['status' => 'APPROVED'];
161
        $this->debitRequest->method('sendRequest')->willReturn($aResponse);
162
163
        $result = $this->classToTest->refund($paymentInfo, 100);
164
        $this->assertInstanceOf(Paydirekt::class, $result);
165
    }
166
167
    public function testRefundError()
168
    {
169
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->getMock();
170
171
        $aResponse = ['status' => 'ERROR', 'errorcode' => '42', 'customermessage' => 'Test error'];
172
        $this->debitRequest->method('sendRequest')->willReturn($aResponse);
173
174
        $this->setExpectedException(LocalizedException::class);
175
        $this->classToTest->refund($paymentInfo, 100);
176
    }
177
178
    public function testRefundNoResponse()
179
    {
180
        $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->getMock();
181
182
        $this->debitRequest->method('sendRequest')->willReturn(false);
183
184
        $this->setExpectedException(LocalizedException::class);
185
        $this->classToTest->refund($paymentInfo, 100);
186
    }
187
188 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...
189
    {
190
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
191
192
        $paymentInfo = $this->getMockBuilder(Info::class)
193
            ->disableOriginalConstructor()
194
            ->setMethods(['getOrder', 'getParentTransactionId'])
195
            ->getMock();
196
        $paymentInfo->method('getOrder')->willReturn($order);
197
        $paymentInfo->method('getParentTransactionId')->willReturn(true);
198
199
        $aResponse = ['status' => 'APPROVED'];
200
        $this->captureRequest->method('sendRequest')->willReturn($aResponse);
201
202
        $result = $this->classToTest->capture($paymentInfo, 100);
203
        $this->assertInstanceOf(Paydirekt::class, $result);
204
    }
205
206
    public function testCaptureError()
207
    {
208
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
209
210
        $paymentInfo = $this->getMockBuilder(Info::class)
211
            ->disableOriginalConstructor()
212
            ->setMethods(['getOrder', 'getParentTransactionId'])
213
            ->getMock();
214
        $paymentInfo->method('getOrder')->willReturn($order);
215
        $paymentInfo->method('getParentTransactionId')->willReturn(true);
216
217
        $aResponse = ['status' => 'ERROR', 'errorcode' => '42', 'customermessage' => 'Test error'];
218
        $this->captureRequest->method('sendRequest')->willReturn($aResponse);
219
220
        $this->setExpectedException(LocalizedException::class);
221
        $this->classToTest->capture($paymentInfo, 100);
222
    }
223
224
    public function testCaptureNoResponse()
225
    {
226
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
227
228
        $paymentInfo = $this->getMockBuilder(Info::class)
229
            ->disableOriginalConstructor()
230
            ->setMethods(['getOrder', 'getParentTransactionId'])
231
            ->getMock();
232
        $paymentInfo->method('getOrder')->willReturn($order);
233
        $paymentInfo->method('getParentTransactionId')->willReturn(true);
234
235
        $this->captureRequest->method('sendRequest')->willReturn(false);
236
237
        $this->setExpectedException(LocalizedException::class);
238
        $this->classToTest->capture($paymentInfo, 100);
239
    }
240
241
    public function testCaptureAuth()
242
    {
243
        $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
244
245
        $paymentInfo = $this->getMockBuilder(Info::class)
246
            ->disableOriginalConstructor()
247
            ->setMethods(['getOrder', 'getParentTransactionId'])
248
            ->getMock();
249
        $paymentInfo->method('getOrder')->willReturn($order);
250
        $paymentInfo->method('getParentTransactionId')->willReturn(false);
251
252
        $aResponse = ['status' => 'REDIRECT', 'txid' => '12345', 'redirecturl' => 'http://testdomain.com'];
253
        $this->authorizationRequest->method('sendRequest')->willReturn($aResponse);
254
255
        $result = $this->classToTest->capture($paymentInfo, 100);
256
        $this->assertInstanceOf(Paydirekt::class, $result);
257
    }
258
259
    public function testCanUseForCountryFalse()
260
    {
261
        $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...
262
            ->method('getConfigParam')
263
            ->willReturnMap(
264
                [
265
                    ['allowspecific', 'global', 'payone_general', null, 0],
266
                    ['specificcountry', 'global', 'payone_general', null, 'DE,AT'],
267
                    ['use_global', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, '0'],
268
                    ['allowspecific', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, '1'],
269
                    ['specificcountry', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, 'NL,AT'],
270
                ]
271
            );
272
        $result = $this->classToTest->canUseForCountry('DE');
273
        $this->assertFalse($result);
274
    }
275
}
276