OrderTestTest::testGetShippingMethodFalse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 9
c 1
b 0
f 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
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
use Payone\Core\Test\Unit\BaseTestCase;
42
use Payone\Core\Model\Test\PayoneObjectManager;
43
44
class OrderTestTest extends BaseTestCase
45
{
46
    /**
47
     * @var ObjectManager|PayoneObjectManager
48
     */
49
    private $objectManager;
50
51
    /**
52
     * @var Order
53
     */
54
    private $order;
55
56
    /**
57
     * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
58
     */
59
    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...
60
61
    /**
62
     * @var OrderCore|\PHPUnit_Framework_MockObject_MockObject
63
     */
64
    private $orderCore;
65
66
    /**
67
     * @var Customer|\PHPUnit_Framework_MockObject_MockObject
68
     */
69
    private $customerHelper;
70
71
    protected function setUp()
72
    {
73
        $this->objectManager = $this->getObjectManager();
74
75
        $databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock();
76
        $databaseHelper->method('getOrderIncrementIdByTxid')->willReturn('000000001');
77
78
        $this->customerHelper = $this->getMockBuilder(Customer::class)->disableOriginalConstructor()->getMock();
79
80
        $this->orderCore = $this->getMockBuilder(OrderCore::class)->disableOriginalConstructor()->getMock();
81
        $this->orderCore->method('loadByIncrementId')->willReturn($this->orderCore);
82
        $orderFactory = $this->getMockBuilder(OrderFactory::class)
83
            ->disableOriginalConstructor()
84
            ->setMethods(['create'])
85
            ->getMock();
86
        $orderFactory->method('create')->willReturn($this->orderCore);
87
88
        $this->order = $this->objectManager->getObject(Order::class, [
89
            'databaseHelper' => $databaseHelper,
90
            'customerHelper' => $this->customerHelper,
91
            'orderFactory' => $orderFactory
92
        ]);
93
    }
94
95
    public function testGetOrderByTxid()
96
    {
97
        $this->orderCore->method('getId')->willReturn('1');
98
99
        $result = $this->order->getOrderByTxid('238');
100
        $this->assertInstanceOf(OrderCore::class, $result);
101
    }
102
103
    public function testGetOrderByTxidNull()
104
    {
105
        $this->orderCore->method('getId')->willReturn(false);
106
107
        $result = $this->order->getOrderByTxid('238');
108
        $this->assertNull($result);
109
    }
110
111
    public function testGetShippingMethod()
112
    {
113
        $expected = 'free_free';
114
115
        $rate1 = $this->getMockBuilder(Rate::class)
116
            ->disableOriginalConstructor()
117
            ->setMethods(['getPrice', 'getCode'])
118
            ->getMock();
119
        $rate2 = clone $rate1;
120
        $rate1->method('getPrice')->willReturn('5.00');
121
        $rate1->method('getCode')->willReturn('not_free');
122
        $rate2->method('getPrice')->willReturn('0.00');
123
        $rate2->method('getCode')->willReturn($expected);
124
        $rates = [
125
            'key' => [
126
                $rate1,
127
                $rate2
128
            ]
129
        ];
130
131
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
132
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
133
        $address->method('getGroupedAllShippingRates')->willReturn($rates);
134
135
        $result = $this->order->getShippingMethod($quote, $address);
136
        $this->assertEquals($expected, $result);
137
    }
138
139 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...
140
    {
141
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
142
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
143
        $address->method('getGroupedAllShippingRates')->willReturn([]);
144
145
        $result = $this->order->getShippingMethod($quote, $address);
146
        $this->assertFalse($result);
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->order->getShippingMethod($quote, $address) on line 145 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...
147
    }
148
149
    public function testSetShippingMethod()
150
    {
151
        $rate = $this->getMockBuilder(Rate::class)
152
            ->disableOriginalConstructor()
153
            ->setMethods(['getPrice', 'getCode'])
154
            ->getMock();
155
        $rate->method('getPrice')->willReturn('0.00');
156
        $rate->method('getCode')->willReturn('free_free');
157
        $rates = ['key' => [$rate]];
158
159
        $expected = 'free_free';
160
161
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
162
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
163
        $address->method('getGroupedAllShippingRates')->willReturn($rates);
164
        $address->method('getShippingMethod')->willReturn($expected);
165
166
        $result = $this->order->setShippingMethod($address, $quote);
167
        $this->assertEquals($expected, $result->getShippingMethod());
168
    }
169
170 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...
171
    {
172
        $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock();
173
        $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
174
        $address->method('getGroupedAllShippingRates')->willReturn([]);
175
176
        $this->expectException(LocalizedException::class);
177
        $this->order->setShippingMethod($address, $quote);
178
    }
179
180
    public function testFillSingleAddress()
181
    {
182
        $address = $this->objectManager->getObject(Address::class);
183
        $firstname = 'Paul';
184
        $lastname = 'Tester';
185
        $street = 'Washington Blvd 13';
186
        $city = 'San Diego';
187
        $zip = '12345';
188
        $country = 'US';
189
        $state = 'CA';
190
        $regionId = '5';
191
192
        $region = $this->getMockBuilder(Region::class)->disableOriginalConstructor()->getMock();
193
        $region->method('getId')->willReturn($regionId);
194
        $this->customerHelper->method('getRegion')->willReturn($region);
195
196
        $result = $this->order->fillSingleAddress($address, $firstname, $lastname, $street, $city, $zip, $country, $state);
197
        $this->assertEquals($firstname, $result->getFirstname());
198
        $this->assertEquals($lastname, $result->getLastname());
199
        $this->assertEquals($street, $result->getStreet()[0]);
200
        $this->assertEquals($city, $result->getCity());
201
        $this->assertEquals($zip, $result->getPostcode());
202
        $this->assertEquals($country, $result->getCountryId());
203
        $this->assertEquals($regionId, $result->getRegionId());
204
    }
205
}
206