AddresscheckTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.4285
cc 1
eloc 12
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\Service\V1\Data;
28
29
use Payone\Core\Service\V1\Addresscheck as ClassToTest;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Payone\Core\Model\Risk\Addresscheck;
32
use Magento\Quote\Api\Data\AddressInterface;
33
use Payone\Core\Service\V1\Data\AddresscheckResponse;
34
use Payone\Core\Service\V1\Data\AddresscheckResponseFactory;
35
use Payone\Core\Test\Unit\BaseTestCase;
36
use Payone\Core\Model\Test\PayoneObjectManager;
37
38
class AddresscheckTest extends BaseTestCase
39
{
40
    /**
41
     * @var ClassToTest
42
     */
43
    private $classToTest;
44
45
    /**
46
     * @var Addresscheck|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $addresscheck;
49
50
    /**
51
     * @var AddresscheckResponse|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $response;
54
55 View Code Duplication
    protected function setUp()
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...
56
    {
57
        $objectManager = $this->getObjectManager();
58
59
        $this->addresscheck = $this->getMockBuilder(Addresscheck::class)->disableOriginalConstructor()->getMock();
60
        $this->response = $objectManager->getObject(AddresscheckResponse::class);
61
        $responseFactory = $this->getMockBuilder(AddresscheckResponseFactory::class)
62
            ->disableOriginalConstructor()
63
            ->setMethods(['create'])
64
            ->getMock();
65
        $responseFactory->method('create')->willReturn($this->response);
66
67
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
68
            'addresscheck' => $this->addresscheck,
69
            'responseFactory' => $responseFactory
70
        ]);
71
    }
72
73 View Code Duplication
    public function testCheckAddressFalse()
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...
74
    {
75
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(false);
76
77
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
78
79
        $result = $this->classToTest->checkAddress($addressData, false, false, 100);
80
        $result = $result->__toArray();
81
        $this->assertFalse($result['success']);
82
    }
83
84 View Code Duplication
    public function testCheckAddressTrue()
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...
85
    {
86
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(true);
87
        $this->addresscheck->method('getResponse')->willReturn(true);
88
89
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
90
91
        $result = $this->classToTest->checkAddress($addressData, false, false, 100);
92
        $result = $result->__toArray();
93
        $this->assertTrue($result['success']);
94
    }
95
96 View Code Duplication
    public function testCheckAddressInvalid()
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...
97
    {
98
        $expected = 'invalid message';
99
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(true);
100
        $this->addresscheck->method('getResponse')->willReturn(['status' => 'INVALID', 'customermessage' => $expected]);
101
        $this->addresscheck->method('getScore')->willReturn('G');
102
        $this->addresscheck->method('getInvalidMessage')->willReturn($expected);
103
104
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
105
106
        $result = $this->classToTest->checkAddress($addressData, false, false, 100);
107
        $result = $result->__toArray();
108
        $this->assertFalse($result['success']);
109
        $this->assertEquals($expected, $result['errormessage']);
110
    }
111
112
    public function testCheckAddressErrorContinue()
113
    {
114
        $expected = 'invalid message';
0 ignored issues
show
Unused Code introduced by
$expected is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(true);
116
        $this->addresscheck->method('getResponse')->willReturn(['status' => 'ERROR']);
117
        $this->addresscheck->method('getScore')->willReturn('G');
118
        $this->addresscheck->method('getConfigParam')->willReturn('continue_checkout');
119
120
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
121
122
        $result = $this->classToTest->checkAddress($addressData, true, false, 100);
123
        $result = $result->__toArray();
124
        $this->assertTrue($result['success']);
125
    }
126
127 View Code Duplication
    public function testCheckAddressErrorStop()
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
        $expected = 'error message';
130
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(true);
131
        $this->addresscheck->method('getResponse')->willReturn(['status' => 'ERROR']);
132
        $this->addresscheck->method('getScore')->willReturn('G');
133
        $this->addresscheck->method('getConfigParam')->willReturn('stop_checkout');
134
        $this->addresscheck->method('getErrorMessage')->willReturn($expected);
135
136
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
137
138
        $result = $this->classToTest->checkAddress($addressData, true, false, 100);
139
        $result = $result->__toArray();
140
        $this->assertFalse($result['success']);
141
        $this->assertEquals($expected, $result['errormessage']);
142
    }
143
144
    public function testCheckAddressValid()
145
    {
146
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
147
148
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(true);
149
        $this->addresscheck->method('getResponse')->willReturn(['status' => 'VALID']);
150
        $this->addresscheck->method('getScore')->willReturn('G');
151
        $this->addresscheck->method('isAddressCorrected')->willReturn(true);
152
        $this->addresscheck->method('correctAddress')->willReturn($addressData);
153
154
        $result = $this->classToTest->checkAddress($addressData, true, false, 100);
155
        $result = $result->__toArray();
156
        $this->assertTrue($result['success']);
157
        $this->assertNotEmpty($result['confirmMessage']);
158
    }
159
160
    public function testCheckAddressValidStreetArray()
161
    {
162
        $addressData = $this->getMockBuilder(AddressInterface::class)->disableOriginalConstructor()->getMock();
163
        $addressData->method('getStreet')->willReturn(['Teststr. 1', 'Additional info']);
164
165
        $this->addresscheck->method('isCheckNeededForQuote')->willReturn(true);
166
        $this->addresscheck->method('getResponse')->willReturn(['status' => 'VALID']);
167
        $this->addresscheck->method('getScore')->willReturn('G');
168
        $this->addresscheck->method('isAddressCorrected')->willReturn(true);
169
        $this->addresscheck->method('correctAddress')->willReturn($addressData);
170
171
        $result = $this->classToTest->checkAddress($addressData, true, false, 100);
172
        $result = $result->__toArray();
173
        $this->assertTrue($result['success']);
174
        $this->assertNotEmpty($result['confirmMessage']);
175
    }
176
}
177