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
|
|
|
], |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'multiple methods' => [ |
113
|
|
|
'ShippingMethods' => [ |
114
|
|
|
'method1' => $this->getMockShippingMethod(1), |
115
|
|
|
'method2' => $this->getMockShippingMethod(2), |
116
|
|
|
], |
117
|
|
|
'expectedResult' => [ |
118
|
|
|
'methodHandle1' => [ |
119
|
|
|
'name' => 'methodName1', |
120
|
|
|
'enabled' => null, |
121
|
|
|
'rules' => [], |
122
|
|
|
], |
123
|
|
|
'methodHandle2' => [ |
124
|
|
|
'name' => 'methodName2', |
125
|
|
|
'enabled' => null, |
126
|
|
|
'rules' => [], |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function provideValidShippingMethodDefinitions() |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
'emptyArray' => [ |
140
|
|
|
'methodDefinitions' => [], |
141
|
|
|
], |
142
|
|
|
'single method' => [ |
143
|
|
|
'methodDefinitions' => [ |
144
|
|
|
'methodHandle1' => [ |
145
|
|
|
'name' => 'methodName1', |
146
|
|
|
'enabled' => null, |
147
|
|
|
'rules' => null, |
148
|
|
|
], |
149
|
|
|
], |
150
|
|
|
], |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//============================================================================================================== |
155
|
|
|
//================================================= MOCKS ==================================================== |
156
|
|
|
//============================================================================================================== |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $methodId |
160
|
|
|
* |
161
|
|
|
* @return Mock|Commerce_ShippingMethodModel |
162
|
|
|
*/ |
163
|
|
|
private function getMockShippingMethod($methodId) |
164
|
|
|
{ |
165
|
|
|
$mockShippingMethod = $this->getMockBuilder(Commerce_ShippingMethodModel::class) |
166
|
|
|
->disableOriginalConstructor() |
167
|
|
|
->getMock(); |
168
|
|
|
|
169
|
|
|
$mockShippingMethod->expects($this->any()) |
170
|
|
|
->method('__get') |
171
|
|
|
->willReturnMap([ |
172
|
|
|
['id', $methodId], |
173
|
|
|
['handle', 'methodHandle'.$methodId], |
174
|
|
|
['name', 'methodName'.$methodId], |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
$mockShippingMethod->expects($this->any()) |
178
|
|
|
->method('getRules') |
179
|
|
|
->willReturn([$this->getMockShippingRule($methodId)]); |
180
|
|
|
|
181
|
|
|
$mockShippingMethod->expects($this->any()) |
182
|
|
|
->method('getAllErrors') |
183
|
|
|
->willReturn([ |
184
|
|
|
'ohnoes' => 'horrible error', |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
return $mockShippingMethod; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $ruleId |
192
|
|
|
* |
193
|
|
|
* @return Mock|Commerce_ShippingRuleModel |
194
|
|
|
*/ |
195
|
|
|
private function getMockShippingRule($ruleId) |
196
|
|
|
{ |
197
|
|
|
$mockShippingRule = $this->getMockBuilder(Commerce_ShippingRuleModel::class) |
198
|
|
|
->disableOriginalConstructor() |
199
|
|
|
->getMock(); |
200
|
|
|
|
201
|
|
|
$mockShippingRule->expects($this->any()) |
202
|
|
|
->method('__get') |
203
|
|
|
->willReturnMap([ |
204
|
|
|
['id', $ruleId], |
205
|
|
|
['name', 'ruleName'.$ruleId], |
206
|
|
|
]); |
207
|
|
|
|
208
|
|
|
$mockShippingRule->expects($this->any()) |
209
|
|
|
->method('getShippingRuleCategories') |
210
|
|
|
->willReturn([$this->getMockShippingRuleCategory($ruleId)]); |
211
|
|
|
|
212
|
|
|
return $mockShippingRule; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $categoryId |
217
|
|
|
* |
218
|
|
|
* @return Mock|Commerce_ShippingRuleCategoryModel |
219
|
|
|
*/ |
220
|
|
|
private function getMockShippingRuleCategory($categoryId) |
221
|
|
|
{ |
222
|
|
|
$mockShippingRuleCategory = $this->getMockBuilder(Commerce_ShippingRuleCategoryModel::class) |
223
|
|
|
->disableOriginalConstructor() |
224
|
|
|
->getMock(); |
225
|
|
|
|
226
|
|
|
$mockShippingRuleCategory->expects($this->any()) |
227
|
|
|
->method('__get') |
228
|
|
|
->willReturnMap([ |
229
|
|
|
['id', $categoryId], |
230
|
|
|
]); |
231
|
|
|
|
232
|
|
|
$mockShippingRuleCategory->expects($this->any()) |
233
|
|
|
->method('getCategory') |
234
|
|
|
->willReturn([$this->getMockShippingCategory($categoryId)]); |
235
|
|
|
|
236
|
|
|
return $mockShippingRuleCategory; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string $categoryId |
241
|
|
|
* |
242
|
|
|
* @return Mock|Commerce_ShippingCategoryModel |
243
|
|
|
*/ |
244
|
|
|
private function getMockShippingCategory($categoryId) |
245
|
|
|
{ |
246
|
|
|
$mockShippingCategory = $this->getMockBuilder(Commerce_ShippingCategoryModel::class) |
247
|
|
|
->disableOriginalConstructor() |
248
|
|
|
->getMock(); |
249
|
|
|
|
250
|
|
|
$mockShippingCategory->expects($this->any()) |
251
|
|
|
->method('__get') |
252
|
|
|
->willReturnMap([ |
253
|
|
|
['id', $categoryId], |
254
|
|
|
['handle', 'categoryHandle'.$categoryId], |
255
|
|
|
]); |
256
|
|
|
|
257
|
|
|
return $mockShippingCategory; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return Mock|Commerce_ShippingMethodsService |
262
|
|
|
*/ |
263
|
|
|
private function setMockShippingMethodsService() |
264
|
|
|
{ |
265
|
|
|
$mockShippingMethodsService = $this->getMockBuilder(Commerce_ShippingMethodsService::class) |
266
|
|
|
->disableOriginalConstructor() |
267
|
|
|
->setMethods(['getAllShippingMethods', 'saveShippingMethod', 'delete']) |
268
|
|
|
->getMock(); |
269
|
|
|
|
270
|
|
|
$mockShippingMethodsService->expects($this->any()) |
271
|
|
|
->method('getAllShippingMethods') |
272
|
|
|
->willReturn([]); |
273
|
|
|
|
274
|
|
|
$this->setComponent(Craft::app(), 'commerce_shippingMethods', $mockShippingMethodsService); |
275
|
|
|
|
276
|
|
|
return $mockShippingMethodsService; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return Mock|DbConnection |
281
|
|
|
*/ |
282
|
|
|
private function setMockDbConnection() |
283
|
|
|
{ |
284
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
285
|
|
|
->disableOriginalConstructor() |
286
|
|
|
->setMethods(['createCommand']) |
287
|
|
|
->getMock(); |
288
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
289
|
|
|
|
290
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
291
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
292
|
|
|
|
293
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
294
|
|
|
|
295
|
|
|
return $mockDbConnection; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return Mock|DbCommand |
300
|
|
|
*/ |
301
|
|
|
private function getMockDbCommand() |
302
|
|
|
{ |
303
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
304
|
|
|
->disableOriginalConstructor() |
305
|
|
|
->setMethods(['insertOrUpdate']) |
306
|
|
|
->getMock(); |
307
|
|
|
|
308
|
|
|
return $mockDbCommand; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|