PayoneMethodTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 17.53 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 4
dl 17
loc 97
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 10 10 1
A testGetClearingtype() 0 6 1
A testGetAuthorizationMode() 0 15 1
A testGetOperationMode() 7 7 1
A testNeedsRedirectUrls() 0 5 1
A testNeedsProductInfo() 0 5 1
A testHasCustomConfig() 0 6 1
A testIsGroupMethod() 0 5 1
A testGetGroupName() 0 5 1
A testGetNarrativeTextMaxLength() 0 6 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\Methods;
28
29
use Payone\Core\Model\Methods\Paydirekt as ClassToTest;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Payone\Core\Helper\Shop;
32
use Payone\Core\Model\PayoneConfig;
33
use Payone\Core\Test\Unit\BaseTestCase;
34
use Payone\Core\Model\Test\PayoneObjectManager;
35
36
class PayoneMethodTest extends BaseTestCase
37
{
38
    /**
39
     * @var ClassToTest
40
     */
41
    private $classToTest;
42
43
    /**
44
     * @var ObjectManager|PayoneObjectManager
45
     */
46
    private $objectManager;
47
48
    /**
49
     * @var Shop|\PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $shopHelper;
52
53 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...
54
    {
55
        $this->objectManager = $this->getObjectManager();
56
57
        $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();
58
59
        $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
60
            'shopHelper' => $this->shopHelper
61
        ]);
62
    }
63
64
    public function testGetClearingtype()
65
    {
66
        $result = $this->classToTest->getClearingtype();
67
        $expected = 'wlt';
68
        $this->assertEquals($expected, $result);
69
    }
70
71
    public function testGetAuthorizationMode()
72
    {
73
        $expected = 'custom_request_type';
74
        $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...
75
            ->method('getConfigParam')
76
            ->willReturnMap(
77
                [
78
                    ['request_type', 'global', 'payone_general', null, 'global_request_type'],
79
                    ['use_global', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, '0'],
80
                    ['request_type', PayoneConfig::METHOD_PAYDIREKT, 'payone_payment', null, $expected]
81
                ]
82
            );
83
        $result = $this->classToTest->getAuthorizationMode();
84
        $this->assertEquals($expected, $result);
85
    }
86
87 View Code Duplication
    public function testGetOperationMode()
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...
88
    {
89
        $expected = 'operation_mode';
90
        $this->shopHelper->method('getConfigParam')->willReturn($expected);
91
        $result = $this->classToTest->getOperationMode();
92
        $this->assertEquals($expected, $result);
93
    }
94
95
    public function testNeedsRedirectUrls()
96
    {
97
        $result = $this->classToTest->needsRedirectUrls();
98
        $this->assertTrue($result);
99
    }
100
101
    public function testNeedsProductInfo()
102
    {
103
        $result = $this->classToTest->needsProductInfo();
104
        $this->assertFalse($result);
105
    }
106
107
    public function testHasCustomConfig()
108
    {
109
        $this->shopHelper->method('getConfigParam')->willReturn('1');
110
        $result = $this->classToTest->hasCustomConfig();
111
        $this->assertFalse($result);
112
    }
113
114
    public function testIsGroupMethod()
115
    {
116
        $result = $this->classToTest->isGroupMethod();
117
        $this->assertFalse($result);
118
    }
119
120
    public function testGetGroupName()
121
    {
122
        $result = $this->classToTest->getGroupName();
123
        $this->assertFalse($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->classToTest->getGroupName() on line 122 can also be of type string; however, PHPUnit\Framework\Assert::assertFalse() 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...
124
    }
125
126
    public function testGetNarrativeTextMaxLength()
127
    {
128
        $result = $this->classToTest->getNarrativeTextMaxLength();
129
        $expected = 37;
130
        $this->assertEquals($expected, $result);
131
    }
132
}
133