1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\Commerce_TaxZoneModel; |
7
|
|
|
use Craft\Commerce_TaxZonesService; |
8
|
|
|
use Craft\Craft; |
9
|
|
|
use Craft\DbCommand; |
10
|
|
|
use Craft\DbConnection; |
11
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
12
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class TaxZonesTest. |
16
|
|
|
* |
17
|
|
|
* @author Nerds & Company |
18
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
19
|
|
|
* @license MIT |
20
|
|
|
* |
21
|
|
|
* @see http://www.nerds.company |
22
|
|
|
* |
23
|
|
|
* @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\TaxZones |
24
|
|
|
* @covers ::__construct |
25
|
|
|
* @covers ::<!public> |
26
|
|
|
*/ |
27
|
|
|
class TaxZonesTest extends BaseTest |
28
|
|
|
{ |
29
|
|
|
//============================================================================================================== |
30
|
|
|
//================================================= TESTS ==================================================== |
31
|
|
|
//============================================================================================================== |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers ::export |
35
|
|
|
* @dataProvider provideValidTaxZones |
36
|
|
|
* |
37
|
|
|
* @param TaxZoneModel[] $zones |
38
|
|
|
* @param array $expectedResult |
39
|
|
|
*/ |
40
|
|
|
public function testSuccessfulExport(array $zones, array $expectedResult = []) |
41
|
|
|
{ |
42
|
|
|
$schematicTaxZonesService = new TaxZones(); |
43
|
|
|
|
44
|
|
|
$actualResult = $schematicTaxZonesService->export($zones); |
45
|
|
|
|
46
|
|
|
$this->assertSame($expectedResult, $actualResult); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers ::import |
51
|
|
|
* @dataProvider provideValidTaxZoneDefinitions |
52
|
|
|
* |
53
|
|
|
* @param array $zoneDefinitions |
54
|
|
|
*/ |
55
|
|
|
public function testSuccessfulImport(array $zoneDefinitions) |
56
|
|
|
{ |
57
|
|
|
$this->setMockTaxZonesService(); |
58
|
|
|
$this->setMockDbConnection(); |
59
|
|
|
|
60
|
|
|
$schematicTaxZonesService = new TaxZones(); |
61
|
|
|
|
62
|
|
|
$import = $schematicTaxZonesService->import($zoneDefinitions); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(Result::class, $import); |
65
|
|
|
$this->assertFalse($import->hasErrors()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @covers ::import |
70
|
|
|
* @dataProvider provideValidTaxZoneDefinitions |
71
|
|
|
* |
72
|
|
|
* @param array $zoneDefinitions |
73
|
|
|
*/ |
74
|
|
|
public function testImportWithForceOption(array $zoneDefinitions) |
75
|
|
|
{ |
76
|
|
|
$this->setMockTaxZonesService(); |
77
|
|
|
$this->setMockDbConnection(); |
78
|
|
|
|
79
|
|
|
$schematicTaxZonesService = new TaxZones(); |
80
|
|
|
|
81
|
|
|
$import = $schematicTaxZonesService->import($zoneDefinitions, true); |
82
|
|
|
|
83
|
|
|
$this->assertInstanceOf(Result::class, $import); |
84
|
|
|
$this->assertFalse($import->hasErrors()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//============================================================================================================== |
88
|
|
|
//============================================== PROVIDERS =================================================== |
89
|
|
|
//============================================================================================================== |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function provideValidTaxZones() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
'single zone' => [ |
98
|
|
|
'TaxZones' => [ |
99
|
|
|
'zone1' => $this->getMockTaxZone(1), |
100
|
|
|
], |
101
|
|
|
'expectedResult' => [ |
102
|
|
|
'zoneName1' => [ |
103
|
|
|
'name' => 'zoneName1', |
104
|
|
|
'description' => null, |
105
|
|
|
'countryBased' => null, |
106
|
|
|
'default' => null, |
107
|
|
|
'countries' => array(), |
108
|
|
|
'states' => array(), |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'multiple zones' => [ |
113
|
|
|
'TaxZones' => [ |
114
|
|
|
'zone1' => $this->getMockTaxZone(1), |
115
|
|
|
'zone2' => $this->getMockTaxZone(2), |
116
|
|
|
], |
117
|
|
|
'expectedResult' => [ |
118
|
|
|
'zoneName1' => [ |
119
|
|
|
'name' => 'zoneName1', |
120
|
|
|
'description' => null, |
121
|
|
|
'countryBased' => null, |
122
|
|
|
'default' => null, |
123
|
|
|
'countries' => array(), |
124
|
|
|
'states' => array(), |
125
|
|
|
], |
126
|
|
|
'zoneName2' => [ |
127
|
|
|
'name' => 'zoneName2', |
128
|
|
|
'description' => null, |
129
|
|
|
'countryBased' => null, |
130
|
|
|
'default' => null, |
131
|
|
|
'countries' => array(), |
132
|
|
|
'states' => array(), |
133
|
|
|
], |
134
|
|
|
], |
135
|
|
|
], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function provideValidTaxZoneDefinitions() |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
'emptyArray' => [ |
146
|
|
|
'zoneDefinitions' => [], |
147
|
|
|
], |
148
|
|
|
'single zone' => [ |
149
|
|
|
'zoneDefinitions' => [ |
150
|
|
|
'zoneName1' => [ |
151
|
|
|
'name' => 'zoneName1', |
152
|
|
|
'description' => null, |
153
|
|
|
'countryBased' => null, |
154
|
|
|
'default' => null, |
155
|
|
|
'countries' => array(), |
156
|
|
|
'states' => array(), |
157
|
|
|
], |
158
|
|
|
], |
159
|
|
|
], |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
//============================================================================================================== |
164
|
|
|
//================================================= MOCKS ==================================================== |
165
|
|
|
//============================================================================================================== |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $zoneId |
169
|
|
|
* |
170
|
|
|
* @return Mock|TaxZoneModel |
171
|
|
|
*/ |
172
|
|
|
private function getMockTaxZone($zoneId) |
173
|
|
|
{ |
174
|
|
|
$mockTaxZone = $this->getMockBuilder(Commerce_TaxZoneModel::class) |
175
|
|
|
->disableOriginalConstructor() |
176
|
|
|
->getMock(); |
177
|
|
|
|
178
|
|
|
$mockTaxZone->expects($this->any()) |
179
|
|
|
->method('__get') |
180
|
|
|
->willReturnMap([ |
181
|
|
|
['id', $zoneId], |
182
|
|
|
['name', 'zoneName'.$zoneId], |
183
|
|
|
]); |
184
|
|
|
|
185
|
|
|
$mockTaxZone->expects($this->any()) |
186
|
|
|
->method('getCountries') |
187
|
|
|
->willReturn([]); |
188
|
|
|
|
189
|
|
|
$mockTaxZone->expects($this->any()) |
190
|
|
|
->method('getStates') |
191
|
|
|
->willReturn([]); |
192
|
|
|
|
193
|
|
|
$mockTaxZone->expects($this->any()) |
194
|
|
|
->method('getAllErrors') |
195
|
|
|
->willReturn([ |
196
|
|
|
'ohnoes' => 'horrible error', |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
return $mockTaxZone; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Mock|CategoriesService |
204
|
|
|
*/ |
205
|
|
|
private function setMockTaxZonesService() |
206
|
|
|
{ |
207
|
|
|
$mockTaxZonesService = $this->getMockBuilder(Commerce_TaxZonesService::class) |
208
|
|
|
->disableOriginalConstructor() |
209
|
|
|
->setMethods(['getAllTaxZones', 'saveTaxZone', 'deleteTaxZoneById']) |
210
|
|
|
->getMock(); |
211
|
|
|
|
212
|
|
|
$mockTaxZonesService->expects($this->any()) |
213
|
|
|
->method('getAllTaxZones') |
214
|
|
|
->willReturn([]); |
215
|
|
|
|
216
|
|
|
$this->setComponent(Craft::app(), 'commerce_taxZones', $mockTaxZonesService); |
217
|
|
|
|
218
|
|
|
return $mockTaxZonesService; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return Mock|DbConnection |
223
|
|
|
*/ |
224
|
|
|
private function setMockDbConnection() |
225
|
|
|
{ |
226
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
227
|
|
|
->disableOriginalConstructor() |
228
|
|
|
->setMethods(['createCommand']) |
229
|
|
|
->getMock(); |
230
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
231
|
|
|
|
232
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
233
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
234
|
|
|
|
235
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
236
|
|
|
|
237
|
|
|
return $mockDbConnection; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return Mock|DbCommand |
242
|
|
|
*/ |
243
|
|
|
private function getMockDbCommand() |
244
|
|
|
{ |
245
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
246
|
|
|
->disableOriginalConstructor() |
247
|
|
|
->setMethods(['insertOrUpdate']) |
248
|
|
|
->getMock(); |
249
|
|
|
|
250
|
|
|
return $mockDbCommand; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|