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