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