CustomerTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 182
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 42
loc 182
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 35 1
A testCustomerHasGivenGender() 7 7 1
A testCustomerHasGivenGenderFalse() 7 7 1
A testCustomerHasGivenBirthday() 7 7 1
A testCustomerHasGivenBirthdayFalse() 7 7 1
A testGetRegionCode() 14 14 1
A testGetRegionCodeDirectPass() 0 10 1
A testGetRegion() 0 6 1
A testGetRegionFalse() 0 5 1
A getGenders() 0 8 1
A testGetGenderParameter() 0 5 1
A getSalutations() 0 8 1
A testGetSalutationParameter() 0 5 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\Helper;
28
29
use Payone\Core\Helper\Customer;
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\Framework\App\Config\ScopeConfigInterface;
35
use Magento\Checkout\Model\Session;
36
use Magento\Customer\Api\Data\CustomerInterface;
37
use Magento\Quote\Model\Quote;
38
use Magento\Sales\Model\Order\Address;
39
use Magento\Directory\Model\RegionFactory;
40
use Magento\Directory\Model\Region;
41
use Payone\Core\Test\Unit\BaseTestCase;
42
use Payone\Core\Model\Test\PayoneObjectManager;
43
44
class CustomerTest extends BaseTestCase
45
{
46
    /**
47
     * @var ObjectManager|PayoneObjectManager
48
     */
49
    private $objectManager;
50
51
    /**
52
     * @var Customer
53
     */
54
    private $customer;
55
56
    /**
57
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
58
     */
59
    private $scopeConfig;
60
61
    /**
62
     * @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
63
     */
64
    private $coreCustomer;
65
66
    /**
67
     * @var Region|\PHPUnit_Framework_MockObject_MockObject
68
     */
69
    private $region;
70
71
    protected function setUp()
72
    {
73
        $this->objectManager = $this->getObjectManager();
74
75
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
76
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
77
78
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
79
        $store->method('getCode')->willReturn(null);
80
81
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
82
        $storeManager->method('getStore')->willReturn($store);
83
84
        $this->coreCustomer = $this->getMockBuilder(CustomerInterface::class)->disableOriginalConstructor()->getMock();
85
86
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
87
        $quote->method('getCustomer')->willReturn($this->coreCustomer);
88
89
        $checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
90
        $checkoutSession->method('getQuote')->willReturn($quote);
91
92
        $this->region = $this->getMockBuilder(Region::class)
93
            ->disableOriginalConstructor()
94
            ->setMethods(['getId', 'getCode', 'loadByName', 'loadByCode'])
95
            ->getMock();
96
        $regionFactory = $this->getMockBuilder(RegionFactory::class)->disableOriginalConstructor()->getMock();
97
        $regionFactory->method('create')->willReturn($this->region);
98
99
        $this->customer = $this->objectManager->getObject(Customer::class, [
100
            'context' => $context,
101
            'storeManager' => $storeManager,
102
            'checkoutSession' => $checkoutSession,
103
            'regionFactory' => $regionFactory
104
        ]);
105
    }
106
107 View Code Duplication
    public function testCustomerHasGivenGender()
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...
108
    {
109
        $this->coreCustomer->method('getGender')->willReturn('m');
110
        $result = $this->customer->customerHasGivenGender();
111
        $expected = true;
112
        $this->assertEquals($expected, $result);
113
    }
114
115 View Code Duplication
    public function testCustomerHasGivenGenderFalse()
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...
116
    {
117
        $this->coreCustomer->method('getGender')->willReturn(null);
118
        $result = $this->customer->customerHasGivenGender();
119
        $expected = false;
120
        $this->assertEquals($expected, $result);
121
    }
122
123 View Code Duplication
    public function testCustomerHasGivenBirthday()
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...
124
    {
125
        $this->coreCustomer->method('getDob')->willReturn('1999-19-09');
126
        $result = $this->customer->customerHasGivenBirthday();
127
        $expected = true;
128
        $this->assertEquals($expected, $result);
129
    }
130
131 View Code Duplication
    public function testCustomerHasGivenBirthdayFalse()
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
        $this->coreCustomer->method('getDob')->willReturn(null);
134
        $result = $this->customer->customerHasGivenBirthday();
135
        $expected = false;
136
        $this->assertEquals($expected, $result);
137
    }
138
139 View Code Duplication
    public function testGetRegionCode()
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...
140
    {
141
        $expected = 'CA';
142
143
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
144
        $address->method('getRegionCode')->willReturn('California');
145
        $address->method('getCountryId')->willReturn('US');
146
147
        $this->region->method('getId')->willReturn('5');
148
        $this->region->method('getCode')->willReturn($expected);
149
150
        $result = $this->customer->getRegionCode($address);
151
        $this->assertEquals($expected, $result);
152
    }
153
154
    public function testGetRegionCodeDirectPass()
155
    {
156
        $expected = 'CA';
157
158
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
159
        $address->method('getRegionCode')->willReturn($expected);
160
161
        $result = $this->customer->getRegionCode($address);
162
        $this->assertEquals($expected, $result);
163
    }
164
165
    public function testGetRegion()
166
    {
167
        $this->region->method('getId')->willReturn('5');
168
        $result = $this->customer->getRegion('US', 'CA');
169
        $this->assertInstanceOf(Region::class, $result);
170
    }
171
172
    public function testGetRegionFalse()
173
    {
174
        $result = $this->customer->getRegion('US', 'CA');
175
        $this->assertFalse($result);
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function getGenders()
182
    {
183
        return [
184
            ['1', 'm'],
185
            ['2', 'f'],
186
            ['x', '']
187
        ];
188
    }
189
190
    /**
191
     * @param int $gender
192
     * @param string $expected
193
     *
194
     * @dataProvider getGenders
195
     */
196
    public function testGetGenderParameter($gender, $expected)
197
    {
198
        $result = $this->customer->getGenderParameter($gender);
199
        $this->assertEquals($expected, $result);
200
    }
201
202
    /**
203
     * @return array
204
     */
205
    public function getSalutations()
206
    {
207
        return [
208
            ['1', 'Mr'],
209
            ['2', 'Mrs'],
210
            ['x', '']
211
        ];
212
    }
213
214
    /**
215
     * @param int $gender
216
     * @param string $expected
217
     *
218
     * @dataProvider getSalutations
219
     */
220
    public function testGetSalutationParameter($gender, $expected)
221
    {
222
        $result = $this->customer->getSalutationParameter($gender);
223
        $this->assertEquals($expected, $result);
224
    }
225
}
226