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\Block\Onepage; |
28
|
|
|
|
29
|
|
|
use Magento\Checkout\Model\Session; |
30
|
|
|
use Payone\Core\Block\Onepage\Review as ClassToTest; |
31
|
|
|
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
32
|
|
|
use Magento\Quote\Model\Quote; |
33
|
|
|
use Magento\Customer\Model\Address\Config; |
34
|
|
|
use Magento\Framework\DataObject; |
35
|
|
|
use Magento\Customer\Block\Address\Renderer\RendererInterface; |
36
|
|
|
use Magento\Quote\Model\Quote\Address; |
37
|
|
|
use Magento\Framework\View\Element\Template\Context; |
38
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface; |
39
|
|
|
use Magento\Tax\Helper\Data; |
40
|
|
|
use Magento\Framework\Pricing\PriceCurrencyInterface; |
41
|
|
|
use Magento\Store\Model\Store; |
42
|
|
|
use Magento\Framework\Escaper; |
43
|
|
|
use Magento\Quote\Model\Quote\Payment; |
44
|
|
|
use Magento\Payment\Model\MethodInterface; |
45
|
|
|
use Magento\Framework\UrlInterface; |
46
|
|
|
use Magento\Framework\Event\ManagerInterface; |
47
|
|
|
|
48
|
|
|
class ReviewTest extends \PHPUnit_Framework_TestCase |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var ClassToTest |
52
|
|
|
*/ |
53
|
|
|
private $classToTest; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ObjectManager |
57
|
|
|
*/ |
58
|
|
|
private $objectManager; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var Quote|\PHPUnit_Framework_MockObject_MockObject |
62
|
|
|
*/ |
63
|
|
|
private $quote; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject |
67
|
|
|
*/ |
68
|
|
|
private $scopeConfig; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var Data|\PHPUnit_Framework_MockObject_MockObject |
72
|
|
|
*/ |
73
|
|
|
private $taxHelper; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject |
77
|
|
|
*/ |
78
|
|
|
private $priceCurrency; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var Store|\PHPUnit_Framework_MockObject_MockObject |
82
|
|
|
*/ |
83
|
|
|
private $store; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject |
87
|
|
|
*/ |
88
|
|
|
private $urlBuilder; |
89
|
|
|
|
90
|
|
|
protected function setUp() |
91
|
|
|
{ |
92
|
|
|
$this->objectManager = new ObjectManager($this); |
93
|
|
|
|
94
|
|
|
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock(); |
95
|
|
|
|
96
|
|
|
$escaper = $this->getMockBuilder(Escaper::class)->disableOriginalConstructor()->getMock(); |
97
|
|
|
$escaper->method('escapeHtml')->willReturn('html'); |
98
|
|
|
|
99
|
|
|
$this->urlBuilder = $this->getMockBuilder(UrlInterface::class)->disableOriginalConstructor()->getMock(); |
100
|
|
|
|
101
|
|
|
$eventManager = $this->getMockBuilder(ManagerInterface::class)->disableOriginalConstructor()->getMock(); |
102
|
|
|
|
103
|
|
|
$context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); |
104
|
|
|
$context->method('getScopeConfig')->willReturn($this->scopeConfig); |
105
|
|
|
$context->method('getEscaper')->willReturn($escaper); |
106
|
|
|
$context->method('getUrlBuilder')->willReturn($this->urlBuilder); |
107
|
|
|
$context->method('getEventManager')->willReturn($eventManager); |
108
|
|
|
|
109
|
|
|
$this->store = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); |
110
|
|
|
|
111
|
|
|
$this->quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); |
112
|
|
|
$this->quote->method('getStore')->willReturn($this->store); |
113
|
|
|
|
114
|
|
|
$checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock(); |
115
|
|
|
$checkoutSession->method('getQuote')->willReturn($this->quote); |
116
|
|
|
|
117
|
|
|
$renderer = $this->getMockBuilder(RendererInterface::class)->disableOriginalConstructor()->getMock(); |
118
|
|
|
$renderer->method('renderArray')->willReturn('address'); |
119
|
|
|
|
120
|
|
|
$object = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->setMethods(['getRenderer'])->getMock(); |
121
|
|
|
$object->method('getRenderer')->willReturn($renderer); |
122
|
|
|
|
123
|
|
|
$addressConfig = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock(); |
124
|
|
|
$addressConfig->method('getFormatByCode')->willReturn($object); |
125
|
|
|
|
126
|
|
|
$this->taxHelper = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock(); |
127
|
|
|
$this->taxHelper->method('displayShippingPriceIncludingTax')->willReturn(false); |
128
|
|
|
$this->taxHelper->method('displayShippingBothPrices')->willReturn(true); |
129
|
|
|
|
130
|
|
|
$this->priceCurrency = $this->getMockBuilder(PriceCurrencyInterface::class)->disableOriginalConstructor()->getMock(); |
131
|
|
|
|
132
|
|
|
$this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ |
133
|
|
|
'context' => $context, |
134
|
|
|
'checkoutSession' => $checkoutSession, |
135
|
|
|
'addressConfig' => $addressConfig, |
136
|
|
|
'taxHelper' => $this->taxHelper, |
137
|
|
|
'priceCurrency' => $this->priceCurrency |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
public function testGetShippingAddress() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$expected = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
144
|
|
|
$this->quote->method('getShippingAddress')->willReturn($expected); |
145
|
|
|
$this->quote->method('getIsVirtual')->willReturn(false); |
146
|
|
|
$result = $this->classToTest->getShippingAddress(); |
147
|
|
|
$this->assertEquals($expected, $result); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testGetShippingAddressVirtual() |
151
|
|
|
{ |
152
|
|
|
$this->quote->method('getIsVirtual')->willReturn(true); |
153
|
|
|
$result = $this->classToTest->getShippingAddress(); |
154
|
|
|
$this->assertFalse($result); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
public function testRenderAddress() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
160
|
|
|
$address->method('getData')->willReturn(['key' => 'value']); |
161
|
|
|
$result = $this->classToTest->renderAddress($address); |
162
|
|
|
$expected = 'address'; // see setUp method |
163
|
|
|
$this->assertEquals($expected, $result); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testGetCarrierName() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$expected = 'carrier'; |
169
|
|
|
$this->scopeConfig->method('getValue')->willReturn($expected); |
170
|
|
|
$result = $this->classToTest->getCarrierName('test'); |
171
|
|
|
$this->assertEquals($expected, $result); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
public function testGetCarrierNameNoCarrier() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$expected = 'test'; |
177
|
|
|
$this->scopeConfig->method('getValue')->willReturn(false); |
178
|
|
|
$result = $this->classToTest->getCarrierName($expected); |
179
|
|
|
$this->assertEquals($expected, $result); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
public function testRenderShippingRateValue() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$object = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->setMethods(['getErrorMessage'])->getMock(); |
185
|
|
|
$object->method('getErrorMessage')->willReturn('error'); |
186
|
|
|
$result = $this->classToTest->renderShippingRateValue($object); |
187
|
|
|
$this->assertEquals('', $result); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
View Code Duplication |
public function testRenderShippingRateValueNoError() |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
$expected = 'code'; |
193
|
|
|
$object = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->setMethods(['getErrorMessage', 'getCode'])->getMock(); |
194
|
|
|
$object->method('getErrorMessage')->willReturn(false); |
195
|
|
|
$object->method('getCode')->willReturn($expected); |
196
|
|
|
$result = $this->classToTest->renderShippingRateValue($object); |
197
|
|
|
$this->assertEquals($expected, $result); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testRenderShippingRateOption() |
201
|
|
|
{ |
202
|
|
|
$object = $this->getMockBuilder(DataObject::class) |
203
|
|
|
->disableOriginalConstructor() |
204
|
|
|
->setMethods(['getErrorMessage', 'getMethodTitle', 'getPrice']) |
205
|
|
|
->getMock(); |
206
|
|
|
$object->method('getErrorMessage')->willReturn(false); |
207
|
|
|
$object->method('getMethodTitle')->willReturn('Free Shipping'); |
208
|
|
|
$object->method('getPrice')->willReturn(5); |
209
|
|
|
|
210
|
|
|
$this->taxHelper->expects($this->any()) |
211
|
|
|
->method('getShippingPrice') |
212
|
|
|
->willReturnMap([[5, false, null, null, null, 5], [5, true, null, null, null, 6]]); |
213
|
|
|
$this->priceCurrency->expects($this->any()) |
|
|
|
|
214
|
|
|
->method('convertAndFormat') |
215
|
|
|
->willReturnMap([ |
216
|
|
|
[5, true, PriceCurrencyInterface::DEFAULT_PRECISION, $this->store, null, 5], |
217
|
|
|
[6, true, PriceCurrencyInterface::DEFAULT_PRECISION, $this->store, null, 6] |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
$result = $this->classToTest->renderShippingRateOption($object); |
221
|
|
|
$expected = 'html - 5 (html 6)'; |
222
|
|
|
$this->assertEquals($expected, $result); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testRenderShippingRateOptionError() |
226
|
|
|
{ |
227
|
|
|
$object = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->setMethods(['getErrorMessage', 'getMethodTitle'])->getMock(); |
228
|
|
|
$object->method('getErrorMessage')->willReturn('error'); |
229
|
|
|
$object->method('getMethodTitle')->willReturn('Free Shipping'); |
230
|
|
|
$result = $this->classToTest->renderShippingRateOption($object); |
231
|
|
|
$expected = 'html - error'; |
232
|
|
|
$this->assertEquals($expected, $result); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testGetCurrentShippingRate() |
236
|
|
|
{ |
237
|
|
|
$result = $this->classToTest->getCurrentShippingRate(); |
238
|
|
|
$this->assertNull($result); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testGetEmail() |
242
|
|
|
{ |
243
|
|
|
$expected = '[email protected]'; |
244
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
245
|
|
|
$address->method('getEmail')->willReturn($expected); |
246
|
|
|
$this->quote->method('getBillingAddress')->willReturn($address); |
247
|
|
|
$result = $this->classToTest->getEmail(); |
248
|
|
|
$this->assertEquals($expected, $result); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function testToHtml() |
252
|
|
|
{ |
253
|
|
|
$infoInstance = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock(); |
254
|
|
|
$infoInstance->method('getTitle')->willReturn('Payone'); |
255
|
|
|
|
256
|
|
|
$payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); |
257
|
|
|
$payment->method('getMethodInstance')->willReturn($infoInstance); |
258
|
|
|
|
259
|
|
|
$rate = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->setMethods(['getCode'])->getMock(); |
260
|
|
|
$rate->method('getCode')->willReturn('free'); |
261
|
|
|
|
262
|
|
|
$rate2 = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->setMethods(['getCode'])->getMock(); |
263
|
|
|
$rate2->method('getCode')->willReturn('not_free'); |
264
|
|
|
|
265
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
266
|
|
|
$address->method('getGroupedAllShippingRates')->willReturn([[$rate2, $rate]]); |
267
|
|
|
$address->method('getShippingMethod')->willReturn('free'); |
268
|
|
|
|
269
|
|
|
$this->quote->method('getPayment')->willReturn($payment); |
270
|
|
|
$this->quote->method('getIsVirtual')->willReturn(false); |
271
|
|
|
$this->quote->method('getShippingAddress')->willReturn($address); |
272
|
|
|
|
273
|
|
|
$result = $this->classToTest->toHtml(); |
274
|
|
|
$expected = ''; |
275
|
|
|
$this->assertEquals($expected, $result); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testToHtmlVirtual() |
279
|
|
|
{ |
280
|
|
|
$infoInstance = $this->getMockBuilder(MethodInterface::class)->disableOriginalConstructor()->getMock(); |
281
|
|
|
$infoInstance->method('getTitle')->willReturn('Payone'); |
282
|
|
|
|
283
|
|
|
$payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); |
284
|
|
|
$payment->method('getMethodInstance')->willReturn($infoInstance); |
285
|
|
|
|
286
|
|
|
$this->quote->method('getPayment')->willReturn($payment); |
287
|
|
|
$this->quote->method('getIsVirtual')->willReturn(true); |
288
|
|
|
|
289
|
|
|
$result = $this->classToTest->toHtml(); |
290
|
|
|
$expected = ''; |
291
|
|
|
$this->assertEquals($expected, $result); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
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.