Completed
Push — master ( d441ee...3cdbd3 )
by Florian
03:18
created

HostedIframeTest::testGetHostedFieldConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 95
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 95
rs 8.4117
cc 1
eloc 78
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helper;
28
29
use Payone\Core\Helper\HostedIframe;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Store\Model\StoreManagerInterface;
32
use Magento\Store\Api\Data\StoreInterface;
33
use Magento\Framework\App\Helper\Context;
34
use Magento\Store\Model\ScopeInterface;
35
use Magento\Framework\App\Config\ScopeConfigInterface;
36
use Payone\Core\Helper\Payment;
37
use Payone\Core\Helper\Shop;
38
use Payone\Core\Helper\Toolkit;
39
use Payone\Core\Test\Unit\BaseTestCase;
40
use Payone\Core\Model\Test\PayoneObjectManager;
41
42
class HostedIframeTest extends BaseTestCase
43
{
44
    /**
45
     * @var ObjectManager|PayoneObjectManager
46
     */
47
    private $objectManager;
48
49
    /**
50
     * @var HostedIframe
51
     */
52
    private $hostedIframe;
53
54
    /**
55
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
56
     */
57
    private $scopeConfig;
58
59
    /**
60
     * @var Toolkit
61
     */
62
    private $toolkitHelper;
63
64
    protected function setUp()
65
    {
66
        $this->objectManager = $this->getObjectManager();
67
68
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
69
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
70
71
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
72
        $store->method('getCode')->willReturn(null);
73
74
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
75
        $storeManager->method('getStore')->willReturn($store);
76
77
        $paymentHelper = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
78
        $paymentHelper->method('isCheckCvcActive')->willReturn(true);
79
80
        $this->toolkitHelper = $this->objectManager->getObject(Toolkit::class, [
81
            'shopHelper' => $this->objectManager->getObject(Shop::class)
82
        ]);
83
84
        $this->hostedIframe = $this->objectManager->getObject(HostedIframe::class, [
85
            'context' => $context,
86
            'storeManager' => $storeManager,
87
            'paymentHelper' => $paymentHelper,
88
            'toolkitHelper' => $this->toolkitHelper
89
        ]);
90
    }
91
92
    public function testGetHostedFieldConfigEmpty()
93
    {
94
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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...
95
            ->method('getValue')
96
            ->willReturnMap(
97
                [
98
                    ['payone_general/creditcard/cc_template', ScopeInterface::SCOPE_STORE, null, $this->toolkitHelper->serialize('string')]
99
                ]
100
            );
101
        $result = $this->hostedIframe->getHostedFieldConfig();
102
        $expected = [];
103
        $this->assertEquals($expected, $result);
104
    }
105
106
    public function testGetHostedFieldConfig()
107
    {
108
        $aHostedConfig = [
109
            "Number_type" => "tel",
110
            "Number_count" => "30",
111
            "Number_max" => "16",
112
            "Number_iframe" => "standard",
113
            "Number_style" => "custom",
114
            "Number_css" => "",
115
            "CVC_type" => "tel",
116
            "CVC_count" => "30",
117
            "CVC_max" => "4",
118
            "CVC_iframe" => "standard",
119
            "CVC_style" => "custom",
120
            "CVC_css" => "",
121
            "Month_type" => "select",
122
            "Month_count" => "3",
123
            "Month_max" => "2",
124
            "Month_iframe" => "custom",
125
            "Month_width" => "120px",
126
            "Month_height" => "20px",
127
            "Month_style" => "standard",
128
            "Year_type" => "select",
129
            "Year_count" => "5",
130
            "Year_max" => "4",
131
            "Year_iframe" => "custom",
132
            "Year_width" => "120px",
133
            "Year_height" => "20px",
134
            "Year_style" => "standard",
135
            "Standard_input" => "",
136
            "Standard_selection" => "width:100px;",
137
            "Iframe_width" => "365px",
138
            "Iframe_height" => "30px",
139
            "Errors_active" => "true",
140
            "Errors_lang" => "de"
141
        ];
142
143
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Magento\Framework\App\Config\ScopeConfigInterface.

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...
144
            ->method('getValue')
145
            ->willReturnMap(
146
                [
147
                    ['payone_general/creditcard/cc_template', ScopeInterface::SCOPE_STORE, null, $this->toolkitHelper->serialize($aHostedConfig)]
148
                ]
149
            );
150
        $result = $this->hostedIframe->getHostedFieldConfig();
151
        $expected = [
152
            'fields' => [
153
                'cardpan' => [
154
                    'selector' => 'cardpan',
155
                    'type' => 'tel',
156
                    'size' => '30',
157
                    'maxlength' => '16',
158
                    'style' => ''
159
                ],
160
                'cardcvc2' => [
161
                    'selector' => 'cardcvc2',
162
                    'type' => 'tel',
163
                    'size' => '30',
164
                    'maxlength' => '4',
165
                    'style' => ''
166
                ],
167
                'cardexpiremonth' => [
168
                    'selector' => 'cardexpiremonth',
169
                    'type' => 'select',
170
                    'size' => '3',
171
                    'maxlength' => '2',
172
                    'iframe' => [
173
                        'width' => '120px',
174
                        'height' => '20px'
175
                    ]
176
                ],
177
                'cardexpireyear' => [
178
                    'selector' => 'cardexpireyear',
179
                    'type' => 'select',
180
                    'size' => '5',
181
                    'maxlength' => '4',
182
                    'iframe' => [
183
                        'width' => '120px',
184
                        'height' => '20px'
185
                    ]
186
                ]
187
            ],
188
            'defaultStyle' => [
189
                'input' => '',
190
                'select' => 'width:100px;',
191
                'iframe' => [
192
                    'width' => '365px',
193
                    'height' => '30px'
194
                ]
195
            ],
196
            'error' => 'errorOutput',
197
            'language' => 'de'
198
        ];
199
        $this->assertEquals($expected, $result);
200
    }
201
}
202