ConsumerscoreTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 156
Duplicated Lines 5.77 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 9
loc 156
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
B testSendRequest() 0 34 1
B testSendRequestBoniversum() 0 40 1
B testSendRequestTrue() 0 28 1
A testSendRequestNotNeeded() 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\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
use Payone\Core\Test\Unit\BaseTestCase;
40
use Payone\Core\Model\Test\PayoneObjectManager;
41
42
class ConsumerscoreTest extends BaseTestCase
43
{
44
    /**
45
     * @var ClassToTest
46
     */
47
    private $classToTest;
48
49
    /**
50
     * @var Api|\PHPUnit_Framework_MockObject_MockObject
51
     */
52
    private $apiHelper;
53
54
    /**
55
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
56
     */
57
    private $shopHelper;
58
59
    /**
60
     * @var CheckedAddresses|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $addressesChecked;
63
64
    protected function setUp()
65
    {
66
        $objectManager = $this->getObjectManager();
67
68
        $databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
69
        $databaseHelper->method('getSequenceNumber')->willReturn('0');
70
71
        $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();
72
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
73
        $this->addressesChecked = $this->getMockBuilder(CheckedAddresses::class)->disableOriginalConstructor()->getMock();
74
75
        $this->classToTest = $objectManager->getObject(ClassToTest::class, [
76
            'databaseHelper' => $databaseHelper,
77
            'apiHelper' => $this->apiHelper,
78
            'shopHelper' => $this->shopHelper,
79
            'addressesChecked' => $this->addressesChecked
80
        ]);
81
    }
82
83
    public function testSendRequest()
84
    {
85
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
86
        $address->method('getCountryId')->willReturn('DE');
87
88
        $address->method('getFirstname')->willReturn('Paul');
89
        $address->method('getLastname')->willReturn('Paytest');
90
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
91
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
92
        $address->method('getPostcode')->willReturn('12345');
93
        $address->method('getCity')->willReturn('Berlin');
94
        $address->method('getRegionCode')->willReturn('Berlin');
95
96
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);
97
98
        $response = [
99
            'status' => 'VALID',
100
            'score'  => 'G',
101
        ];
102
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
103
104
        $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...
105
            ->method('getConfigParam')
106
            ->willReturnMap([
107
                ['enabled', 'creditrating', 'payone_protect', null, true],
108
                ['mode', 'creditrating', 'payone_protect', null, 'test'],
109
                ['aid', 'global', 'payone_general', null, '12345'],
110
                ['addresscheck', 'creditrating', 'payone_protect', null, 'test'],
111
                ['type', 'creditrating', 'payone_protect', null, 'test'],
112
            ]);
113
114
        $result = $this->classToTest->sendRequest($address);
115
        $this->assertArrayHasKey('status', $result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address) on line 114 can also be of type boolean; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, 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...
116
    }
117
118
    public function testSendRequestBoniversum()
119
    {
120
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
121
        $address->method('getCountryId')->willReturn('DE');
122
123
        $address->method('getFirstname')->willReturn('Paul');
124
        $address->method('getLastname')->willReturn('Paytest');
125
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
126
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
127
        $address->method('getPostcode')->willReturn('12345');
128
        $address->method('getCity')->willReturn('Berlin');
129
        $address->method('getRegionCode')->willReturn('Berlin');
130
131
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);
132
133
        $response = [
134
            'status' => 'VALID',
135
            'score'  => 'U',
136
        ];
137
        $this->apiHelper->method('sendApiRequest')->willReturn($response);
138
139
        $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...
140
            ->method('getConfigParam')
141
            ->willReturnMap([
142
                ['enabled', 'creditrating', 'payone_protect', null, true],
143
                ['mode', 'creditrating', 'payone_protect', null, 'test'],
144
                ['aid', 'global', 'payone_general', null, '12345'],
145
                ['addresscheck', 'creditrating', 'payone_protect', null, 'test'],
146
                ['type', 'creditrating', 'payone_protect', null, CreditratingCheckType::BONIVERSUM_VERITA],
147
            ]);
148
149
        /** @var AddressInterface $address */
150
        $result = $this->classToTest->sendRequest($address);
151
        ConsumerscoreTest::assertArrayHasKey('status', $result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address) on line 150 can also be of type boolean; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, 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...
152
        ConsumerscoreTest::assertEquals(
153
            AddressCheckType::BONIVERSUM_PERSON,
154
            $this->classToTest->getParameter('addresschecktype'),
155
            'Check types do not match!'
156
        );
157
    }
158
159
    public function testSendRequestTrue()
160
    {
161
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
162
        $address->method('getCountryId')->willReturn('DE');
163
164
        $address->method('getFirstname')->willReturn('Paul');
165
        $address->method('getLastname')->willReturn('Paytest');
166
        $address->method('getCompany')->willReturn('Testcompany Ltd.');
167
        $address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
168
        $address->method('getPostcode')->willReturn('12345');
169
        $address->method('getCity')->willReturn('Berlin');
170
        $address->method('getRegionCode')->willReturn('Berlin');
171
172
        $this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(true);
173
174
        $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...
175
            ->method('getConfigParam')
176
            ->willReturnMap([
177
                ['enabled', 'creditrating', 'payone_protect', null, true],
178
                ['mode', 'creditrating', 'payone_protect', null, 'test'],
179
                ['aid', 'global', 'payone_general', null, '12345'],
180
                ['addresscheck', 'creditrating', 'payone_protect', null, 'test'],
181
                ['type', 'creditrating', 'payone_protect', null, 'test'],
182
            ]);
183
184
        $result = $this->classToTest->sendRequest($address);
185
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address) on line 184 can also be of type array<string,?,{"score":"?","status":"?"}>; 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...
186
    }
187
188 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...
189
    {
190
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
191
192
        $this->shopHelper->method('getConfigParam')->willReturn(false);
193
194
        $result = $this->classToTest->sendRequest($address);
195
        $this->assertTrue($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->sendRequest($address) on line 194 can also be of type array<string,?,{"score":"?","status":"?"}>; 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...
196
    }
197
}
198