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

ForwardingTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 245
Duplicated Lines 42.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 2
dl 104
loc 245
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 25 1
A testIsCheckNeededForQuoteMinBasket() 9 9 1
A testIsCheckNeededForQuoteMaxBasket() 9 9 1
A testIsCheckNeededForQuoteVirtual() 9 9 1
A testIsCheckNeededForQuote() 9 9 1
A testCorrectAddress() 8 8 1
A testHandleAddressManagement() 0 19 1
A testHandleAddressManagementExceptionError() 0 21 1
A testHandleAddressManagementExceptionInvalid() 20 20 1
A testHandleAddressManagementExceptionInvalidDefault() 20 20 1
A testGetScoreR() 0 9 1
A testGetScorePersonstatusNoMapping() 0 14 1
A testGetScorePersonstatus() 0 16 1
A testGetScoreStillValid() 11 11 1
A testGetResponse() 9 9 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\Model\Risk;
28
29
use Payone\Core\Model\Risk\Addresscheck as ClassToTest;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Payone\Core\Model\Api\Request\Addresscheck;
32
use Payone\Core\Helper\Database;
33
use Payone\Core\Helper\Toolkit;
34
use Magento\Checkout\Model\Session;
35
use Magento\Quote\Model\Quote\Address;
36
use Magento\Quote\Model\Quote;
37
use Magento\Framework\Exception\LocalizedException;
38
39
class ForwardingTest extends \PHPUnit_Framework_TestCase
40
{
41
    /**
42
     * @var ClassToTest
43
     */
44
    private $classToTest;
45
46
    /**
47
     * @var ObjectManager
48
     */
49
    private $objectManager;
50
51
    /**
52
     * @var Addresscheck|\PHPUnit_Framework_MockObject_MockObject
53
     */
54
    private $addresscheck;
55
56
    /**
57
     * @var Database|\PHPUnit_Framework_MockObject_MockObject
58
     */
59
    private $databaseHelper;
60
61
    protected function setUp()
62
    {
63
        $this->objectManager = new ObjectManager($this);
64
65
        $this->addresscheck = $this->getMockBuilder(Addresscheck::class)->disableOriginalConstructor()->getMock();
66
        $this->databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
67
        $toolkitHelper = $this->getMockBuilder(Toolkit::class)->disableOriginalConstructor()->getMock();
68
        $toolkitHelper->method('handleSubstituteReplacement')->willReturn('Invalid message');
69
        $checkoutSession = $this->getMockBuilder(Session::class)
70
            ->disableOriginalConstructor()
71
            ->setMethods([
72
                'getPayoneBillingAddresscheckScore',
73
                'getPayoneShippingAddresscheckScore',
74
                'unsPayoneBillingAddresscheckScore',
75
                'unsPayoneShippingAddresscheckScore'])
76
            ->getMock();
77
        $checkoutSession->method('getPayoneShippingAddresscheckScore')->willReturn(null);
78
79
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
80
            'addresscheck' => $this->addresscheck,
81
            'databaseHelper' => $this->databaseHelper,
82
            'toolkitHelper' => $toolkitHelper,
83
            'checkoutSession' => $checkoutSession
84
        ]);
85
    }
86
87 View Code Duplication
    public function testIsCheckNeededForQuoteMinBasket()
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...
88
    {
89
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
90
            ->method('getConfigParam')
91
            ->willReturnMap([['min_order_total', 'address_check', 'payone_protect', null, 10]]);
92
93
        $result = $this->classToTest->isCheckNeededForQuote(true, true, 5);
94
        $this->assertFalse($result);
95
    }
96
97 View Code Duplication
    public function testIsCheckNeededForQuoteMaxBasket()
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...
98
    {
99
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
100
            ->method('getConfigParam')
101
            ->willReturnMap([['max_order_total', 'address_check', 'payone_protect', null, 10]]);
102
103
        $result = $this->classToTest->isCheckNeededForQuote(true, true, 15);
104
        $this->assertFalse($result);
105
    }
106
107 View Code Duplication
    public function testIsCheckNeededForQuoteVirtual()
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...
108
    {
109
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
110
            ->method('getConfigParam')
111
            ->willReturnMap([['check_billing_for_virtual_order', 'address_check', 'payone_protect', null, 0]]);
112
113
        $result = $this->classToTest->isCheckNeededForQuote(true, true, 15);
114
        $this->assertFalse($result);
115
    }
116
117 View Code Duplication
    public function testIsCheckNeededForQuote()
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...
118
    {
119
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
120
            ->method('getConfigParam')
121
            ->willReturnMap([['check_billing_for_virtual_order', 'address_check', 'payone_protect', null, 0]]);
122
123
        $result = $this->classToTest->isCheckNeededForQuote(true, false, 15);
124
        $this->assertTrue($result);
125
    }
126
127 View Code Duplication
    public function testCorrectAddress()
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...
128
    {
129
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
130
        $this->addresscheck->method('sendRequest')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
131
132
        $result = $this->classToTest->correctAddress($address);
133
        $this->assertEquals($address, $result);
134
    }
135
136
    public function testHandleAddressManagement()
