Completed
Push — master ( b50753...137f43 )
by Florian
03:57
created

DebitTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 160
Duplicated Lines 20.63 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 33
loc 160
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 52 1
A testExecuteNoPayment() 0 7 1
A testExecuteSuccess() 10 10 1
A testExecuteMandateMismatch() 11 11 1
A testExecuteMandate() 0 12 1
A testExecuteMandatePending() 12 12 1
A testExecuteMandateError() 0 12 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\Onepage;
28
29
use Magento\Quote\Model\Quote;
30
use Payone\Core\Controller\Onepage\Debit as ClassToTest;
31
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
32
use Magento\Sales\Model\Order as OrderCore;
33
use Magento\Checkout\Model\Session;
34
use Magento\Framework\App\Action\Context;
35
use Magento\Customer\Model\Session as CustomerSession;
36
use Payone\Core\Model\Api\Request\Managemandate;
37
use Magento\Framework\View\Result\PageFactory;
38
use Magento\Checkout\Model\Type\Onepage;
39
use Magento\Quote\Model\Quote\Payment;
40
use Payone\Core\Model\Methods\PayoneMethod;
41
use Magento\Store\App\Response\Redirect as RedirectResponse;
42
use Magento\Framework\App\Console\Response;
43
use Magento\Framework\UrlInterface;
44
use Magento\Framework\App\RequestInterface;
45
use Magento\Framework\View\Result\Page;
46
47
class DebitTest 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 Session|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $checkoutSession;
63
64
    /**
65
     * @var Quote|\PHPUnit_Framework_MockObject_MockObject
66
     */
67
    private $quote;
68
69
    /**
70
     * @var Payment|\PHPUnit_Framework_MockObject_MockObject
71
     */
72
    private $payment;
73
74
    /**
75
     * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
76
     */
77
    private $request;
78
79
    /**
80
     * @var Managemandate|\PHPUnit_Framework_MockObject_MockObject
81
     */
82
    private $managemandateRequest;
83
84
    protected function setUp()
85
    {
86
        $this->objectManager = new ObjectManager($this);
87
88
        $redirectResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
89
90
        $redirect = $this->getMockBuilder(RedirectResponse::class)->disableOriginalConstructor()->getMock();
91
        $redirect->method('redirect')->willReturn($redirectResponse);
92
93
        $response = $this->getMockBuilder(Response::class)
94
            ->disableOriginalConstructor()
95
            ->setMethods(['setRedirect'])
96
            ->getMock();
97
98
        $url = $this->getMockBuilder(UrlInterface::class)->disableOriginalConstructor()->getMock();
99
100
        $this->request = $this->getMockBuilder(RequestInterface::class)->disableOriginalConstructor()->getMock();
101
102
        $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
103
        $context->method('getRedirect')->willReturn($redirect);
104
        $context->method('getRequest')->willReturn($this->request);
105
        $context->method('getResponse')->willReturn($response);
106
        $context->method('getUrl')->willReturn($url);
107
108
        $this->payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
109
110
        $this->quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
111
        $this->quote->method('getPayment')->willReturn($this->payment);
112
        $this->quote->method('getId')->willReturn('12345');
113
114
        $this->checkoutSession = $this->getMockBuilder(Session::class)
115
            ->disableOriginalConstructor()
116
            ->setMethods(['setPayoneMandate', 'getPayoneMandate', 'setPayoneDebitError', 'getQuote'])
117
            ->getMock();
118
        $this->checkoutSession->method('getQuote')->willReturn($this->quote);
119
120
        $page = $this->getMockBuilder(Page::class)->disableOriginalConstructor()->getMock();
121
122
        $pageFactory = $this->getMockBuilder(PageFactory::class)->disableOriginalConstructor()->getMock();
123
        $pageFactory->method('create')->willReturn($page);
124
        $this->managemandateRequest = $this->getMockBuilder(Managemandate::class)->disableOriginalConstructor()->getMock();
125
        $typeOnepage = $this->getMockBuilder(Onepage::class)->disableOriginalConstructor()->getMock();
126
        $typeOnepage->method('getCheckoutMethod')->willReturn('register');
127
128
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
129
            'context' => $context,
130
            'checkoutSession' => $this->checkoutSession,
131
            'managemandateRequest' => $this->managemandateRequest,
132
            'pageFactory' => $pageFactory,
133
            'typeOnepage' => $typeOnepage
134
        ]);
