1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\Commerce_TaxCategoryModel; |
7
|
|
|
use Craft\Commerce_TaxRateModel; |
8
|
|
|
use Craft\Commerce_TaxZoneModel; |
9
|
|
|
use Craft\Commerce_TaxCategoriesService; |
10
|
|
|
use Craft\Commerce_TaxRatesService; |
11
|
|
|
use Craft\Commerce_TaxZonesService; |
12
|
|
|
use Craft\Craft; |
13
|
|
|
use Craft\DbCommand; |
14
|
|
|
use Craft\DbConnection; |
15
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
16
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TaxRatesTest. |
20
|
|
|
* |
21
|
|
|
* @author Nerds & Company |
22
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
23
|
|
|
* @license MIT |
24
|
|
|
* |
25
|
|
|
* @see http://www.nerds.company |
26
|
|
|
* |
27
|
|
|
* @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\TaxRates |
28
|
|
|
* @covers ::__construct |
29
|
|
|
* @covers ::<!public> |
30
|
|
|
*/ |
31
|
|
|
class TaxRatesTest extends BaseTest |
32
|
|
|
{ |
33
|
|
|
//============================================================================================================== |
34
|
|
|
//================================================= TESTS ==================================================== |
35
|
|
|
//============================================================================================================== |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @covers ::export |
39
|
|
|
* @dataProvider provideValidTaxRates |
40
|
|
|
* |
41
|
|
|
* @param TaxRateModel[] $rates |
42
|
|
|
* @param array $expectedResult |
43
|
|
|
*/ |
44
|
|
|
public function testSuccessfulExport(array $rates, array $expectedResult = []) |
45
|
|
|
{ |
46
|
|
|
$schematicTaxRatesService = new TaxRates(); |
47
|
|
|
|
48
|
|
|
$actualResult = $schematicTaxRatesService->export($rates); |
49
|
|
|
|
50
|
|
|
$this->assertSame($expectedResult, $actualResult); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @covers ::import |
55
|
|
|
* @dataProvider provideValidTaxRateDefinitions |
56
|
|
|
* |
57
|
|
|
* @param array $rateDefinitions |
58
|
|
|
*/ |
59
|
|
|
public function testSuccessfulImport(array $rateDefinitions) |
60
|
|
|
{ |
61
|
|
|
$this->setMockTaxRatesService(); |
62
|
|
|
$this->setMockTaxCategoriesService(); |
63
|
|
|
$this->setMockTaxZonesService(); |
64
|
|
|
$this->setMockDbConnection(); |
65
|
|
|
|
66
|
|
|
$schematicTaxRatesService = new TaxRates(); |
67
|
|
|
|
68
|
|
|
$import = $schematicTaxRatesService->import($rateDefinitions); |
69
|
|
|
|
70
|
|
|
$this->assertInstanceOf(Result::class, $import); |
71
|
|
|
$this->assertFalse($import->hasErrors()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers ::import |
76
|
|
|
* @dataProvider provideValidTaxRateDefinitions |
77
|
|
|
* |
78
|
|
|
* @param array $rateDefinitions |
79
|
|
|
*/ |
80
|
|
|
public function testImportWithForceOption(array $rateDefinitions) |
81
|
|
|
{ |
82
|
|
|
$this->setMockTaxRatesService(); |
83
|
|
|
$this->setMockTaxCategoriesService(); |
84
|
|
|
$this->setMockTaxZonesService(); |
85
|
|
|
$this->setMockDbConnection(); |
86
|
|
|
|
87
|
|
|
$schematicTaxRatesService = new TaxRates(); |
88
|
|
|
|
89
|
|
|
$import = $schematicTaxRatesService->import($rateDefinitions, true); |
90
|
|
|
|
91
|
|
|
$this->assertInstanceOf(Result::class, $import); |
92
|
|
|
$this->assertFalse($import->hasErrors()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//============================================================================================================== |
96
|
|
|
//============================================== PROVIDERS =================================================== |
97
|
|
|
//============================================================================================================== |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function provideValidTaxRates() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'single rate' => [ |
106
|
|
|
'TaxRates' => [ |
107
|
|
|
'rate1' => $this->getMockTaxRate(1), |
108
|
|
|
], |
109
|
|
|
'expectedResult' => [ |
110
|
|
|
'rateName1' => [ |
111
|
|
|
'name' => 'rateName1', |
112
|
|
|
'rate' => null, |
113
|
|
|
'include' => null, |
114
|
|
|
'isVat' => null, |
115
|
|
|
'taxable' => null, |
116
|
|
|
'taxCategory' => 'categoryHandle1', |
117
|
|
|
'taxZone' => 'zoneName1', |
118
|
|
|
], |
119
|
|
|
], |
120
|
|
|
], |
121
|
|
|
'multiple rates' => [ |
122
|
|
|
'TaxRates' => [ |
123
|
|
|
'rate1' => $this->getMockTaxRate(1), |
124
|
|
|
'rate2' => $this->getMockTaxRate(2), |
125
|
|
|
], |
126
|
|
|
'expectedResult' => [ |
127
|
|
|
'rateName1' => [ |
128
|
|
|
'name' => 'rateName1', |
129
|
|
|
'rate' => null, |
130
|
|
|
'include' => null, |
131
|
|
|
'isVat' => null, |
132
|
|
|
'taxable' => null, |
133
|
|
|
'taxCategory' => 'categoryHandle1', |
134
|
|
|
'taxZone' => 'zoneName1', |
135
|
|
|
], |
136
|
|
|
'rateName2' => [ |
137
|
|
|
'name' => 'rateName2', |
138
|
|
|
'rate' => null, |
139
|
|
|
'include' => null, |
140
|
|
|
'isVat' => null, |
141
|
|
|
'taxable' => null, |
142
|
|
|
'taxCategory' => 'categoryHandle2', |
143
|
|
|
'taxZone' => 'zoneName2', |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function provideValidTaxRateDefinitions() |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
'emptyArray' => [ |
157
|
|
|
'rateDefinitions' => [], |
158
|
|
|
], |
159
|
|
|
'single rate' => [ |
160
|
|
|
'rateDefinitions' => [ |
161
|
|
|
'rateName1' => [ |
162
|
|
|
'name' => 'rateName1', |
163
|
|
|
'rate' => null, |
164
|
|
|
'include' => null, |
165
|
|
|
'isVat' => null, |
166
|
|
|
'taxable' => null, |
167
|
|
|
'taxCategory' => 'categoryHandle1', |
168
|
|
|
'taxZone' => 'zoneName1', |
169
|
|
|
], |
170
|
|
|
], |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
//============================================================================================================== |
176
|
|
|
//================================================= MOCKS ==================================================== |
177
|
|
|
//============================================================================================================== |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $rateId |
181
|
|
|
* |
182
|
|
|
* @return Mock|Commce_TaxRateModel |
183
|
|
|
*/ |
184
|
|
|
private function getMockTaxRate($rateId) |
185
|
|
|
{ |
186
|
|
|
$mockTaxRate = $this->getMockBuilder(Commerce_TaxRateModel::class) |
187
|
|
|
->disableOriginalConstructor() |
188
|
|
|
->getMock(); |
189
|
|
|
|
190
|
|
|
$mockTaxRate->expects($this->any()) |
191
|
|
|
->method('__get') |
192
|
|
|
->willReturnMap([ |
193
|
|
|
['id', $rateId], |
194
|
|
|
['name', 'rateName'.$rateId], |
195
|
|
|
]); |
196
|
|
|
|
197
|
|
|
$mockTaxRate->expects($this->any()) |
198
|
|
|
->method('getTaxCategory') |
199
|
|
|
->willReturn($this->getMockTaxCategory($rateId)); |
200
|
|
|
|
201
|
|
|
$mockTaxRate->expects($this->any()) |
202
|
|
|
->method('getTaxZone') |
203
|
|
|
->willReturn($this->getMockTaxZone($rateId)); |
204
|
|
|
|
205
|
|
|
$mockTaxRate->expects($this->any()) |
206
|
|
|
->method('getAllErrors') |
207
|
|
|
->willReturn([ |
208
|
|
|
'ohnoes' => 'horrible error', |
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
return $mockTaxRate; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $categoryId |
216
|
|
|
* |
217
|
|
|
* @return Mock|Commce_TaxCategoryModel |
218
|
|
|
*/ |
219
|
|
|
private function getMockTaxCategory($categoryId) |
220
|
|
|
{ |
221
|
|
|
$mockTaxCategory = $this->getMockBuilder(Commerce_TaxCategoryModel::class) |
222
|
|
|
->disableOriginalConstructor() |
223
|
|
|
->getMock(); |
224
|
|
|
|
225
|
|
|
$mockTaxCategory->expects($this->any()) |
226
|
|
|
->method('__get') |
227
|
|
|
->willReturnMap([ |
228
|
|
|
['id', $categoryId], |
229
|
|
|
['handle', 'categoryHandle'.$categoryId], |
230
|
|
|
]); |
231
|
|
|
|
232
|
|
|
return $mockTaxCategory; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $zoneId |
237
|
|
|
* |
238
|
|
|
* @return Mock|Commce_TaxZoneModel |
239
|
|
|
*/ |
240
|
|
|
private function getMockTaxZone($zoneId) |
241
|
|
|
{ |
242
|
|
|
$mockTaxZone = $this->getMockBuilder(Commerce_TaxZoneModel::class) |
243
|
|
|
->disableOriginalConstructor() |
244
|
|
|
->getMock(); |
245
|
|
|
|
246
|
|
|
$mockTaxZone->expects($this->any()) |
247
|
|
|
->method('__get') |
248
|
|
|
->willReturnMap([ |
249
|
|
|
['id', $zoneId], |
250
|
|
|
['name', 'zoneName'.$zoneId], |
251
|
|
|
]); |
252
|
|
|
|
253
|
|
|
return $mockTaxZone; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return Mock|Commerce_TaxRatesService |
258
|
|
|
*/ |
259
|
|
|
private function setMockTaxRatesService() |
260
|
|
|
{ |
261
|
|
|
$mockTaxRatesService = $this->getMockBuilder(Commerce_TaxRatesService::class) |
262
|
|
|
->disableOriginalConstructor() |
263
|
|
|
->setMethods(['getAllTaxRates', 'saveTaxRate', 'deleteTaxRateById']) |
264
|
|
|
->getMock(); |
265
|
|
|
|
266
|
|
|
$mockTaxRatesService->expects($this->any()) |
267
|
|
|
->method('getAllTaxRates') |
268
|
|
|
->willReturn([]); |
269
|
|
|
|
270
|
|
|
$this->setComponent(Craft::app(), 'commerce_taxRates', $mockTaxRatesService); |
271
|
|
|
|
272
|
|
|
return $mockTaxRatesService; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return Mock|Commerce_TaxCategoriesService |
277
|
|
|
*/ |
278
|
|
|
private function setMockTaxCategoriesService() |
279
|
|
|
{ |
280
|
|
|
$mockTaxCategoriesService = $this->getMockBuilder(Commerce_TaxCategoriesService::class) |
281
|
|
|
->disableOriginalConstructor() |
282
|
|
|
->setMethods(['getTaxCategoryByHandle']) |
283
|
|
|
->getMock(); |
284
|
|
|
|
285
|
|
|
$mockTaxCategoriesService->expects($this->any()) |
286
|
|
|
->method('getTaxCategoryByHandle') |
287
|
|
|
->willReturn($this->getMockTaxCategory(1)); |
288
|
|
|
|
289
|
|
|
$this->setComponent(Craft::app(), 'commerce_taxCategories', $mockTaxCategoriesService); |
290
|
|
|
|
291
|
|
|
return $mockTaxCategoriesService; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return Mock|Commerce_TaxZonesService |
296
|
|
|
*/ |
297
|
|
|
private function setMockTaxZonesService() |
298
|
|
|
{ |
299
|
|
|
$mockTaxZonesService = $this->getMockBuilder(Commerce_TaxZonesService::class) |
300
|
|
|
->disableOriginalConstructor() |
301
|
|
|
->setMethods(['getAllTaxZones']) |
302
|
|
|
->getMock(); |
303
|
|
|
|
304
|
|
|
$mockTaxZonesService->expects($this->any()) |
305
|
|
|
->method('getAllTaxZones') |
306
|
|
|
->willReturn([]); |
307
|
|
|
|
308
|
|
|
$this->setComponent(Craft::app(), 'commerce_taxZones', $mockTaxZonesService); |
309
|
|
|
|
310
|
|
|
return $mockTaxZonesService; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return Mock|DbConnection |
315
|
|
|
*/ |
316
|
|
|
private function setMockDbConnection() |
317
|
|
|
{ |
318
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
319
|
|
|
->disableOriginalConstructor() |
320
|
|
|
->setMethods(['createCommand']) |
321
|
|
|
->getMock(); |
322
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
323
|
|
|
|
324
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
325
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
326
|
|
|
|
327
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
328
|
|
|
|
329
|
|
|
return $mockDbConnection; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return Mock|DbCommand |
334
|
|
|
*/ |
335
|
|
|
private function getMockDbCommand() |
336
|
|
|
{ |
337
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
338
|
|
|
->disableOriginalConstructor() |
339
|
|
|
->setMethods(['insertOrUpdate']) |
340
|
|
|
->getMock(); |
341
|
|
|
|
342
|
|
|
return $mockDbCommand; |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|