1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\Commerce_CountryModel; |
7
|
|
|
use Craft\Commerce_StateModel; |
8
|
|
|
use Craft\Commerce_StatesService; |
9
|
|
|
use Craft\Craft; |
10
|
|
|
use Craft\DbCommand; |
11
|
|
|
use Craft\DbConnection; |
12
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
13
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class StatesTest. |
17
|
|
|
* |
18
|
|
|
* @author Nerds & Company |
19
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
20
|
|
|
* @license MIT |
21
|
|
|
* |
22
|
|
|
* @see http://www.nerds.company |
23
|
|
|
* |
24
|
|
|
* @coversDefaultClass \NerdsAndCompany\Schematic\Commerce\Services\States |
25
|
|
|
* @covers ::__construct |
26
|
|
|
* @covers ::<!public> |
27
|
|
|
*/ |
28
|
|
|
class StatesTest extends BaseTest |
29
|
|
|
{ |
30
|
|
|
//============================================================================================================== |
31
|
|
|
//================================================= TESTS ==================================================== |
32
|
|
|
//============================================================================================================== |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers ::export |
36
|
|
|
* @dataProvider provideValidStates |
37
|
|
|
* |
38
|
|
|
* @param StateModel[] $states |
39
|
|
|
* @param array $expectedResult |
40
|
|
|
*/ |
41
|
|
|
public function testSuccessfulExport(array $states, array $expectedResult = []) |
42
|
|
|
{ |
43
|
|
|
$schematicStatesService = new States(); |
44
|
|
|
|
45
|
|
|
$actualResult = $schematicStatesService->export($states); |
46
|
|
|
|
47
|
|
|
$this->assertSame($expectedResult, $actualResult); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @covers ::import |
52
|
|
|
* @dataProvider provideValidStateDefinitions |
53
|
|
|
* |
54
|
|
|
* @param array $stateDefinitions |
55
|
|
|
*/ |
56
|
|
|
public function testSuccessfulImport(array $stateDefinitions) |
57
|
|
|
{ |
58
|
|
|
$this->setMockStatesService(); |
59
|
|
|
$this->setMockCountriesService(); |
60
|
|
|
$this->setMockDbConnection(); |
61
|
|
|
|
62
|
|
|
$schematicStatesService = new States(); |
63
|
|
|
|
64
|
|
|
$import = $schematicStatesService->import($stateDefinitions); |
65
|
|
|
|
66
|
|
|
$this->assertInstanceOf(Result::class, $import); |
67
|
|
|
$this->assertFalse($import->hasErrors()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @covers ::import |
72
|
|
|
* @dataProvider provideValidStateDefinitions |
73
|
|
|
* |
74
|
|
|
* @param array $stateDefinitions |
75
|
|
|
*/ |
76
|
|
|
public function testImportWithForceOption(array $stateDefinitions) |
77
|
|
|
{ |
78
|
|
|
$this->setMockStatesService(); |
79
|
|
|
$this->setMockCountriesService(); |
80
|
|
|
$this->setMockDbConnection(); |
81
|
|
|
|
82
|
|
|
$schematicStatesService = new States(); |
83
|
|
|
|
84
|
|
|
$import = $schematicStatesService->import($stateDefinitions, 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 provideValidStates() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
'single state' => [ |
101
|
|
|
'States' => [ |
102
|
|
|
'state1' => $this->getMockState(1), |
103
|
|
|
], |
104
|
|
|
'expectedResult' => [ |
105
|
|
|
'stateAbbr1' => [ |
106
|
|
|
'name' => 'stateName1', |
107
|
|
|
'country' => 'countryIso1', |
108
|
|
|
], |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
'multiple states' => [ |
112
|
|
|
'States' => [ |
113
|
|
|
'state1' => $this->getMockState(1), |
114
|
|
|
'state2' => $this->getMockState(2), |
115
|
|
|
], |
116
|
|
|
'expectedResult' => [ |
117
|
|
|
'stateAbbr1' => [ |
118
|
|
|
'name' => 'stateName1', |
119
|
|
|
'country' => 'countryIso1', |
120
|
|
|
], |
121
|
|
|
'stateAbbr2' => [ |
122
|
|
|
'name' => 'stateName2', |
123
|
|
|
'country' => 'countryIso2', |
124
|
|
|
], |
125
|
|
|
], |
126
|
|
|
], |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function provideValidStateDefinitions() |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
'emptyArray' => [ |
137
|
|
|
'stateDefinitions' => [], |
138
|
|
|
], |
139
|
|
|
'single state' => [ |
140
|
|
|
'stateDefinitions' => [ |
141
|
|
|
'stateAbbr1' => [ |
142
|
|
|
'name' => 'stateName1', |
143
|
|
|
'country' => 'countryIso1', |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
//============================================================================================================== |
151
|
|
|
//================================================= MOCKS ==================================================== |
152
|
|
|
//============================================================================================================== |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $stateId |
156
|
|
|
* |
157
|
|
|
* @return Mock|StateModel |
158
|
|
|
*/ |
159
|
|
|
private function getMockState($stateId) |
160
|
|
|
{ |
161
|
|
|
$mockState = $this->getMockBuilder(Commerce_StateModel::class) |
162
|
|
|
->disableOriginalConstructor() |
163
|
|
|
->getMock(); |
164
|
|
|
|
165
|
|
|
$mockState->expects($this->any()) |
166
|
|
|
->method('__get') |
167
|
|
|
->willReturnMap([ |
168
|
|
|
['id', $stateId], |
169
|
|
|
['abbreviation', 'stateAbbr'.$stateId], |
170
|
|
|
['name', 'stateName'.$stateId], |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
$mockState->expects($this->any()) |
174
|
|
|
->method('getCountry') |
175
|
|
|
->willReturn($this->getMockCountry($stateId)); |
176
|
|
|
|
177
|
|
|
$mockState->expects($this->any()) |
178
|
|
|
->method('getAllErrors') |
179
|
|
|
->willReturn([ |
180
|
|
|
'ohnoes' => 'horrible error', |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
return $mockState; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $countryId |
188
|
|
|
* |
189
|
|
|
* @return Mock|Commerce_CountryModel |
190
|
|
|
*/ |
191
|
|
|
private function getMockCountry($countryId) |
192
|
|
|
{ |
193
|
|
|
$mockCountry = $this->getMockBuilder(Commerce_CountryModel::class) |
194
|
|
|
->disableOriginalConstructor() |
195
|
|
|
->getMock(); |
196
|
|
|
|
197
|
|
|
$mockCountry->expects($this->any()) |
198
|
|
|
->method('__get') |
199
|
|
|
->willReturnMap([ |
200
|
|
|
['id', $countryId], |
201
|
|
|
['iso', 'countryIso'.$countryId], |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
return $mockCountry; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return Mock|Commerce_StatesService |
209
|
|
|
*/ |
210
|
|
|
private function setMockStatesService() |
211
|
|
|
{ |
212
|
|
|
$mockStatesService = $this->getMockBuilder(Commerce_StatesService::class) |
213
|
|
|
->disableOriginalConstructor() |
214
|
|
|
->setMethods(['getAllStates', 'saveState', 'deleteStateById']) |
215
|
|
|
->getMock(); |
216
|
|
|
|
217
|
|
|
$mockStatesService->expects($this->any()) |
218
|
|
|
->method('getAllStates') |
219
|
|
|
->willReturn([]); |
220
|
|
|
|
221
|
|
|
$this->setComponent(Craft::app(), 'commerce_states', $mockStatesService); |
222
|
|
|
|
223
|
|
|
return $mockStatesService; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return Mock|Commerce_CountriesService |
228
|
|
|
*/ |
229
|
|
|
private function setMockCountriesService() |
230
|
|
|
{ |
231
|
|
|
$mockCountriesService = $this->getMockBuilder(Commerce_CountriesService::class) |
232
|
|
|
->disableOriginalConstructor() |
233
|
|
|
->setMethods(['getCountryByAttributes']) |
234
|
|
|
->getMock(); |
235
|
|
|
|
236
|
|
|
$mockCountriesService->expects($this->any()) |
237
|
|
|
->method('getCountryByAttributes') |
238
|
|
|
->willReturn($this->getMockCountry(1)); |
239
|
|
|
|
240
|
|
|
$this->setComponent(Craft::app(), 'commerce_countries', $mockCountriesService); |
241
|
|
|
|
242
|
|
|
return $mockCountriesService; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return Mock|DbConnection |
247
|
|
|
*/ |
248
|
|
|
private function setMockDbConnection() |
249
|
|
|
{ |
250
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
251
|
|
|
->disableOriginalConstructor() |
252
|
|
|
->setMethods(['createCommand']) |
253
|
|
|
->getMock(); |
254
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
255
|
|
|
|
256
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
257
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
258
|
|
|
|
259
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
260
|
|
|
|
261
|
|
|
return $mockDbConnection; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return Mock|DbCommand |
266
|
|
|
*/ |
267
|
|
|
private function getMockDbCommand() |
268
|
|
|
{ |
269
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
270
|
|
|
->disableOriginalConstructor() |
271
|
|
|
->setMethods(['insertOrUpdate']) |
272
|
|
|
->getMock(); |
273
|
|
|
|
274
|
|
|
return $mockDbCommand; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|