|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
|
6
|
|
|
use Craft\Commerce_ShippingCategoryModel; |
|
7
|
|
|
use Craft\Commerce_ShippingMethodModel; |
|
8
|
|
|
use Craft\Commerce_ShippingRuleModel; |
|
9
|
|
|
use Craft\Commerce_ShippingRuleCategoryModel; |
|
10
|
|
|
use Craft\Commerce_ShippingMethodsService; |
|
11
|
|
|
use Craft\Craft; |
|
12
|
|
|
use Craft\DbCommand; |
|
13
|
|
|
use Craft\DbConnection; |
|
14
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
|
15
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ShippingMethodsTest. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Nerds & Company |
|
21
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
|
22
|
|
|
* @license MIT |
|
23
|
|
|
* |
|
24
|
|
|
* @see http://www.nerds.company |
|
25
|
|
|
* |
|
26
|
|
|
* @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\ShippingMethods |
|
27
|
|
|
* @covers ::__construct |
|
28
|
|
|
* @covers ::<!public> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ShippingMethodsTest extends BaseTest |
|
31
|
|
|
{ |
|
32
|
|
|
//============================================================================================================== |
|
33
|
|
|
//================================================= TESTS ==================================================== |
|
34
|
|
|
//============================================================================================================== |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @covers ::export |
|
38
|
|
|
* @dataProvider provideValidShippingMethods |
|
39
|
|
|
* |
|
40
|
|
|
* @param ShippingMethodModel[] $methods |
|
41
|
|
|
* @param array $expectedResult |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testSuccessfulExport(array $methods, array $expectedResult = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$schematicShippingMethodsService = new ShippingMethods(); |
|
46
|
|
|
|
|
47
|
|
|
$actualResult = $schematicShippingMethodsService->export($methods); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @covers ::import |
|
54
|
|
|
* @dataProvider provideValidShippingMethodDefinitions |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $methodDefinitions |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testSuccessfulImport(array $methodDefinitions) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->setMockShippingMethodsService(); |
|
61
|
|
|
$this->setMockDbConnection(); |
|
62
|
|
|
|
|
63
|
|
|
$schematicShippingMethodsService = new ShippingMethods(); |
|
64
|
|
|
|
|
65
|
|
|
$import = $schematicShippingMethodsService->import($methodDefinitions); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertInstanceOf(Result::class, $import); |
|
68
|
|
|
$this->assertFalse($import->hasErrors()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @covers ::import |
|
73
|
|
|
* @dataProvider provideValidShippingMethodDefinitions |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $methodDefinitions |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testImportWithForceOption(array $methodDefinitions) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->setMockShippingMethodsService(); |
|
80
|
|
|
$this->setMockDbConnection(); |
|
81
|
|
|
|
|
82
|
|
|
$schematicShippingMethodsService = new ShippingMethods(); |
|
83
|
|
|
|
|
84
|
|
|
$import = $schematicShippingMethodsService->import($methodDefinitions, true); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertInstanceOf(Result::class, $import); |
|
87
|
|
|
$this->assertFalse($import->hasErrors()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
//============================================================================================================== |
|
91
|
|
|
//============================================== PROVIDERS =================================================== |
|
92
|
|
|
//============================================================================================================== |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function provideValidShippingMethods() |
|
98
|
|
|
{ |
|
99
|
|
|
return [ |
|
100
|
|
|
'single method' => [ |
|
101
|
|
|
'ShippingMethods' => [ |
|
102
|
|
|
'method1' => $this->getMockShippingMethod(1), |
|
103
|
|
|
], |
|
104
|
|
|
'expectedResult' => [ |
|
105
|
|
|
'methodHandle1' => [ |
|
106
|
|
|
'name' => 'methodName1', |
|
107
|
|
|
'enabled' => null, |
|
108
|
|
|
'rules' => [ |
|
109
|
|
|
'ruleName1' => [ |
|
110
|
|
|
'name' => 'ruleName1', |
|
111
|
|
|
'description' => null, |
|
112
|
|
|
'shippingZone' => null, |
|
113
|
|
|
'priority' => null, |
|
114
|
|
|
'enabled' => null, |
|
115
|
|
|
'minQty' => null, |
|
116
|
|
|
'maxQty' => null, |
|
117
|
|
|
'minTotal' => null, |
|
118
|
|
|
'maxTotal' => null, |
|
119
|
|
|
'minWeight' => null, |
|
120
|
|
|
'maxWeight' => null, |
|
121
|
|
|
'baseRate' => null, |
|
122
|
|
|
'perItemRate' => null, |
|
123
|
|
|
'weightRate' => null, |
|
124
|
|
|
'percentageRate' => null, |
|
125
|
|
|
'minRate' => null, |
|
126
|
|
|
'maxRate' => null, |
|
127
|
|
|
'categories' => [ |
|
128
|
|
|
'categoryHandle1' => [ |
|
129
|
|
|
'condition' => null, |
|
130
|
|
|
'perItemRate' => null, |
|
131
|
|
|
'weightRate' => null, |
|
132
|
|
|
'percentageRate' => null, |
|
133
|
|
|
], |
|
134
|
|
|
], |
|
135
|
|
|
], |
|
136
|
|
|
], |
|
137
|
|
|
], |
|
138
|
|
|
], |
|
139
|
|
|
], |
|
140
|
|
|
'multiple methods' => [ |
|
141
|
|
|
'ShippingMethods' => [ |
|
142
|
|
|
'method1' => $this->getMockShippingMethod(1), |
|
143
|
|
|
'method2' => $this->getMockShippingMethod(2), |
|
144
|
|
|
], |
|
145
|
|
|
'expectedResult' => [ |
|
146
|
|
|
'methodHandle1' => [ |
|
147
|
|
|
'name' => 'methodName1', |
|
148
|
|
|
'enabled' => null, |
|
149
|
|
|
'rules' => [ |
|
150
|
|
|
'ruleName1' => [ |
|
151
|
|
|
'name' => 'ruleName1', |
|
152
|
|
|
'description' => null, |
|
153
|
|
|
'shippingZone' => null, |
|
154
|
|
|
'priority' => null, |
|
155
|
|
|
'enabled' => null, |
|
156
|
|
|
'minQty' => null, |
|
157
|
|
|
'maxQty' => null, |
|
158
|
|
|
'minTotal' => null, |
|
159
|
|
|
'maxTotal' => null, |
|
160
|
|
|
'minWeight' => null, |
|
161
|
|
|
'maxWeight' => null, |
|
162
|
|
|
'baseRate' => null, |
|
163
|
|
|
'perItemRate' => null, |
|
164
|
|
|
'weightRate' => null, |
|
165
|
|
|
'percentageRate' => null, |
|
166
|
|
|
'minRate' => null, |
|
167
|
|
|
'maxRate' => null, |
|
168
|
|
|
'categories' => [ |
|
169
|
|
|
'categoryHandle1' => [ |
|
170
|
|
|
'condition' => null, |
|
171
|
|
|
'perItemRate' => null, |
|
172
|
|
|
'weightRate' => null, |
|
173
|
|
|
'percentageRate' => null, |
|
174
|
|
|
], |
|
175
|
|
|
], |
|
176
|
|
|
], |
|
177
|
|
|
], |
|
178
|
|
|
], |
|
179
|
|
|
'methodHandle2' => [ |
|
180
|
|
|
'name' => 'methodName2', |
|
181
|
|
|
'enabled' => null, |
|
182
|
|
|
'rules' => [ |
|
183
|
|
|
'ruleName2' => [ |
|
184
|
|
|
'name' => 'ruleName2', |
|
185
|
|
|
'description' => null, |
|
186
|
|
|
'shippingZone' => null, |
|
187
|
|
|
'priority' => null, |
|
188
|
|
|
'enabled' => null, |
|
189
|
|
|
'minQty' => null, |
|
190
|
|
|
'maxQty' => null, |
|
191
|
|
|
'minTotal' => null, |
|
192
|
|
|
'maxTotal' => null, |
|
193
|
|
|
'minWeight' => null, |
|
194
|
|
|
'maxWeight' => null, |
|
195
|
|
|
'baseRate' => null, |
|
196
|
|
|
'perItemRate' => null, |
|
197
|
|
|
'weightRate' => null, |
|
198
|
|
|
'percentageRate' => null, |
|
199
|
|
|
'minRate' => null, |
|
200
|
|
|
'maxRate' => null, |
|
201
|
|
|
'categories' => [ |
|
202
|
|
|
'categoryHandle2' => [ |
|
203
|
|
|
'condition' => null, |
|
204
|
|
|
'perItemRate' => null, |
|
205
|
|
|
'weightRate' => null, |
|
206
|
|
|
'percentageRate' => null, |
|
207
|
|
|
], |
|
208
|
|
|
], |
|
209
|
|
|
], |
|
210
|
|
|
], |
|
211
|
|
|
], |
|
212
|
|
|
], |
|
213
|
|
|
], |
|
214
|
|
|
]; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return array |
|
219
|
|
|
*/ |
|
220
|
|
|
public function provideValidShippingMethodDefinitions() |
|
221
|
|
|
{ |
|
222
|
|
|
return [ |
|
223
|
|
|
'emptyArray' => [ |
|
224
|
|
|
'methodDefinitions' => [], |
|
225
|
|
|
], |
|
226
|
|
|
'single method' => [ |
|
227
|
|
|
'methodDefinitions' => [ |
|
228
|
|
|
'methodHandle1' => [ |
|
229
|
|
|
'name' => 'methodName1', |
|
230
|
|
|
'enabled' => null, |
|
231
|
|
|
'rules' => null, |
|
232
|
|
|
], |
|
233
|
|
|
], |
|
234
|
|
|
], |
|
235
|
|
|
]; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
//============================================================================================================== |
|
239
|
|
|
//================================================= MOCKS ==================================================== |
|
240
|
|
|
//============================================================================================================== |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param string $methodId |
|
244
|
|
|
* |
|
245
|
|
|
* @return Mock|Commerce_ShippingMethodModel |
|
246
|
|
|
*/ |
|
247
|
|
|
private function getMockShippingMethod($methodId) |
|
248
|
|
|
{ |
|
249
|
|
|
$mockShippingMethod = $this->getMockBuilder(Commerce_ShippingMethodModel::class) |
|
250
|
|
|
->disableOriginalConstructor() |
|
251
|
|
|
->getMock(); |
|
252
|
|
|
|
|
253
|
|
|
$mockShippingMethod->expects($this->any()) |
|
254
|
|
|
->method('__get') |
|
255
|
|
|
->willReturnMap([ |
|
256
|
|
|
['id', $methodId], |
|
257
|
|
|
['handle', 'methodHandle'.$methodId], |
|
258
|
|
|
['name', 'methodName'.$methodId], |
|
259
|
|
|
]); |
|
260
|
|
|
|
|
261
|
|
|
$mockShippingMethod->expects($this->any()) |
|
262
|
|
|
->method('getRules') |
|
263
|
|
|
->willReturn([$this->getMockShippingRule($methodId)]); |
|
264
|
|
|
|
|
265
|
|
|
$mockShippingMethod->expects($this->any()) |
|
266
|
|
|
->method('getAllErrors') |
|
267
|
|
|
->willReturn([ |
|
268
|
|
|
'ohnoes' => 'horrible error', |
|
269
|
|
|
]); |
|
270
|
|
|
|
|
271
|
|
|
return $mockShippingMethod; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @param string $ruleId |
|
276
|
|
|
* |
|
277
|
|
|
* @return Mock|Commerce_ShippingRuleModel |
|
278
|
|
|
*/ |
|
279
|
|
|
private function getMockShippingRule($ruleId) |
|
280
|
|
|
{ |
|
281
|
|
|
$mockShippingRule = $this->getMockBuilder(Commerce_ShippingRuleModel::class) |
|
282
|
|
|
->disableOriginalConstructor() |
|
283
|
|
|
->getMock(); |
|
284
|
|
|
|
|
285
|
|
|
$mockShippingRule->expects($this->any()) |
|
286
|
|
|
->method('__get') |
|
287
|
|
|
->willReturnMap([ |
|
288
|
|
|
['id', $ruleId], |
|
289
|
|
|
['name', 'ruleName'.$ruleId], |
|
290
|
|
|
]); |
|
291
|
|
|
|
|
292
|
|
|
$mockShippingRule->expects($this->any()) |
|
293
|
|
|
->method('getShippingRuleCategories') |
|
294
|
|
|
->willReturn([$this->getMockShippingRuleCategory($ruleId)]); |
|
295
|
|
|
|
|
296
|
|
|
return $mockShippingRule; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @param string $categoryId |
|
301
|
|
|
* |
|
302
|
|
|
* @return Mock|Commerce_ShippingRuleCategoryModel |
|
303
|
|
|
*/ |
|
304
|
|
|
private function getMockShippingRuleCategory($categoryId) |
|
305
|
|
|
{ |
|
306
|
|
|
$mockShippingRuleCategory = $this->getMockBuilder(Commerce_ShippingRuleCategoryModel::class) |
|
307
|
|
|
->disableOriginalConstructor() |
|
308
|
|
|
->getMock(); |
|
309
|
|
|
|
|
310
|
|
|
$mockShippingRuleCategory->expects($this->any()) |
|
311
|
|
|
->method('__get') |
|
312
|
|
|
->willReturnMap([ |
|
313
|
|
|
['id', $categoryId], |
|
314
|
|
|
]); |
|
315
|
|
|
|
|
316
|
|
|
$mockShippingRuleCategory->expects($this->any()) |
|
317
|
|
|
->method('getCategory') |
|
318
|
|
|
->willReturn($this->getMockShippingCategory($categoryId)); |
|
319
|
|
|
|
|
320
|
|
|
return $mockShippingRuleCategory; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param string $categoryId |
|
325
|
|
|
* |
|
326
|
|
|
* @return Mock|Commerce_ShippingCategoryModel |
|
327
|
|
|
*/ |
|
328
|
|
|
private function getMockShippingCategory($categoryId) |
|
329
|
|
|
{ |
|
330
|
|
|
$mockShippingCategory = $this->getMockBuilder(Commerce_ShippingCategoryModel::class) |
|
331
|
|
|
->disableOriginalConstructor() |
|
332
|
|
|
->getMock(); |
|
333
|
|
|
|
|
334
|
|
|
$mockShippingCategory->expects($this->any()) |
|
335
|
|
|
->method('__get') |
|
336
|
|
|
->willReturnMap([ |
|
337
|
|
|
['id', $categoryId], |
|
338
|
|
|
['handle', 'categoryHandle'.$categoryId], |
|
339
|
|
|
]); |
|
340
|
|
|
|
|
341
|
|
|
return $mockShippingCategory; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* @return Mock|Commerce_ShippingMethodsService |
|
346
|
|
|
*/ |
|
347
|
|
|
private function setMockShippingMethodsService() |
|
348
|
|
|
{ |
|
349
|
|
|
$mockShippingMethodsService = $this->getMockBuilder(Commerce_ShippingMethodsService::class) |
|
350
|
|
|
->disableOriginalConstructor() |
|
351
|
|
|
->setMethods(['getAllShippingMethods', 'saveShippingMethod', 'delete']) |
|
352
|
|
|
->getMock(); |
|
353
|
|
|
|
|
354
|
|
|
$mockShippingMethodsService->expects($this->any()) |
|
355
|
|
|
->method('getAllShippingMethods') |
|
356
|
|
|
->willReturn([]); |
|
357
|
|
|
|
|
358
|
|
|
$this->setComponent(Craft::app(), 'commerce_shippingMethods', $mockShippingMethodsService); |
|
359
|
|
|
|
|
360
|
|
|
return $mockShippingMethodsService; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @return Mock|DbConnection |
|
365
|
|
|
*/ |
|
366
|
|
|
private function setMockDbConnection() |
|
367
|
|
|
{ |
|
368
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
|
369
|
|
|
->disableOriginalConstructor() |
|
370
|
|
|
->setMethods(['createCommand']) |
|
371
|
|
|
->getMock(); |
|
372
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
|
373
|
|
|
|
|
374
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
|
375
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
|
376
|
|
|
|
|
377
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
|
378
|
|
|
|
|
379
|
|
|
return $mockDbConnection; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* @return Mock|DbCommand |
|
384
|
|
|
*/ |
|
385
|
|
|
private function getMockDbCommand() |
|
386
|
|
|
{ |
|
387
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
|
388
|
|
|
->disableOriginalConstructor() |
|
389
|
|
|
->setMethods(['insertOrUpdate']) |
|
390
|
|
|
->getMock(); |
|
391
|
|
|
|
|
392
|
|
|
return $mockDbCommand; |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
|