Completed
Push — master ( 1a26ad...341936 )
by Florian
04:37
created

AddresscheckTest::testSendRequestChecked()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 20
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
36
class AddresscheckTest extends \PHPUnit_Framework_TestCase
37
{
38
    /**
39
     * @var ClassToTest
40
     */
41
    private $classToTest;
42
43
    /**
44
     * @var Api|\PHPUnit_Framework_MockObject_MockObject
45
     */
46
    private $apiHelper;
47
48
    /**
49
     * @var CheckedAddresses|\PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $addressesChecked;
52
53
    /**
54
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
55
     */
56
    private $shopHelper;
57
58
    protected function setUp()
59
    {
60
        $objectManager = new ObjectManager($this);
61
62
        $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();
63
64
        $this->addressesChecked = $this->getMockBuilder(CheckedAddresses::class)->disableOriginalConstructor()->getMock();
65
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
66
67
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
68
            'apiHelper' => $this->apiHelper,
69
            'shopHelper' => $this->shopHelper,
70
            'addressesChecked' => $this->addressesChecked
71
        ]);
72
    }
73
74 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...
75
    {
76
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
77
78
        $this->shopHelper->method('getConfigParam')->willReturn(false);
79
80
        $result = $this->classToTest->sendRequest($address, true);
81
        $this->assertTrue($result);
82
    }
83
84 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...
85
    {
86
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
87
88
        $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...
89
            ->method('getConfigParam')
90
            ->willReturnMap([
91
                ['enabled', 'address_check', 'payone_protect', null, true],
92
                ['check_billing', 'address_check', 'payone_protect', null, 'NO']
93
            ]);
94
95
        $result = $this->classToTest->sendRequest($address, true);
96
        $this->assertTrue($result);
97
    }
98
99 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...
100
    {
101
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
102
103
        $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...
104
            ->method('getConfigParam')
105
            ->willReturnMap([
106
                ['enabled', 'address_check', 'payone_protect', null, true],
107
                ['check_shipping', 'address_check', 'payone_protect', null, 'NO']
108
            ]);
109
110
        $result = $this->classToTest->sendRequest($address, false);
111
        $this->assertTrue($result);
112
    }
113
114 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...
115
    {
116
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
117
        $address->method('getCountryId')->willReturn('FR');
118
119
        $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...
120
            ->method('getConfigParam')
121
            ->willReturnMap([
122
                ['enabled', 'address_check', 'payone_protect', null, true],
123
                ['check_shipping', 'address_check', 'payone_protect', null, 'PE']
124
            ]);
125
126
        $result = $this->classToTest->sendRequest($address, false);
127
        $expected = ['wrongCountry' => true];
128
        $this->assertEquals($expected, $result);
129
    }
130
131 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...
132
    {
133
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
134
        $address->method('getCountryId')->willReturn('XY');
135
136
        $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...
137
            ->method('getConfigParam')
138
            ->willReturnMap([
139
                ['enabled', 'address_check', 'payone_protect', null, true],
140
                ['check_shipping', 'address_check', 'payone_protect', null, 'BA']
141
            ]);
142
143
        $result = $this->classToTest->sendRequest($address, false);
144
        $expected = ['wrongCountry' => true];
145
        $this->assertEquals($expected, $result);
146
    }
147
148
    public function testSendRequestNotChecked()
149
    {
150
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
151
        $address->method('getCountryId')->willReturn('DE');
152
        $address->method('getFirstname')->willReturn('Paul');
153
        $address->method('getLastname')->willReturn('Paytest');
154
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
155
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
156
        $address->method('getPostcode')->willReturn('12345');
157
        $address->method('getCity')->willReturn('Berlin');
158
        $address->method('getRegionCode')->willReturn('Berlin');
159
160
        $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...
161
            ->method('getConfigParam')
162
            ->willReturnMap([
163
                ['enabled', 'address_check', 'payone_protect', null, true],
164
                ['check_shipping', 'address_check', 'payone_protect', null, 'PE'],
165
                ['mode', 'address_check', 'payone_protect', null, 'test'],
166
                ['aid', 'global', 'payone_general', null, 'PE']
167
            ]);
168
169
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);
170
171
        $response = ['status' => 'VALID'];
172
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
173
174
        $result = $this->classToTest->sendRequest($address, false);
175
        $this->assertEquals($response, $result);
176
    }
177
178
    public function testSendRequestChecked()
179
    {
180
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
181
        $address->method('getCountryId')->willReturn('DE');
182
        $address->method('getFirstname')->willReturn('Paul');
183
        $address->method('getLastname')->willReturn('Paytest');
184
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
185
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
186
        $address->method('getPostcode')->willReturn('12345');
187
        $address->method('getCity')->willReturn('Berlin');
188
        $address->method('getRegionCode')->willReturn('Berlin');
189
190
        $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...
191
            ->method('getConfigParam')
192
            ->willReturnMap([
193
                ['enabled', 'address_check', 'payone_protect', null, true],
194
                ['check_shipping', 'address_check', 'payone_protect', null, 'PE'],
195
                ['mode', 'address_check', 'payone_protect', null, 'test'],
196
                ['aid', 'global', 'payone_general', null, 'PE']
197
            ]);
198
199
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(true);
200
201
        $result = $this->classToTest->sendRequest($address, false);
202
        $this->assertTrue($result);
203
    }
204
}
205