135
    }
136
137
    public function testExecuteNoPayment()
138
    {
139
        $this->payment->method('getMethodInstance')->willReturn(null);
140
141
        $result = $this->classToTest->execute();
142
        $this->assertNull($result);
143
    }
144
145 View Code Duplication
    public function testExecuteSuccess()
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...
146
    {
147
        $paymentMethod = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
148
        $this->payment->method('getMethodInstance')->willReturn($paymentMethod);
149
        $this->request->method('getParam')->willReturn(0);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Magento\Framework\App\RequestInterface>.

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...
150
        $this->checkoutSession->method('getPayoneMandate')->willReturn(['dummyMandate']);
151
152
        $result = $this->classToTest->execute();
153
        $this->assertNull($result);
154
    }
155
156 View Code Duplication
    public function testExecuteMandateMismatch()
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...
157
    {
158
        $paymentMethod = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
159
        $paymentMethod->method('getCustomConfigParam')->willReturn(true);
160
        $this->payment->method('getMethodInstance')->willReturn($paymentMethod);
161
        $this->request->method('getParam')->willReturn(1);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Magento\Framework\App\RequestInterface>.

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...
162
        $this->checkoutSession->method('getPayoneMandate')->willReturn(['mandate_identification' => 5]);
163
164
        $result = $this->classToTest->execute();
165
        $this->assertNull($result);
166
    }
167
168
    public function testExecuteMandate()
169
    {
170
        $paymentMethod = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
171
        $paymentMethod->method('getCustomConfigParam')->willReturn(true);
172
        $this->payment->method('getMethodInstance')->willReturn($paymentMethod);
173
        $this->request->method('getParam')->willReturn(1);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Magento\Framework\App\RequestInterface>.

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...
174
        $this->checkoutSession->method('getPayoneMandate')->willReturn(null);
175
        $this->managemandateRequest->method('sendRequest')->willReturn(['status' => 'VALID', 'mandate_status' => 'valid']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...\Request\Managemandate>.

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...
176
177
        $result = $this->classToTest->execute();
178
        $this->assertNull($result);
179
    }
180
181 View Code Duplication
    public function testExecuteMandatePending()
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...
182
    {
183
        $paymentMethod = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
184
        $paymentMethod->method('getCustomConfigParam')->willReturn(true);
185
        $this->payment->method('getMethodInstance')->willReturn($paymentMethod);
186
        $this->request->method('getParam')->willReturn(1);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Magento\Framework\App\RequestInterface>.

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...
187
        $this->checkoutSession->method('getPayoneMandate')->willReturn(null);
188
        $this->managemandateRequest->method('sendRequest')->willReturn(['status' => 'VALID', 'mandate_status' => 'pending']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...\Request\Managemandate>.

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...
189
190
        $result = $this->classToTest->execute();
191
        $this->assertInstanceOf(Page::class, $result);
192
    }
193
194
    public function testExecuteMandateError()
195
    {
196
        $paymentMethod = $this->getMockBuilder(PayoneMethod::class)->disableOriginalConstructor()->getMock();
197
        $paymentMethod->method('getCustomConfigParam')->willReturn(true);
198
        $this->payment->method('getMethodInstance')->willReturn($paymentMethod);
199
        $this->request->method('getParam')->willReturn(1);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Magento\Framework\App\RequestInterface>.

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...
200
        $this->checkoutSession->method('getPayoneMandate')->willReturn(null);
201
        $this->managemandateRequest->method('sendRequest')->willReturn(['status' => 'ERROR', 'errorcode' => '123', 'customermessage' => 'error']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...\Request\Managemandate>.

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...
202
203
        $result = $this->classToTest->execute();
204
        $this->assertInstanceOf(Page::class, $result);
205
    }
206
}
207