Completed
Push — master ( fe4d16...77dac1 )
by Florian
26:03
created

ExpressTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 153
Duplicated Lines 31.37 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 48
loc 153
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 47 1
A testExecuteNotActivated() 0 7 1
A testExecuteLoginNeeded() 0 13 1
A testExecuteStatusError() 16 16 1
A testExecuteStatusErrorGuestAllowed() 16 16 1
A testExecuteStatusRedirect() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
47
class ExpressTest extends \PHPUnit_Framework_TestCase
48
{
49
    /**
50
     * @var ClassToTest
51
     */
52
    private $classToTest;
53
54
    /**
55
     * @var ObjectManager
56
     */
57
    private $objectManager;
58
59
    /**
60
     * @var Payment|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $paymentHelper;
63
64
    /**
65
     * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
66
     */
67
    private $customerSession;
68
69
    /**
70
     * @var PayPalExpress|\PHPUnit_Framework_MockObject_MockObject
71
     */
72
    private $genericRequest;
73
74
    /**
75
     * @var Data|\PHPUnit_Framework_MockObject_MockObject
76
     */
77
    private $checkoutHelper;
78
79
    protected function setUp()
80
    {
81
        $this->objectManager = new ObjectManager($this);
82
83
        $resultRedirect = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock();
84
        $resultRedirect->method('setPath')->willReturn($resultRedirect);
85
86
        $resultFactory = $this->getMockBuilder(ResultFactory::class)->disableOriginalConstructor()->getMock();
87
        $resultFactory->method('create')->willReturn($resultRedirect);
88
89
        $messageManager = $this->getMockBuilder(ManagerInterface::class)->disableOriginalConstructor()->getMock();
90
91
        $redirectResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
92
93
        $redirect = $this->getMockBuilder(RedirectResponse::class)->disableOriginalConstructor()->getMock();
94
        $redirect->method('redirect')->willReturn($redirectResponse);
95
96
        $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
97
98
        $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
99
        $context->method('getResultFactory')->willReturn($resultFactory);
100
        $context->method('getMessageManager')->willReturn($messageManager);
101
        $context->method('getRedirect')->willReturn($redirect);
102
        $context->method('getResponse')->willReturn($response);
103
104
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
105
        $quote->method('hasItems')->willReturn(true);
106
        $quote->method('getCheckoutMethod')->willReturn(false);
107
        $quote->method('getStoreId')->willReturn(15);
108
109
        $checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
110
        $checkoutSession->method('getQuote')->willReturn($quote);
111
112
        $this->genericRequest = $this->getMockBuilder(PayPalExpress::class)->disableOriginalConstructor()->getMock();
113
        $this->checkoutHelper = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
114
        $this->customerSession = $this->getMockBuilder(CustomerSession::class)->disableOriginalConstructor()->getMock();
115
        $this->paymentHelper = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
116
117
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
118
            'context' => $context,
119
            'checkoutSession' => $checkoutSession,
120
            'genericRequest' => $this->genericRequest,
121
            'checkoutHelper' => $this->checkoutHelper,
122
            'customerSession' => $this->customerSession,
123
            'paymentHelper' => $this->paymentHelper
124
        ]);
125
    }
126
127
    public function testExecuteNotActivated()
128
    {
129
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Payment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
131
        $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...
132
        $this->assertInstanceOf(Redirect::class, $result);
133
    }
134
135
    public function testExecuteLoginNeeded()
136
    {
137
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Payment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(false);
139
140
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
141
        $customer->method('getId')->willReturn(null);
142
143
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
144
145
        $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...
146
        $this->assertInstanceOf(Redirect::class, $result);
147
    }
148
149 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...
150
    {
151
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Payment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(false);
153
154
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
155
        $customer->method('getId')->willReturn(15);
156
157
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
158
159
        $response = ['status' => 'ERROR', 'customermessage' => 'An error occured'];
160
        $this->genericRequest->method('sendRequest')->willReturn($response);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...cpayment\PayPalExpress>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
162
        $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...
163
        $this->assertInstanceOf(Redirect::class, $result);
164
    }
165
166 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...
167
    {
168
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Payment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(true);
170
171
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
172
        $customer->method('getId')->willReturn(null);
173
174
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
175
176
        $response = ['status' => 'ERROR', 'customermessage' => 'An error occured'];
177
        $this->genericRequest->method('sendRequest')->willReturn($response);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...cpayment\PayPalExpress>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
179
        $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...
180
        $this->assertInstanceOf(Redirect::class, $result);
181
    }
182
183 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...
184
    {
185
        $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Payment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
        $this->checkoutHelper->method('isAllowedGuestCheckout')->willReturn(false);
187
188
        $customer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
189
        $customer->method('getId')->willReturn(15);
190
191
        $this->customerSession->method('getCustomerDataObject')->willReturn($customer);
192
193
        $response = ['status' => 'REDIRECT', 'workorderid' => '12345', 'redirecturl' => 'http://redirect.org'];
194
        $this->genericRequest->method('sendRequest')->willReturn($response);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...cpayment\PayPalExpress>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
196
        $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...
197
        $this->assertNull($result);
198
    }
199
}
200