1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\Commerce_CountryModel; |
7
|
|
|
use Craft\Commerce_CountriesService; |
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 CountriesTest. |
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\Countries |
24
|
|
|
* @covers ::__construct |
25
|
|
|
* @covers ::<!public> |
26
|
|
|
*/ |
27
|
|
|
class CountriesTest extends BaseTest |
28
|
|
|
{ |
29
|
|
|
//============================================================================================================== |
30
|
|
|
//================================================= TESTS ==================================================== |
31
|
|
|
//============================================================================================================== |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers ::export |
35
|
|
|
* @dataProvider provideValidCountries |
36
|
|
|
* |
37
|
|
|
* @param CountryModel[] $countries |
38
|
|
|
* @param array $expectedResult |
39
|
|
|
*/ |
40
|
|
|
public function testSuccessfulExport(array $countries, array $expectedResult = []) |
41
|
|
|
{ |
42
|
|
|
$schematicCountriesService = new Countries(); |
43
|
|
|
|
44
|
|
|
$actualResult = $schematicCountriesService->export($countries); |
45
|
|
|
|
46
|
|
|
$this->assertSame($expectedResult, $actualResult); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers ::import |
51
|
|
|
* @dataProvider provideValidCountryDefinitions |
52
|
|
|
* |
53
|
|
|
* @param array $countryDefinitions |
54
|
|
|
*/ |
55
|
|
|
public function testSuccessfulImport(array $countryDefinitions) |
56
|
|
|
{ |
57
|
|
|
$this->setMockCountriesService(); |
58
|
|
|
$this->setMockDbConnection(); |
59
|
|
|
|
60
|
|
|
$schematicCountriesService = new Countries(); |
61
|
|
|
|
62
|
|
|
$import = $schematicCountriesService->import($countryDefinitions); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(Result::class, $import); |
65
|
|
|
$this->assertFalse($import->hasErrors()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @covers ::import |
70
|
|
|
* @dataProvider provideValidCountryDefinitions |
71
|
|
|
* |
72
|
|
|
* @param array $countryDefinitions |
73
|
|
|
*/ |
74
|
|
|
public function testImportWithForceOption(array $countryDefinitions) |
75
|
|
|
{ |
76
|
|
|
$this->setMockCountriesService(); |
77
|
|
|
$this->setMockDbConnection(); |
78
|
|
|
|
79
|
|
|
$schematicCountriesService = new Countries(); |
80
|
|
|
|
81
|
|
|
$import = $schematicCountriesService->import($countryDefinitions, 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 provideValidCountries() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
'single country' => [ |
98
|
|
|
'Countries' => [ |
99
|
|
|
'country1' => $this->getMockCountry(1), |
100
|
|
|
], |
101
|
|
|
'expectedResult' => [ |
102
|
|
|
'countryIso1' => [ |
103
|
|
|
'name' => 'countryName1', |
104
|
|
|
'stateRequired' => null, |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
], |
108
|
|
|
'multiple countries' => [ |
109
|
|
|
'Countries' => [ |
110
|
|
|
'country1' => $this->getMockCountry(1), |
111
|
|
|
'country2' => $this->getMockCountry(2), |
112
|
|
|
], |
113
|
|
|
'expectedResult' => [ |
114
|
|
|
'countryIso1' => [ |
115
|
|
|
'name' => 'countryName1', |
116
|
|
|
'stateRequired' => null, |
117
|
|
|
], |
118
|
|
|
'countryIso2' => [ |
119
|
|
|
'name' => 'countryName2', |
120
|
|
|
'stateRequired' => null, |
121
|
|
|
], |
122
|
|
|
], |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function provideValidCountryDefinitions() |
131
|
|
|
{ |
132
|
|
|
return [ |
133
|
|
|
'emptyArray' => [ |
134
|
|
|
'countryDefinitions' => [], |
135
|
|
|
], |
136
|
|
|
'single country' => [ |
137
|
|
|
'countryDefinitions' => [ |
138
|
|
|
'countryIso1' => [ |
139
|
|
|
'name' => 'countryName1', |
140
|
|
|
'stateRequired' => null, |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//============================================================================================================== |
148
|
|
|
//================================================= MOCKS ==================================================== |
149
|
|
|
//============================================================================================================== |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $countryId |
153
|
|
|
* |
154
|
|
|
* @return Mock|CountryModel |
155
|
|
|
*/ |
156
|
|
|
private function getMockCountry($countryId) |
157
|
|
|
{ |
158
|
|
|
$mockCountry = $this->getMockBuilder(Commerce_CountryModel::class) |
159
|
|
|
->disableOriginalConstructor() |
160
|
|
|
->getMock(); |
161
|
|
|
|
162
|
|
|
$mockCountry->expects($this->any()) |
163
|
|
|
->method('__get') |
164
|
|
|
->willReturnMap([ |
165
|
|
|
['id', $countryId], |
166
|
|
|
['iso', 'countryIso'.$countryId], |
167
|
|
|
['name', 'countryName'.$countryId], |
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
$mockCountry->expects($this->any()) |
171
|
|
|
->method('getAllErrors') |
172
|
|
|
->willReturn([ |
173
|
|
|
'ohnoes' => 'horrible error', |
174
|
|
|
]); |
175
|
|
|
|
176
|
|
|
return $mockCountry; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return Mock|CategoriesService |
181
|
|
|
*/ |
182
|
|
|
private function setMockCountriesService() |
183
|
|
|
{ |
184
|
|
|
$mockCountriesService = $this->getMockBuilder(Commerce_CountriesService::class) |
185
|
|
|
->disableOriginalConstructor() |
186
|
|
|
->setMethods(['getAllCountries', 'saveCountry', 'deleteCountryById']) |
187
|
|
|
->getMock(); |
188
|
|
|
|
189
|
|
|
$mockCountriesService->expects($this->any()) |
190
|
|
|
->method('getAllCountries') |
191
|
|
|
->willReturn([]); |
192
|
|
|
|
193
|
|
|
$this->setComponent(Craft::app(), 'commerce_countries', $mockCountriesService); |
194
|
|
|
|
195
|
|
|
return $mockCountriesService; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Mock|DbConnection |
200
|
|
|
*/ |
201
|
|
|
private function setMockDbConnection() |
202
|
|
|
{ |
203
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
204
|
|
|
->disableOriginalConstructor() |
205
|
|
|
->setMethods(['createCommand']) |
206
|
|
|
->getMock(); |
207
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
208
|
|
|
|
209
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
210
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
211
|
|
|
|
212
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
213
|
|
|
|
214
|
|
|
return $mockDbConnection; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Mock|DbCommand |
219
|
|
|
*/ |
220
|
|
|
private function getMockDbCommand() |
221
|
|
|
{ |
222
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
223
|
|
|
->disableOriginalConstructor() |
224
|
|
|
->setMethods(['insertOrUpdate']) |
225
|
|
|
->getMock(); |
226
|
|
|
|
227
|
|
|
return $mockDbCommand; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|