testIfOrderDataIsSetCorrectlyResponseMandate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 11
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\Helper;
28
29
use Payone\Core\Helper\Api;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Store\Model\StoreManagerInterface;
32
use Magento\Store\Api\Data\StoreInterface;
33
use Payone\Core\Model\Methods\PayoneMethod;
34
use Magento\Framework\App\Helper\Context;
35
use Magento\Store\Model\ScopeInterface;
36
use Magento\Framework\App\Config\ScopeConfigInterface;
37
use Magento\Sales\Model\Order;
38
use Payone\Core\Helper\Connection\CurlPhp;
39
use Payone\Core\Helper\Connection\CurlCli;
40
use Payone\Core\Helper\Connection\Fsockopen;
41
use Payone\Core\Test\Unit\BaseTestCase;
42
use Payone\Core\Model\Test\PayoneObjectManager;
43
44
class ApiTest extends BaseTestCase
45
{
46
    /**
47
     * @var ObjectManager|PayoneObjectManager
48
     */
49
    private $objectManager;
50
51
    /**
52
     * @var Api
53
     */
54
    private $api;
55
56
    /**
57
     * @var PayoneMethod
58
     */
59
    private $payment;
60
61
    /**
62
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
63
     */
64
    private $scopeConfig;
65
66
    /**
67
     * @var CurlPhp|\PHPUnit_Framework_MockObject_MockObject
68
     */
69
    private $connCurlPhp;
70
71
    /**
72
     * @var CurlCli|\PHPUnit_Framework_MockObject_MockObject
73
     */
74
    private $connCurlCli;
75
76
    /**
77
     * @var Fsockopen|\PHPUnit_Framework_MockObject_MockObject
78
     */
79
    private $connFsockopen;
80
81
    protected function setUp()
82
    {
83
        $this->objectManager = $this->getObjectManager();
84
85
        $this->payment = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
86
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
87
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
88
89
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
90
        $store->method('getCode')->willReturn('test');
91
92
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
93
        $storeManager->method('getStore')->willReturn($store);
94
95
        $this->connCurlPhp = $this->getMockBuilder(CurlPhp::class)->disableOriginalConstructor()->getMock();
96
        $this->connCurlCli = $this->getMockBuilder(CurlCli::class)->disableOriginalConstructor()->getMock();
97
        $this->connFsockopen = $this->getMockBuilder(Fsockopen::class)->disableOriginalConstructor()->getMock();
98
99
        $sendOutput = [
100
            'status=APPROVED',
101
            'txid=42',
102
            'userid=0815',
103
            'test',
104
            ''
105
        ];
106
        $this->connCurlPhp->method('sendCurlPhpRequest')->willReturn($sendOutput);
107
        $this->connCurlCli->method('sendCurlCliRequest')->willReturn($sendOutput);
108
        $this->connFsockopen->method('sendSocketRequest')->willReturn($sendOutput);
109
110
        $this->api = $this->objectManager->getObject(Api::class, [
111
            'context' => $context,
112
            'storeManager' => $storeManager,
113
            'connCurlPhp' => $this->connCurlPhp,
114
            'connCurlCli' => $this->connCurlCli,
115
            'connFsockopen' => $this->connFsockopen
116
        ]);
117
    }
118
119
    public function testIsInvoiceDataNeededForRequest()
120
    {
121
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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...
122
            ->method('getValue')
123
            ->willReturnMap(
124
                [
125
                    ['payone_general/global/request_type', ScopeInterface::SCOPE_STORE, null, 'authorization'],
126
                    ['payone_general/invoicing/transmit_enabled', ScopeInterface::SCOPE_STORE, null, 1]
127
                ]
128
            );
129
        $this->payment->method('needsProductInfo')->willReturn(true);
130
131
        $result = $this->api->isInvoiceDataNeeded($this->payment);
132
        $this->assertTrue($result);
133
    }
134
135
    public function testIsInvoiceDataNotNeededForRequest()
136
    {
137
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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...
138
            ->method('getValue')
139
            ->willReturnMap(
140
                [
141
                    ['payone_general/global/request_type', ScopeInterface::SCOPE_STORE, null, 'preauthorization'],
142
                    ['payone_general/invoicing/transmit_enabled', ScopeInterface::SCOPE_STORE, null, 0]
143
                ]
144
            );
145
        $this->payment->method('needsProductInfo')->willReturn(false);
146
147
        $result = $this->api->isInvoiceDataNeeded($this->payment);
148
        $this->assertFalse($result);
149
    }
150
151
    public function testIfOrderDataIsSetCorrectly()
152
    {
153
        $oOrder = $this->objectManager->getObject(Order::class);
154
155
        $reference = 'ref123';
156
        $request = 'authorization';
157
        $mode = 'live';
158
        $mandate = 'mandate';
159
        $txid = '12345';
160
        $aRequest = [
161
            'reference' => $reference,
162
            'request' => $request,
163
            'mode' => $mode,
164
            'mandate_identification' => $mandate,
165
            'add_paydata[installment_duration]' => '5'
166
        ];
167
        $aResponse = [
168
            'txid' => $txid,
169
            'clearing_reference' => 'REFERENCE',
170
            'add_paydata[clearing_reference]' => 'REFERENCE',
171
            'add_paydata[workorderid]' => 'WORKORDER'
172
        ];
173
174
        $this->api->addPayoneOrderData($oOrder, $aRequest, $aResponse);
175
        $this->assertEquals($reference, $oOrder->getPayoneRefnr());
176
        $this->assertEquals($request, $oOrder->getPayoneAuthmode());
177
        $this->assertEquals($mode, $oOrder->getPayoneMode());
178
        $this->assertEquals($mandate, $oOrder->getPayoneMandateId());
179
        $this->assertEquals($txid, $oOrder->getPayoneTxid());
180
    }
181
182
    public function testIfOrderDataIsSetCorrectlyResponseMandate()
183
    {
184
        $oOrder = $this->objectManager->getObject(Order::class);
185
186
        $mandate = 'mandate';
187
        $aRequest = [
188
            'reference' => 'ref123',
189
            'request' => 'authorization',
190
            'mode' => 'live',
191
            'workorderid' => 'WORKORDER',
192
        ];
193
        $aResponse = ['mandate_identification' => $mandate];
194
195
        $this->api->addPayoneOrderData($oOrder, $aRequest, $aResponse);
196
        $this->assertEquals($mandate, $oOrder->getPayoneMandateId());
197
    }
198
199
    public function testIfTheRequestUrlIsGeneratedCorretly()
200
    {
201
        $aParameters = [
202
            'sku' => ['sku5', 'sku3'],
203
            'session' => 'test-session',
204
            'random' => 'sp ace'
205
        ];
206
        $sApiUrl = 'http://test.com';
207
        $result = $this->api->getRequestUrl($aParameters, $sApiUrl);
208
        $expected = 'http://test.com?sku[0]=sku5&sku[1]=sku3&session=test-session&random=sp+ace';
209
        $this->assertEquals($result, $expected);
210
    }
211
212
    public function testIfParseErrorIsReturnedCorrectly()
213
    {
214
        $return = $this->api->sendApiRequest("http://user@:80");
215
        $expected = ["errormessage" => "Payone API request URL could not be parsed."];
216
        $this->assertEquals($expected, $return);
217
    }
218
219 View Code Duplication
    public function testSendApiRequestReturnValueCurlPhp()
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...
220
    {
221
        $this->connCurlPhp->method('isApplicable')->willReturn(true);
222
223
        $return = $this->api->sendApiRequest("http://payone.de");
224
        $expected = [
225
            'status' => 'APPROVED',
226
            'txid' => '42',
227
            'userid' => '0815',
228
            3 => 'test'
229
        ];
230
        $this->assertEquals($expected, $return);
231
    }
232
233 View Code Duplication
    public function testSendApiRequestReturnValueCurlCli()
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...
234
    {
235
        $this->connCurlPhp->method('isApplicable')->willReturn(false);
236
        $this->connCurlCli->method('isApplicable')->willReturn(true);
237
238
        $return = $this->api->sendApiRequest("http://payone.de");
239
        $expected = [
240
            'status' => 'APPROVED',
241
            'txid' => '42',
242
            'userid' => '0815',
243
            3 => 'test'
244
        ];
245
        $this->assertEquals($expected, $return);
246
    }
247
248 View Code Duplication
    public function testSendApiRequestReturnValueFsockopen()
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...
249
    {
250
        $this->connCurlPhp->method('isApplicable')->willReturn(false);
251
        $this->connCurlCli->method('isApplicable')->willReturn(false);
252
253
        $return = $this->api->sendApiRequest("http://payone.de");
254
        $expected = [
255
            'status' => 'APPROVED',
256
            'txid' => '42',
257
            'userid' => '0815',
258
            3 => 'test'
259
        ];
260
        $this->assertEquals($expected, $return);
261
    }
262
}
263