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