AddresscheckTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
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\Model\Api\Request;
28
29
use Magento\Quote\Model\Quote\Address;
30
use Payone\Core\Model\Api\Request\Addresscheck as ClassToTest;
31
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
32
use Payone\Core\Helper\Api;
33
use Payone\Core\Model\ResourceModel\CheckedAddresses;
34
use Payone\Core\Helper\Shop;
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 Api|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $apiHelper;
49
50
    /**
51
     * @var CheckedAddresses|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $addressesChecked;
54
55
    /**
56
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $shopHelper;
59
60
    protected function setUp()
61
    {
62
        $objectManager = $this->getObjectManager();
63
64
        $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();
65
66
        $this->addressesChecked = $this->getMockBuilder(CheckedAddresses::class)->disableOriginalConstructor()->getMock();
67
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
68
69
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
70
            'apiHelper' => $this->apiHelper,
71
            'shopHelper' => $this->shopHelper,
72
            'addressesChecked' => $this->addressesChecked
73
        ]);
74
    }
75
76 View Code Duplication
    public function testSendRequestTrueNotEnabled()
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...
77
    {
78
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
79
80
        $this->shopHelper->method('getConfigParam')->willReturn(false);
81
82
        $result = $this->classToTest->sendRequest($address, true);
83
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address, true) on line 82 can also be of type array<string,?,{"status":"?"}> or array<string,boolean,{"wrongCountry":"boolean"}>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
    }
85
86 View Code Duplication
    public function testSendRequestTrueNoBilling()
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...
87
    {
88
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
89
90
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
91
            ->method('getConfigParam')
92
            ->willReturnMap([
93
                ['enabled', 'address_check', 'payone_protect', null, true],
94
                ['check_billing', 'address_check', 'payone_protect', null, 'NO']
95
            ]);
96
97
        $result = $this->classToTest->sendRequest($address, true);
98
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address, true) on line 97 can also be of type array<string,?,{"status":"?"}> or array<string,boolean,{"wrongCountry":"boolean"}>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
    }
100
101 View Code Duplication
    public function testSendRequestTrueNoShipping()
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...
102
    {
103
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
104
105
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
            ->method('getConfigParam')
107
            ->willReturnMap([
108
                ['enabled', 'address_check', 'payone_protect', null, true],
109
                ['check_shipping', 'address_check', 'payone_protect', null, 'NO']
110
            ]);
111
112
        $result = $this->classToTest->sendRequest($address, false);
113
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address, false) on line 112 can also be of type array<string,?,{"status":"?"}> or array<string,boolean,{"wrongCountry":"boolean"}>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
    }
115
116 View Code Duplication
    public function testSendRequestInvalidTypePE()
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...
117
    {
118
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
119
        $address->method('getCountryId')->willReturn('FR');
120
121
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
122
            ->method('getConfigParam')
123
            ->willReturnMap([
124
                ['enabled', 'address_check', 'payone_protect', null, true],
125
                ['check_shipping', 'address_check', 'payone_protect', null, 'PE']
126
            ]);
127
128
        $result = $this->classToTest->sendRequest($address, false);
129
        $expected = ['wrongCountry' => true];
130
        $this->assertEquals($expected, $result);
131
    }
132
133 View Code Duplication
    public function testSendRequestInvalidTypeBA()
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...
134
    {
135
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
136
        $address->method('getCountryId')->willReturn('XY');
137
138
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
139
            ->method('getConfigParam')
140
            ->willReturnMap([
141
                ['enabled', 'address_check', 'payone_protect', null, true],
142
                ['check_shipping', 'address_check', 'payone_protect', null, 'BA']
143
            ]);
144
145
        $result = $this->classToTest->sendRequest($address, false);
146
        $expected = ['wrongCountry' => true];
147
        $this->assertEquals($expected, $result);
148
    }
149
150
    public function testSendRequestNotChecked()
151
    {
152
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
153
        $address->method('getCountryId')->willReturn('DE');
154
        $address->method('getFirstname')->willReturn('Paul');
155
        $address->method('getLastname')->willReturn('Paytest');
156
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
157
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
158
        $address->method('getPostcode')->willReturn('12345');
159
        $address->method('getCity')->willReturn('Berlin');
160
        $address->method('getRegionCode')->willReturn('Berlin');
161
162
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
163
            ->method('getConfigParam')
164
            ->willReturnMap([
165
                ['enabled', 'address_check', 'payone_protect', null, true],
166
                ['check_shipping', 'address_check', 'payone_protect', null, 'PE'],
167
                ['mode', 'address_check', 'payone_protect', null, 'test'],
168
                ['aid', 'global', 'payone_general', null, 'PE']
169
            ]);
170
171
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);
172
173
        $response = ['status' => 'VALID'];
174
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
175
176
        $result = $this->classToTest->sendRequest($address, false);
177
        $this->assertEquals($response, $result);
178
    }
179
180
    public function testSendRequestChecked()
181
    {
182
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
183
        $address->method('getCountryId')->willReturn('DE');
184
        $address->method('getFirstname')->willReturn('Paul');
185
        $address->method('getLastname')->willReturn('Paytest');
186
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
187
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
188
        $address->method('getPostcode')->willReturn('12345');
189
        $address->method('getCity')->willReturn('Berlin');
190
        $address->method('getRegionCode')->willReturn('Berlin');
191
192
        $this->shopHelper->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Payone\Core\Helper\Shop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
193
            ->method('getConfigParam')
194
            ->willReturnMap([
195
                ['enabled', 'address_check', 'payone_protect', null, true],
196
                ['check_shipping', 'address_check', 'payone_protect', null, 'PE'],
197
                ['mode', 'address_check', 'payone_protect', null, 'test'],
198
                ['aid', 'global', 'payone_general', null, 'PE']
199
            ]);
200
201
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(true);
202
203
        $result = $this->classToTest->sendRequest($address, false);
204
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address, false) on line 203 can also be of type array<string,?,{"status":"?"}> or array<string,boolean,{"wrongCountry":"boolean"}>; however, PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
205
    }
206
}
207