Completed
Pull Request — master (#73)
by Mario
05:29
created

ConsumerscoreTest::testSendRequestTrue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
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\Api\Data\AddressInterface;
30
use Magento\Quote\Model\Quote\Address;
31
use Payone\Core\Helper\Database;
32
use Payone\Core\Model\Api\Request\Consumerscore as ClassToTest;
33
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
34
use Payone\Core\Helper\Api;
35
use Payone\Core\Helper\Shop;
36
use Payone\Core\Model\ResourceModel\CheckedAddresses;
37
use Payone\Core\Model\Source\AddressCheckType;
38
use Payone\Core\Model\Source\CreditratingCheckType;
39
40
class ConsumerscoreTest extends \PHPUnit_Framework_TestCase
41
{
42
    /**
43
     * @var ClassToTest
44
     */
45
    private $classToTest;
46
47
    /**
48
     * @var Api|\PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $apiHelper;
51
52
    /**
53
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
54
     */
55
    private $shopHelper;
56
57
    /**
58
     * @var CheckedAddresses|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private $addressesChecked;
61
62
    protected function setUp()
63
    {
64
        $objectManager = new ObjectManager($this);
65
66
        $databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
67
        $databaseHelper->method('getSequenceNumber')->willReturn('0');
68
69
        $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();
70
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
71
        $this->addressesChecked = $this->getMockBuilder(CheckedAddresses::class)->disableOriginalConstructor()->getMock();
72
73
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
74
            'databaseHelper' => $databaseHelper,
75
            'apiHelper' => $this->apiHelper,
76
            'shopHelper' => $this->shopHelper,
77
            'addressesChecked' => $this->addressesChecked
78
        ]);
79
    }
80
81
    public function testSendRequest()
82
    {
83
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
84
        $address->method('getCountryId')->willReturn('DE');
85
86
        $address->method('getFirstname')->willReturn('Paul');
87
        $address->method('getLastname')->willReturn('Paytest');
88
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
89
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
90
        $address->method('getPostcode')->willReturn('12345');
91
        $address->method('getCity')->willReturn('Berlin');
92
        $address->method('getRegionCode')->willReturn('Berlin');
93
94
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);
95
96
        $response = [
97
            'status' => 'VALID',
98
            'score'  => 'G',
99
        ];
100
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
101
102
        $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...
103
            ->method('getConfigParam')
104
            ->willReturnMap([
105
                ['enabled', 'creditrating', 'payone_protect', null, true],
106
                ['mode', 'creditrating', 'payone_protect', null, 'test'],
107
                ['aid', 'global', 'payone_general', null, '12345'],
108
                ['addresscheck', 'creditrating', 'payone_protect', null, 'test'],
109
                ['type', 'creditrating', 'payone_protect', null, 'test'],
110
            ]);
111
112
        $result = $this->classToTest->sendRequest($address);
113
        $this->assertArrayHasKey('status', $result);
114
    }
115
116
    public function testSendRequestBoniversum()
117
    {
118
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
119
        $address->method('getCountryId')->willReturn('DE');
120
121
        $address->method('getFirstname')->willReturn('Paul');
122
        $address->method('getLastname')->willReturn('Paytest');
123
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
124
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
125
        $address->method('getPostcode')->willReturn('12345');
126
        $address->method('getCity')->willReturn('Berlin');
127
        $address->method('getRegionCode')->willReturn('Berlin');
128
129
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);
130
131
        $response = [
132
            'status' => 'VALID',
133
            'score'  => 'U',
134
        ];
135
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
136
137
        $this->shopHelper->expects(ConsumerscoreTest::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...
138
            ->method('getConfigParam')
139
            ->willReturnMap([
140
                ['enabled', 'creditrating', 'payone_protect', null, true],
141
                ['mode', 'creditrating', 'payone_protect', null, 'test'],
142
                ['aid', 'global', 'payone_general', null, '12345'],
143
                ['addresscheck', 'creditrating', 'payone_protect', null, 'test'],
144
                ['type', 'creditrating', 'payone_protect', null, CreditratingCheckType::BONIVERSUM_VERITA],
145
            ]);
146
147
        /** @var AddressInterface $address */
148
        $result = $this->classToTest->sendRequest($address);
149
        ConsumerscoreTest::assertArrayHasKey('status', $result);
150
        ConsumerscoreTest::assertEquals(
151
            AddressCheckType::BONIVERSUM_PERSON,
152
            $this->classToTest->getParameter('addresschecktype'),
153
            'Check types do not match!'
154
        );
155
    }
156
157
    public function testSendRequestTrue()
158
    {
159
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
160
        $address->method('getCountryId')->willReturn('DE');
161
162
        $address->method('getFirstname')->willReturn('Paul');
163
        $address->method('getLastname')->willReturn('Paytest');
164
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
165
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
166
        $address->method('getPostcode')->willReturn('12345');
167
        $address->method('getCity')->willReturn('Berlin');
168
        $address->method('getRegionCode')->willReturn('Berlin');
169
170
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(true);
171
172
        $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...
173
            ->method('getConfigParam')
174
            ->willReturnMap([
175
                ['enabled', 'creditrating', 'payone_protect', null, true],
176
                ['mode', 'creditrating', 'payone_protect', null, 'test'],
177
                ['aid', 'global', 'payone_general', null, '12345'],
178
                ['addresscheck', 'creditrating', 'payone_protect', null, 'test'],
179
                ['type', 'creditrating', 'payone_protect', null, 'test'],
180
            ]);
181
182
        $result = $this->classToTest->sendRequest($address);
183
        $this->assertTrue($result);
184
    }
185
186 View Code Duplication
    public function testSendRequestNotNeeded()
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...
187
    {
188
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
189
190
        $this->shopHelper->method('getConfigParam')->willReturn(false);
191
192
        $result = $this->classToTest->sendRequest($address);
193
        $this->assertTrue($result);
194
    }
195
}
196