1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\CategoriesService; |
7
|
|
|
use Craft\CategoryGroupLocaleModel; |
8
|
|
|
use Craft\CategoryGroupModel; |
9
|
|
|
use Craft\Craft; |
10
|
|
|
use Craft\DbCommand; |
11
|
|
|
use Craft\DbConnection; |
12
|
|
|
use Craft\FieldLayoutModel; |
13
|
|
|
use Craft\FieldsService; |
14
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
15
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CategoryGroupsTest. |
19
|
|
|
* |
20
|
|
|
* @author Nerds & Company |
21
|
|
|
* @copyright Copyright (c) 2015-2016, Nerds & Company |
22
|
|
|
* @license MIT |
23
|
|
|
* |
24
|
|
|
* @link http://www.nerds.company |
25
|
|
|
* |
26
|
|
|
* @coversDefaultClass NerdsAndCompany\Schematic\Services\CategoryGroups |
27
|
|
|
* @covers ::__construct |
28
|
|
|
* @covers ::<!public> |
29
|
|
|
*/ |
30
|
|
|
class CategoryGroupsTest extends BaseTest |
31
|
|
|
{ |
32
|
|
|
//============================================================================================================== |
33
|
|
|
//================================================= TESTS ==================================================== |
34
|
|
|
//============================================================================================================== |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers ::export |
38
|
|
|
* @dataProvider provideValidCategoryGroups |
39
|
|
|
* |
40
|
|
|
* @param CategoryGroupModel[] $groups |
41
|
|
|
* @param array $expectedResult |
42
|
|
|
*/ |
43
|
|
|
public function testSuccessfulExport(array $groups, array $expectedResult = []) |
44
|
|
|
{ |
45
|
|
|
$this->setMockFieldsService(); |
46
|
|
|
$this->setMockSchematicFields(); |
47
|
|
|
|
48
|
|
|
$schematicCategoryGroupsService = new CategoryGroups(); |
49
|
|
|
|
50
|
|
|
$actualResult = $schematicCategoryGroupsService->export($groups); |
51
|
|
|
|
52
|
|
|
$this->assertSame($expectedResult, $actualResult); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @covers ::import |
57
|
|
|
* @dataProvider provideValidCategoryGroupDefinitions |
58
|
|
|
* |
59
|
|
|
* @param array $groupDefinitions |
60
|
|
|
*/ |
61
|
|
|
public function testSuccessfulImport(array $groupDefinitions) |
62
|
|
|
{ |
63
|
|
|
$this->setMockCategoriesService(); |
64
|
|
|
$this->setMockDbConnection(); |
65
|
|
|
$this->setMockSchematicFields(); |
66
|
|
|
|
67
|
|
|
$schematicUserGroupsService = new CategoryGroups(); |
68
|
|
|
|
69
|
|
|
$import = $schematicUserGroupsService->import($groupDefinitions); |
70
|
|
|
|
71
|
|
|
$this->assertInstanceOf(Result::class, $import); |
72
|
|
|
$this->assertFalse($import->hasErrors()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @covers ::import |
77
|
|
|
* @dataProvider provideValidCategoryGroupDefinitions |
78
|
|
|
* |
79
|
|
|
* @param array $groupDefinitions |
80
|
|
|
*/ |
81
|
|
|
public function testImportWithForceOption(array $groupDefinitions) |
82
|
|
|
{ |
83
|
|
|
$this->setMockCategoriesService(); |
84
|
|
|
$this->setMockDbConnection(); |
85
|
|
|
$this->setMockSchematicFields(); |
86
|
|
|
|
87
|
|
|
$schematicUserGroupsService = new CategoryGroups(); |
88
|
|
|
|
89
|
|
|
$import = $schematicUserGroupsService->import($groupDefinitions, true); |
90
|
|
|
|
91
|
|
|
$this->assertInstanceOf(Result::class, $import); |
92
|
|
|
$this->assertFalse($import->hasErrors()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//============================================================================================================== |
96
|
|
|
//============================================== PROVIDERS =================================================== |
97
|
|
|
//============================================================================================================== |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function provideValidCategoryGroups() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'emptyArray' => [ |
106
|
|
|
'CategoryGroups' => [], |
107
|
|
|
'expectedResult' => [], |
108
|
|
|
], |
109
|
|
|
'single group' => [ |
110
|
|
|
'CategoryGroups' => [ |
111
|
|
|
'group1' => $this->getMockCategoryGroup(1), |
112
|
|
|
], |
113
|
|
|
'expectedResult' => [ |
114
|
|
|
'groupHandle1' => [ |
115
|
|
|
'name' => 'groupName1', |
116
|
|
|
'hasUrls' => null, |
117
|
|
|
'template' => null, |
118
|
|
|
'maxLevels' => null, |
119
|
|
|
'locales' => [ |
120
|
|
|
'en' => [ |
121
|
|
|
'urlFormat' => null, |
122
|
|
|
'nestedUrlFormat' => null, |
123
|
|
|
], |
124
|
|
|
], |
125
|
|
|
'fieldLayout' => [ |
126
|
|
|
'fields' => [] |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
], |
131
|
|
|
'multiple groups' => [ |
132
|
|
|
'CategoryGroups' => [ |
133
|
|
|
'group1' => $this->getMockCategoryGroup(1), |
134
|
|
|
'group2' => $this->getMockCategoryGroup(2), |
135
|
|
|
], |
136
|
|
|
'expectedResult' => [ |
137
|
|
|
'groupHandle1' => [ |
138
|
|
|
'name' => 'groupName1', |
139
|
|
|
'hasUrls' => null, |
140
|
|
|
'template' => null, |
141
|
|
|
'maxLevels' => null, |
142
|
|
|
'locales' => [ |
143
|
|
|
'en' => [ |
144
|
|
|
'urlFormat' => null, |
145
|
|
|
'nestedUrlFormat' => null, |
146
|
|
|
], |
147
|
|
|
], |
148
|
|
|
'fieldLayout' => [ |
149
|
|
|
'fields' => [] |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
'groupHandle2' => [ |
153
|
|
|
'name' => 'groupName2', |
154
|
|
|
'hasUrls' => null, |
155
|
|
|
'template' => null, |
156
|
|
|
'maxLevels' => null, |
157
|
|
|
'locales' => [ |
158
|
|
|
'en' => [ |
159
|
|
|
'urlFormat' => null, |
160
|
|
|
'nestedUrlFormat' => null, |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
'fieldLayout' => [ |
164
|
|
|
'fields' => [] |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
], |
168
|
|
|
], |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function provideValidCategoryGroupDefinitions() |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
'emptyArray' => [ |
179
|
|
|
'groupDefinitions' => [], |
180
|
|
|
], |
181
|
|
|
'single group' => [ |
182
|
|
|
'groupDefinitions' => [ |
183
|
|
|
'groupHandle1' => [ |
184
|
|
|
'name' => 'groupName1', |
185
|
|
|
'hasUrls' => false, |
186
|
|
|
'template' => '', |
187
|
|
|
'maxLevels' => 3, |
188
|
|
|
'locales' => [ |
189
|
|
|
'en' => [ |
190
|
|
|
'urlFormat' => '', |
191
|
|
|
'nestedUrlFormat' => '', |
192
|
|
|
], |
193
|
|
|
], |
194
|
|
|
'fieldLayout' => [ |
195
|
|
|
'fields' => [] |
196
|
|
|
], |
197
|
|
|
], |
198
|
|
|
], |
199
|
|
|
], |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
//============================================================================================================== |
204
|
|
|
//================================================= MOCKS ==================================================== |
205
|
|
|
//============================================================================================================== |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $groupId |
209
|
|
|
* |
210
|
|
|
* @return Mock|CategoryGroupModel |
211
|
|
|
*/ |
212
|
|
|
private function getMockCategoryGroup($groupId) |
213
|
|
|
{ |
214
|
|
|
$mockCategoryGroup = $this->getMockBuilder(CategoryGroupModel::class) |
215
|
|
|
->disableOriginalConstructor() |
216
|
|
|
->getMock(); |
217
|
|
|
|
218
|
|
|
$mockCategoryGroup->expects($this->any()) |
219
|
|
|
->method('__get') |
220
|
|
|
->willReturnMap([ |
221
|
|
|
['id', $groupId], |
222
|
|
|
['fieldLayoutId', $groupId], |
223
|
|
|
['handle', 'groupHandle' . $groupId], |
224
|
|
|
['name', 'groupName' . $groupId], |
225
|
|
|
]); |
226
|
|
|
|
227
|
|
|
$mockCategoryGroup->expects($this->any()) |
228
|
|
|
->method('getLocales') |
229
|
|
|
->willReturn([$this->getMockCategoryGroupLocale()]); |
230
|
|
|
|
231
|
|
|
$mockCategoryGroup->expects($this->any()) |
232
|
|
|
->method('getAllErrors') |
233
|
|
|
->willReturn([ |
234
|
|
|
'ohnoes' => 'horrible error', |
235
|
|
|
]); |
236
|
|
|
|
237
|
|
|
return $mockCategoryGroup; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return Mock|CraftFieldsService |
242
|
|
|
*/ |
243
|
|
|
private function setMockFieldsService() |
244
|
|
|
{ |
245
|
|
|
$mockFieldsService = $this->getMockBuilder(FieldsService::class) |
246
|
|
|
->disableOriginalConstructor() |
247
|
|
|
->getMock(); |
248
|
|
|
|
249
|
|
|
$mockFieldsService->expects($this->any()) |
250
|
|
|
->method('getLayoutById') |
251
|
|
|
->with($this->isType('integer')) |
252
|
|
|
->willReturn($this->getMockFieldLayout()); |
253
|
|
|
|
254
|
|
|
$this->setComponent(Craft::app(), 'fields', $mockFieldsService); |
255
|
|
|
|
256
|
|
|
return $mockFieldsService; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return Mock|fields |
261
|
|
|
*/ |
262
|
|
|
private function setMockSchematicFields() |
263
|
|
|
{ |
264
|
|
|
$mockSchematicFields = $this->getMockBuilder(Fields::class) |
265
|
|
|
->disableOriginalConstructor() |
266
|
|
|
->getMock(); |
267
|
|
|
|
268
|
|
|
$mockSchematicFields->expects($this->any()) |
269
|
|
|
->method('getFieldLayoutDefinition') |
270
|
|
|
->with($this->isInstanceOf(FieldLayoutModel::class)) |
271
|
|
|
->willReturn(['fields' => []]); |
272
|
|
|
|
273
|
|
|
$mockSchematicFields->expects($this->any()) |
274
|
|
|
->method('getFieldLayout') |
275
|
|
|
->with($this->isType('array')) |
276
|
|
|
->willReturn($this->getMockFieldLayout()); |
277
|
|
|
|
278
|
|
|
$this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFields); |
279
|
|
|
|
280
|
|
|
return $mockSchematicFields; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return Mock|CategoriesService |
285
|
|
|
*/ |
286
|
|
|
private function setMockCategoriesService() |
287
|
|
|
{ |
288
|
|
|
$mockCategoriesService = $this->getMockBuilder(CategoriesService::class) |
289
|
|
|
->disableOriginalConstructor() |
290
|
|
|
->getMock(); |
291
|
|
|
|
292
|
|
|
$mockCategoriesService->expects($this->any()) |
293
|
|
|
->method('getAllGroups') |
294
|
|
|
->with('handle') |
295
|
|
|
->willReturn([]); |
296
|
|
|
|
297
|
|
|
$this->setComponent(Craft::app(), 'categories', $mockCategoriesService); |
298
|
|
|
|
299
|
|
|
return $mockCategoriesService; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return Mock|FieldLayoutModel |
305
|
|
|
*/ |
306
|
|
|
private function getMockFieldLayout() |
307
|
|
|
{ |
308
|
|
|
$mockFieldLayout = $this->getMockBuilder(FieldLayoutModel::class) |
309
|
|
|
->disableOriginalConstructor() |
310
|
|
|
->getMock(); |
311
|
|
|
|
312
|
|
|
return $mockFieldLayout; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return Mock|CategoryGroupLocaleModel |
317
|
|
|
*/ |
318
|
|
|
private function getMockCategoryGroupLocale() |
319
|
|
|
{ |
320
|
|
|
$mockCategoryGroupLocale = $this->getMockBuilder(CategoryGroupLocaleModel::class) |
321
|
|
|
->disableOriginalConstructor() |
322
|
|
|
->getMock(); |
323
|
|
|
|
324
|
|
|
$mockCategoryGroupLocale->expects($this->any()) |
325
|
|
|
->method('__get') |
326
|
|
|
->willReturnMap([ |
327
|
|
|
['locale', 'en'], |
328
|
|
|
]); |
329
|
|
|
|
330
|
|
|
return $mockCategoryGroupLocale; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return Mock|DbConnection |
335
|
|
|
*/ |
336
|
|
|
private function setMockDbConnection() |
337
|
|
|
{ |
338
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
339
|
|
|
->disableOriginalConstructor() |
340
|
|
|
->setMethods(['createCommand']) |
341
|
|
|
->getMock(); |
342
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
343
|
|
|
|
344
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
345
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
346
|
|
|
|
347
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
return $mockDbConnection; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @return Mock|DbCommand |
355
|
|
|
*/ |
356
|
|
|
private function getMockDbCommand() |
357
|
|
|
{ |
358
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
359
|
|
|
->disableOriginalConstructor() |
360
|
|
|
->setMethods(['insertOrUpdate']) |
361
|
|
|
->getMock(); |
362
|
|
|
|
363
|
|
|
return $mockDbCommand; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|