ExpressTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 50
c 2
b 0
f 0
rs 9.3333
cc 1
eloc 35
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\Controller\Paypal;
28
29
use Payone\Core\Controller\Paypal\Express as ClassToTest;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Sales\Model\Order as OrderCore;
32
use Magento\Framework\Controller\Result\Redirect;
33
use Magento\Checkout\Model\Session;
34
use Magento\Framework\Controller\ResultFactory;
35
use Magento\Framework\App\Action\Context;
36
use Magento\Framework\Message\ManagerInterface;
37
use Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress;
38
use Magento\Checkout\Helper\Data;
39
use Magento\Customer\Model\Session as CustomerSession;
40
use Payone\Core\Helper\Payment;
41
use Magento\Customer\Api\Data\CustomerInterface;
42
use Magento\Quote\Model\Quote;
43
use Magento\Store\App\Response\Redirect as RedirectResponse;
44
use Magento\Framework\App\Console\Response;
45
use Magento\Framework\App\ResponseInterface;
46
use Magento\Quote\Model\Quote\Payment as CorePayment;
47
use Payone\Core\Test\Unit\BaseTestCase;
48
use Payone\Core\Model\Test\PayoneObjectManager;
49
50
class ExpressTest extends BaseTestCase
51
{
52
    /**
53
     * @var ClassToTest
54
     */
55
    private $classToTest;
56
57
    /**
58
     * @var ObjectManager|PayoneObjectManager
59
     */
60
    private $objectManager;
61
62
    /**
63
     * @var Payment|\PHPUnit_Framework_MockObject_MockObject
64
     */
65
    private $paymentHelper;
66
67
    /**
68
     * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
69
     */
70
    private $customerSession;
71
72
    /**
73
     * @var PayPalExpress|\PHPUnit_Framework_MockObject_MockObject
74
     */
75
    private $genericRequest;
76
77
    /**
78
     * @var Data|\PHPUnit_Framework_MockObject_MockObject
79
     */
80
    private $checkoutHelper;
81
82
    protected function setUp()
83
    {
84
        $this->objectManager = $this->getObjectManager();
85
86
        $resultRedirect = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock();
87
        $resultRedirect->method('setPath')->willReturn($resultRedirect);
88
89
        $resultFactory = $this->getMockBuilder(ResultFactory::class)->disableOriginalConstructor()->getMock();
90
        $resultFactory->method('create')->willReturn($resultRedirect);
91
92
        $messageManager = $this->getMockBuilder(ManagerInterface::class)->disableOriginalConstructor()->getMock();
93
94
        $redirectResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
95
96
        $redirect = $this->getMockBuilder(RedirectResponse::class)->disableOriginalConstructor()->getMock();
97
        $redirect->method('redirect')->willReturn($redirectResponse);
98
99
        $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
100
101
        $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
102
        $context->method('getResultFactory')->willReturn($resultFactory);
103
        $context->method('getMessageManager')->willReturn($messageManager);
104
        $context->method('getRedirect')->willReturn($redirect);
105
        $context->method('getResponse')->willReturn($response);
106
107
        $payment = $this->getMockBuilder(CorePayment::class)->disableOriginalConstructor()->getMock();
108
109
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
110
        $quote->method('hasItems')->willReturn(true);
111
        $quote->method('getCheckoutMethod')->willReturn(false);
112
        $quote->method('getStoreId')->willReturn(15);
113
        $quote->method('getPayment')->willReturn($payment);
114
115
        $checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
116
        $checkoutSession->method('getQuote')->willReturn($quote);
117
118
        $this->genericRequest = $this->getMockBuilder(PayPalExpress::class)->disableOriginalConstructor()->getMock();
119
        $this->checkoutHelper = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
120
        $this->customerSession = $this->getMockBuilder(CustomerSession::class)->disableOriginalConstructor()->getMock();
121
        $this->paymentHelper = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
122
123
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
124
            'context' => $context,
125
            'checkoutSession' => $checkoutSession,
126
            'genericRequest' => $this->genericRequest,
127
            'checkoutHelper' => $this->checkoutHelper,
128
            'customerSession' => $this->customerSession,
129
            'paymentHelper' => $this->paymentHelper
130
        ]);
131
    }
132
133
    public function testExecuteNotActivated()
134
    {
135
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(false);
136
137
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\Paypal\Express::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
138
        $this->assertInstanceOf(Redirect::class, $result);
139
    }
140
141
    public function testExecuteLoginNeeded()
142
    {
143
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
144
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(false);
145
146
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
147
        $customer->method('getId')->willReturn(null);
148
149
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
150
151
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\Paypal\Express::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
152
        $this->assertInstanceOf(Redirect::class, $result);
153
    }
154
155 View Code Duplication
    public function testExecuteStatusError()
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...
156
    {
157
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
158
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(false);
159
160
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
161
        $customer->method('getId')->willReturn(15);
162
163
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
164
165
        $response = ['status' => 'ERROR', 'customermessage' => 'An error occured'];
166
        $this->genericRequest->method('sendRequest')->willReturn($response);
167
168
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\Paypal\Express::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
169
        $this->assertInstanceOf(Redirect::class, $result);
170
    }
171
172 View Code Duplication
    public function testExecuteStatusErrorGuestAllowed()
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...
173
    {
174
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
175
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(true);
176
177
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
178
        $customer->method('getId')->willReturn(null);
179
180
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
181
182
        $response = ['status' => 'ERROR', 'customermessage' => 'An error occured'];
183
        $this->genericRequest->method('sendRequest')->willReturn($response);
184
185
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\Paypal\Express::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
186
        $this->assertInstanceOf(Redirect::class, $result);
187
    }
188
189 View Code Duplication
    public function testExecuteStatusRedirect()
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...
190
    {
191
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
192
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(false);
193
194
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
195
        $customer->method('getId')->willReturn(15);
196
197
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
198
199
        $response = ['status' => 'REDIRECT', 'workorderid' => '12345', 'redirecturl' => 'http://redirect.org'];
200
        $this->genericRequest->method('sendRequest')->willReturn($response);
201
202
        $result = $this->classToTest->execute();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->classToTest->execute() (which targets Payone\Core\Controller\Paypal\Express::execute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
203
        $this->assertNull($result);
204
    }
205
}
206