137
    {
138
        $address = $this->getMockBuilder(Address::class)
139
            ->disableOriginalConstructor()
140
            ->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData'])
141
            ->getMock();
142
        $address->method('getPayoneAddresscheckScore')->willReturn(null);
143
        $address->method('getStreet')->willReturn(['Teststr. 12', '3rd floor']);
144
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
145
        $quote->method('isVirtual')->willReturn(false);
146
        $quote->method('getSubtotal')->willReturn(100);
147
        $this->addresscheck->method('sendRequest')->willReturn(['status' => 'VALID', 'street' => 'Another str. 7', 'firstname' => 'Patrick']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
148
149
        $result = $this->classToTest->handleAddressManagement($address, $quote, false);
150
        $this->assertInstanceOf(Address::class, $result);
151
152
        $result = $this->classToTest->isAddressCorrected();
153
        $this->assertTrue($result);
154
    }
155
156
    public function testHandleAddressManagementExceptionError()
157
    {
158
        $address = $this->getMockBuilder(Address::class)
159
            ->disableOriginalConstructor()
160
            ->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData'])
161
            ->getMock();
162
        $address->method('getPayoneAddresscheckScore')->willReturn(null);
163
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
164
        $quote->method('isVirtual')->willReturn(false);
165
        $quote->method('getSubtotal')->willReturn(100);
166
        $this->addresscheck->method('sendRequest')->willReturn(['status' => 'ERROR']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
167
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
168
            ->method('getConfigParam')
169
            ->willReturnMap([
170
                ['handle_response_error', 'address_check', 'payone_protect', null, 'stop_checkout'],
171
                ['stop_checkout_message', 'address_check', 'payone_protect', null, null]
172
            ]);
173
174
        $this->setExpectedException(LocalizedException::class);
175
        $this->classToTest->handleAddressManagement($address, $quote, false);
176
    }
177
178 View Code Duplication
    public function testHandleAddressManagementExceptionInvalid()
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...
179
    {
180
        $address = $this->getMockBuilder(Address::class)
181
            ->disableOriginalConstructor()
182
            ->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData'])
183
            ->getMock();
184
        $address->method('getPayoneAddresscheckScore')->willReturn(null);
185
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
186
        $quote->method('isVirtual')->willReturn(false);
187
        $quote->method('getSubtotal')->willReturn(100);
188
        $this->addresscheck->method('sendRequest')->willReturn(['status' => 'INVALID', 'customermessage' => 'Address invalid']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
190
            ->method('getConfigParam')
191
            ->willReturnMap([
192
                ['message_response_invalid', 'address_check', 'payone_protect', null, null]
193
            ]);
194
195
        $this->setExpectedException(LocalizedException::class);
196
        $this->classToTest->handleAddressManagement($address, $quote, false);
197
    }
198
199 View Code Duplication
    public function testHandleAddressManagementExceptionInvalidDefault()
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...
200
    {
201
        $address = $this->getMockBuilder(Address::class)
202
            ->disableOriginalConstructor()
203
            ->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData'])
204
            ->getMock();
205
        $address->method('getPayoneAddresscheckScore')->willReturn(null);
206
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
207
        $quote->method('isVirtual')->willReturn(false);
208
        $quote->method('getSubtotal')->willReturn(100);
209
        $this->addresscheck->method('sendRequest')->willReturn(['status' => 'INVALID', 'customermessage' => 'Address invalid']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
210
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
211
            ->method('getConfigParam')
212
            ->willReturnMap([
213
                ['message_response_invalid', 'address_check', 'payone_protect', null, 'Address invalid: {{payone_customermessage}}']
214
            ]);
215
216
        $this->setExpectedException(LocalizedException::class);
217
        $this->classToTest->handleAddressManagement($address, $quote, false);
218
    }
219
220
    public function testGetScoreR()
221
    {
222
        $this->addresscheck->method('sendRequest')->willReturn(['status' => 'INVALID']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
223
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
224
225
        $result = $this->classToTest->getScore($address);
226
        $expected = 'R';
227
        $this->assertEquals($expected, $result);
228
    }
229
230
    public function testGetScorePersonstatusNoMapping()
231
    {
232
        $status = 'x';
233
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
234
            ->method('getConfigParam')
235
            ->willReturnMap([['mapping_personstatus', 'address_check', 'payone_protect', null, serialize($status)]]);
236
237
        $this->addresscheck->method('sendRequest')->willReturn(['personstatus' => 'ABC']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
238
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
239
240
        $result = $this->classToTest->getScore($address);
241
        $expected = 'G';
242
        $this->assertEquals($expected, $result);
243
    }
244
245
    public function testGetScorePersonstatus()
246
    {
247
        $status = [
248
            ['personstatus' => 'ABC', 'score' => 'D']
249
        ];
250
        $this->databaseHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
251
            ->method('getConfigParam')
252
            ->willReturnMap([['mapping_personstatus', 'address_check', 'payone_protect', null, serialize($status)]]);
253
254
        $this->addresscheck->method('sendRequest')->willReturn(['personstatus' => 'ABC']);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
255
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
256
257
        $result = $this->classToTest->getScore($address);
258
        $expected = 'D';
259
        $this->assertEquals($expected, $result);
260
    }
261
262 View Code Duplication
    public function testGetScoreStillValid()
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...
263
    {
264
        $this->addresscheck->method('sendRequest')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
265
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
266
267
        $expected = 'X';
268
        $this->databaseHelper->method('getOldAddressStatus')->willReturn($expected);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Database>.

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...
269
270
        $result = $this->classToTest->getScore($address);
271
        $this->assertEquals($expected, $result);
272
    }
273
274 View Code Duplication
    public function testGetResponse()
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...
275
    {
276
        $this->addresscheck->method('sendRequest')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Model...i\Request\Addresscheck>.

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...
277
278
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
279
280
        $result = $this->classToTest->getResponse($address);
281
        $this->assertTrue($result);
282
    }
283
}
284