Completed
Push — master ( b50753...137f43 )
by Florian
03:57
created

CountryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 22
loc 99
c 1
b 0
f 0
wmc 6
lcom 1
cbo 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 22 22 1
A testGetCountryNameByIso2() 0 7 1
A testGetCountryNameByIso2ReturnFalse() 0 7 1
A testGetDebitSepaCountriesEmpty() 0 13 1
A testGetDebitSepaCountries() 0 14 1
A testIsStateNeeded() 0 8 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\Country;
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 Magento\Directory\Model\Country as CoreCountry;
37
38
class CountryTest extends \PHPUnit_Framework_TestCase
39
{
40
    /**
41
     * @var ObjectManager
42
     */
43
    private $objectManager;
44
45
    /**
46
     * @var Country
47
     */
48
    private $country;
49
50
    /**
51
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $scopeConfig;
54
55
    /**
56
     * @var CoreCountry|\PHPUnit_Framework_MockObject_MockObject
57
     */
58
    private $coreCountry;
59
60 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...
61
    {
62
        $this->objectManager = new ObjectManager($this);
63
64
        $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();
65
        $context = $this->objectManager->getObject(Context::class, ['scopeConfig' => $this->scopeConfig]);
66
67
        $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock();
68
        $store->method('getCode')->willReturn(null);
69
70
        $storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
71
        $storeManager->method('getStore')->willReturn($store);
72
73
        $this->coreCountry = $this->getMockBuilder(CoreCountry::class)->disableOriginalConstructor()->getMock();
74
        $this->coreCountry->method('getName')->willReturn('Deutschland');
75
76
        $this->country = $this->objectManager->getObject(Country::class, [
77
            'context' => $context,
78
            'storeManager' => $storeManager,
79
            'country' => $this->coreCountry
80
        ]);
81
    }
82
83
    public function testGetCountryNameByIso2()
84
    {
85
        $this->coreCountry->method('loadByCode')->willReturn($this->coreCountry);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Magento\Directory\Model\Country. Did you maybe mean getDataUsingMethod()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
        $result = $this->country->getCountryNameByIso2('DE');
87
        $expected = 'Deutschland';
88
        $this->assertEquals($expected, $result);
89
    }
90
91
    public function testGetCountryNameByIso2ReturnFalse()
92
    {
93
        $this->coreCountry->method('loadByCode')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Magento\Directory\Model\Country. Did you maybe mean getDataUsingMethod()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
94
        $result = $this->country->getCountryNameByIso2('XY');
95
        $expected = false;
96
        $this->assertEquals($expected, $result);
97
    }
98
99
    public function testGetDebitSepaCountriesEmpty()
100
    {
101
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Magento\Framework...g\ScopeConfigInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            ->method('getValue')
103
            ->willReturnMap(
104
                [
105
                    ['payone_payment/payone_debit/sepa_country', ScopeInterface::SCOPE_STORE, null, null]
106
                ]
107
            );
108
        $result = $this->country->getDebitSepaCountries();
109
        $expected = [];
110
        $this->assertEquals($expected, $result);
111
    }
112
113
    public function testGetDebitSepaCountries()
114
    {
115
        $this->coreCountry->method('loadByCode')->willReturn($this->coreCountry);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Magento\Directory\Model\Country. Did you maybe mean getDataUsingMethod()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
        $this->scopeConfig->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Magento\Framework...g\ScopeConfigInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
            ->method('getValue')
118
            ->willReturnMap(
119
                [
120
                    ['payone_payment/payone_debit/sepa_country', ScopeInterface::SCOPE_STORE, null, 'DE']
121
                ]
122
            );
123
        $result = $this->country->getDebitSepaCountries();
124
        $expected = [['id' => 'DE', 'title' => 'Deutschland']];
125
        $this->assertEquals($expected, $result);
126
    }
127
128
    public function testIsStateNeeded()
129
    {
130
        $result = Country::isStateNeeded('US');
131
        $this->assertTrue($result);
132
133
        $result = Country::isStateNeeded('NL');
134
        $this->assertFalse($result);
135
    }
136
}
137