Completed
Push — master ( 1a26ad...341936 )
by Florian
04:37
created

AuthorizationTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 159
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 46 1
A getPaymentMock() 0 17 1
A getAddressMock() 0 15 1
B testSendRequest() 0 27 1
A testSendRequestPaypal() 0 22 1
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\Api\Request;
28
29
use Magento\Quote\Model\Quote\Address;
30
use Magento\Sales\Model\Order;
31
use Payone\Core\Helper\Database;
32
use Payone\Core\Model\Api\Request\Authorization as ClassToTest;
33
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
34
use Payone\Core\Model\Methods\PayoneMethod;
35
use Payone\Core\Helper\Api;
36
use Payone\Core\Helper\Toolkit;
37
use Payone\Core\Helper\Shop;
38
use Payone\Core\Model\PayoneConfig;
39
use Magento\Checkout\Model\Session;
40
use Magento\Quote\Model\Quote;
41
use Magento\Customer\Api\Data\CustomerInterface;
42
use Payone\Core\Helper\Environment;
43
44
class AuthorizationTest extends \PHPUnit_Framework_TestCase
45
{
46
    /**
47
     * @var ClassToTest
48
     */
49
    private $classToTest;
50
51
    /**
52
     * @var Api|\PHPUnit_Framework_MockObject_MockObject
53
     */
54
    private $apiHelper;
55
56
    /**
57
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
58
     */
59
    private $shopHelper;
60
61
    protected function setUp()
62
    {
63
        $objectManager = new ObjectManager($this);
64
65
        $databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
66
        $databaseHelper->method('getSequenceNumber')->willReturn('0');
67
68
        $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();
69
        $this->apiHelper->method('isInvoiceDataNeeded')->willReturn(true);
70
71
        $toolkitHelper = $this->getMockBuilder(Toolkit::class)->disableOriginalConstructor()->getMock();
72
        $toolkitHelper->method('handleSubstituteReplacement')->willReturn('test text');
73
        $toolkitHelper->method('getNarrativeText')->willReturn('narrative text');
74
75
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
76
        $this->shopHelper->expects($this->any())
77
            ->method('getConfigParam')
78
            ->willReturnMap([
79
                ['aid', 'global', 'payone_general', null, '12345'],
80
                ['transmit_ip', 'global', 'payone_general', null, '1'],
81
                ['bill_as_del_address', PayoneConfig::METHOD_PAYPAL, 'payone_payment', null, true],
82
                ['ref_prefix', 'global', 'payone_general', null, 'test_']
83
            ]);
84
85
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
86
        $customer->method('getGender')->willReturn('m');
87
        $customer->method('getDob')->willReturn('20000101');
88
89
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
90
        $quote->method('getCustomer')->willReturn($customer);
91
92
        $environmentHelper = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
93
        $environmentHelper->method('getRemoteIp')->willReturn('127.0.0.0.1');
94
95
        $checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
96
        $checkoutSession->method('getQuote')->willReturn($quote);
97
98
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
99
            'databaseHelper' => $databaseHelper,
100
            'apiHelper' => $this->apiHelper,
101
            'toolkitHelper' => $toolkitHelper,
102
            'shopHelper' => $this->shopHelper,
103
            'checkoutSession' => $checkoutSession,
104
            'environmentHelper' => $environmentHelper
105
        ]);
106
    }
107
108
    /**
109
     * Get payment mock object
110
     *
111
     * @return \PHPUnit_Framework_MockObject_MockObject
112
     */
113
    private function getPaymentMock()
114
    {
115
        $payment = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
116
        $payment->method('getAuthorizationMode')->willReturn('authorization');
117
        $payment->method('getOperationMode')->willReturn('test');
118
        $payment->method('hasCustomConfig')->willReturn(true);
119
        $payment->method('formatReferenceNumber')->willReturn('12345');
120
        $payment->method('getCode')->willReturn(PayoneConfig::METHOD_PAYPAL);
121
        $payment->method('getClearingtype')->willReturn('wlt');
122
        $payment->method('getPaymentSpecificParameters')->willReturn([]);
123
        $payment->method('needsRedirectUrls')->willReturn(true);
124
        $payment->method('getSuccessUrl')->willReturn('http://testdomain.com');
125
        $payment->method('getErrorUrl')->willReturn('http://testdomain.com');
126
        $payment->method('getCancelUrl')->willReturn('http://testdomain.com');
127
        $payment->method('getCustomConfigParam')->willReturn('true');
128
        return $payment;
129
    }
130
131
    /**
132
     * Return address mock object
133
     *
134
     * @return \PHPUnit_Framework_MockObject_MockObject
135
     */
136
    private function getAddressMock()
137
    {
138
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
139
        $address->method('getCountryId')->willReturn('US');
140
        $address->method('getFirstname')->willReturn('Paul');
141
        $address->method('getLastname')->willReturn('Paytest');
142
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
143
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
144
        $address->method('getPostcode')->willReturn('12345');
145
        $address->method('getCity')->willReturn('Berlin');
146
        $address->method('getRegionCode')->willReturn('CA');
147
        $address->method('getTelephone')->willReturn('0301234567');
148
        $address->method('getVatId')->willReturn('12345');
149
        return $address;
150
    }
151
152
    public function testSendRequest()
153
    {
154
        $payment = $this->getPaymentMock();
155
        $address = $this->getAddressMock();
156
157
        $expectedOrderId = '54321';
158
159
        $order = $this->getMockBuilder(Order::class)
160
            ->disableOriginalConstructor()
161
            ->setMethods(['getRealOrderId', 'getOrderCurrencyCode', 'getCustomerId', 'getCustomerEmail', 'getBillingAddress', 'getShippingAddress'])
162
            ->getMock();
163
        $order->method('getRealOrderId')->willReturn($expectedOrderId);
164
        $order->method('getOrderCurrencyCode')->willReturn('EUR');
165
        $order->method('getCustomerId')->willReturn('12345');
166
        $order->method('getCustomerEmail')->willReturn('[email protected]');
167
        $order->method('getBillingAddress')->willReturn($address);
168
        $order->method('getShippingAddress')->willReturn($address);
169
170
        $response = ['status' => 'VALID'];
171
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
172
173
        $result = $this->classToTest->sendRequest($payment, $order, 100);
174
        $this->assertEquals($response, $result);
175
176
        $orderId = $this->classToTest->getOrderId();
177
        $this->assertEquals($expectedOrderId, $orderId);
178
    }
179
180
    public function testSendRequestPaypal()
181
    {
182
        $payment = $this->getPaymentMock();
183
        $address = $this->getAddressMock();
184
185
        $order = $this->getMockBuilder(Order::class)
186
            ->disableOriginalConstructor()
187
            ->setMethods(['getRealOrderId', 'getOrderCurrencyCode', 'getCustomerId', 'getCustomerEmail', 'getBillingAddress', 'getShippingAddress'])
188
            ->getMock();
189
        $order->method('getRealOrderId')->willReturn('54321');
190
        $order->method('getOrderCurrencyCode')->willReturn('EUR');
191
        $order->method('getCustomerId')->willReturn('12345');
192
        $order->method('getCustomerEmail')->willReturn('[email protected]');
193
        $order->method('getBillingAddress')->willReturn($address);
194
        $order->method('getShippingAddress')->willReturn(false);
195
196
        $response = ['status' => 'VALID'];
197
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
198
199
        $result = $this->classToTest->sendRequest($payment, $order, 100);
200
        $this->assertEquals($response, $result);
201
    }
202
}
203