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

OrderTestTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 159
Duplicated Lines 11.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 18
loc 159
c 1
b 0
f 0
wmc 8
lcom 1
cbo 2
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 1
A testGetOrderByTxid() 0 7 1
A testGetOrderByTxidNull() 0 7 1
B testGetShippingMethod() 0 27 1
A testGetShippingMethodFalse() 9 9 1
A testSetShippingMethod() 0 20 1
A testSetShippingMethodException() 9 9 1
B testFillSingleAddress() 0 25 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\Order;
30
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31
use Magento\Framework\App\Config\ScopeConfigInterface;
32
use Payone\Core\Helper\Database;
33
use Payone\Core\Helper\Customer;
34
use Magento\Sales\Model\Order as OrderCore;
35
use Magento\Sales\Model\OrderFactory;
36
use Magento\Quote\Model\Quote\Address;
37
use Magento\Framework\Exception\LocalizedException;
38
use Magento\Quote\Model\Quote;
39
use Magento\Quote\Model\ResourceModel\Quote\Address\Rate;
40
use Magento\Directory\Model\Region;
41
42
class OrderTestTest extends \PHPUnit_Framework_TestCase
43
{
44
    /**
45
     * @var ObjectManager
46
     */
47
    private $objectManager;
48
49
    /**
50
     * @var Order
51
     */
52
    private $order;
53
54
    /**
55
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
56
     */
57
    private $scopeConfig;
0 ignored issues
show
Unused Code introduced by
The property $scopeConfig is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
58
59
    /**
60
     * @var OrderCore|\PHPUnit_Framework_MockObject_MockObject
61
     */
62
    private $orderCore;
63
64
    /**
65
     * @var Customer|\PHPUnit_Framework_MockObject_MockObject
66
     */
67
    private $customerHelper;
68
69
    protected function setUp()
70
    {
71
        $this->objectManager = new ObjectManager($this);
72
73
        $databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
74
        $databaseHelper->method('getOrderIncrementIdByTxid')->willReturn('000000001');
75
76
        $this->customerHelper = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
77
78
        $this->orderCore = $this->getMockBuilder(OrderCore::class)->disableOriginalConstructor()->getMock();
79
        $this->orderCore->method('loadByIncrementId')->willReturn($this->orderCore);
80
        $orderFactory = $this->getMockBuilder(OrderFactory::class)->disableOriginalConstructor()->getMock();
81
        $orderFactory->method('create')->willReturn($this->orderCore);
82
83
        $this->order = $this->objectManager->getObject(Order::class, [
84
            'databaseHelper' => $databaseHelper,
85
            'customerHelper' => $this->customerHelper,
86
            'orderFactory' => $orderFactory
87
        ]);
88
    }
89
90
    public function testGetOrderByTxid()
91
    {
92
        $this->orderCore->method('getId')->willReturn('1');
93
94
        $result = $this->order->getOrderByTxid('238');
95
        $this->assertInstanceOf(OrderCore::class, $result);
96
    }
97
98
    public function testGetOrderByTxidNull()
99
    {
100
        $this->orderCore->method('getId')->willReturn(false);
101
102
        $result = $this->order->getOrderByTxid('238');
103
        $this->assertNull($result);
104
    }
105
106
    public function testGetShippingMethod()
107
    {
108
        $expected = 'free_free';
109
110
        $rate1 = $this->getMockBuilder(Rate::class)
111
            ->disableOriginalConstructor()
112
            ->setMethods(['getPrice', 'getCode'])
113
            ->getMock();
114
        $rate2 = clone $rate1;
115
        $rate1->method('getPrice')->willReturn('5.00');
116
        $rate1->method('getCode')->willReturn('not_free');
117
        $rate2->method('getPrice')->willReturn('0.00');
118
        $rate2->method('getCode')->willReturn($expected);
119
        $rates = [
120
            'key' => [
121
                $rate1,
122
                $rate2
123
            ]
124
        ];
125
126
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
127
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
128
        $address->method('getGroupedAllShippingRates')->willReturn($rates);
129
130
        $result = $this->order->getShippingMethod($quote, $address);
131
        $this->assertEquals($expected, $result);
132
    }
133
134 View Code Duplication
    public function testGetShippingMethodFalse()
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...
135
    {
136
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
137
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
138
        $address->method('getGroupedAllShippingRates')->willReturn([]);
139
140
        $result = $this->order->getShippingMethod($quote, $address);
141
        $this->assertFalse($result);
142
    }
143
144
    public function testSetShippingMethod()
145
    {
146
        $rate = $this->getMockBuilder(Rate::class)
147
            ->disableOriginalConstructor()
148
            ->setMethods(['getPrice', 'getCode'])
149
            ->getMock();
150
        $rate->method('getPrice')->willReturn('0.00');
151
        $rate->method('getCode')->willReturn('free_free');
152
        $rates = ['key' => [$rate]];
153
154
        $expected = 'free_free';
155
156
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
157
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
158
        $address->method('getGroupedAllShippingRates')->willReturn($rates);
159
        $address->method('getShippingMethod')->willReturn($expected);
160
161
        $result = $this->order->setShippingMethod($address, $quote);
162
        $this->assertEquals($expected, $result->getShippingMethod());
163
    }
164
165 View Code Duplication
    public function testSetShippingMethodException()
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...
166
    {
167
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
168
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
169
        $address->method('getGroupedAllShippingRates')->willReturn([]);
170
171
        $this->setExpectedException(LocalizedException::class);
172
        $this->order->setShippingMethod($address, $quote);
173
    }
174
175
    public function testFillSingleAddress()
176
    {
177
        $address = $this->objectManager->getObject(Address::class);
178
        $firstname = 'Paul';
179
        $lastname = 'Tester';
180
        $street = 'Washington Blvd 13';
181
        $city = 'San Diego';
182
        $zip = '12345';
183
        $country = 'US';
184
        $state = 'CA';
185
        $regionId = '5';
186
187
        $region = $this->getMockBuilder(Region::class)->disableOriginalConstructor()->getMock();
188
        $region->method('getId')->willReturn($regionId);
189
        $this->customerHelper->method('getRegion')->willReturn($region);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Payone\Core\Helper\Customer>.

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...
190
191
        $result = $this->order->fillSingleAddress($address, $firstname, $lastname, $street, $city, $zip, $country, $state);
192
        $this->assertEquals($firstname, $result->getFirstname());
193
        $this->assertEquals($lastname, $result->getLastname());
194
        $this->assertEquals($street, $result->getStreet()[0]);
195
        $this->assertEquals($city, $result->getCity());
196
        $this->assertEquals($zip, $result->getPostcode());
197
        $this->assertEquals($country, $result->getCountryId());
198
        $this->assertEquals($regionId, $result->getRegionId());
199
    }
200
}
201