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
|
|
|
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
|
|
|
//============================================== PROVIDERS =================================================== |
78
|
|
|
//============================================================================================================== |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function provideValidCategoryGroups() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
'emptyArray' => [ |
87
|
|
|
'CategoryGroups' => [], |
88
|
|
|
'expectedResult' => [], |
89
|
|
|
], |
90
|
|
|
'single group' => [ |
91
|
|
|
'CategoryGroups' => [ |
92
|
|
|
'group1' => $this->getMockCategoryGroup(1), |
93
|
|
|
], |
94
|
|
|
'expectedResult' => [ |
95
|
|
|
'groupHandle1' => [ |
96
|
|
|
'name' => 'groupName1', |
97
|
|
|
'hasUrls' => null, |
98
|
|
|
'template' => null, |
99
|
|
|
'maxLevels' => null, |
100
|
|
|
'locales' => [ |
101
|
|
|
'en' => [ |
102
|
|
|
'urlFormat' => null, |
103
|
|
|
'nestedUrlFormat' => null, |
104
|
|
|
], |
105
|
|
|
], |
106
|
|
|
'fieldLayout' => [ |
107
|
|
|
'fields' => [] |
108
|
|
|
], |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'multiple groups' => [ |
113
|
|
|
'CategoryGroups' => [ |
114
|
|
|
'group1' => $this->getMockCategoryGroup(1), |
115
|
|
|
'group2' => $this->getMockCategoryGroup(2), |
116
|
|
|
], |
117
|
|
|
'expectedResult' => [ |
118
|
|
|
'groupHandle1' => [ |
119
|
|
|
'name' => 'groupName1', |
120
|
|
|
'hasUrls' => null, |
121
|
|
|
'template' => null, |
122
|
|
|
'maxLevels' => null, |
123
|
|
|
'locales' => [ |
124
|
|
|
'en' => [ |
125
|
|
|
'urlFormat' => null, |
126
|
|
|
'nestedUrlFormat' => null, |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
'fieldLayout' => [ |
130
|
|
|
'fields' => [] |
131
|
|
|
], |
132
|
|
|
], |
133
|
|
|
'groupHandle2' => [ |
134
|
|
|
'name' => 'groupName2', |
135
|
|
|
'hasUrls' => null, |
136
|
|
|
'template' => null, |
137
|
|
|
'maxLevels' => null, |
138
|
|
|
'locales' => [ |
139
|
|
|
'en' => [ |
140
|
|
|
'urlFormat' => null, |
141
|
|
|
'nestedUrlFormat' => null, |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
'fieldLayout' => [ |
145
|
|
|
'fields' => [] |
146
|
|
|
], |
147
|
|
|
], |
148
|
|
|
], |
149
|
|
|
], |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function provideValidCategoryGroupDefinitions() |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
|
|
'emptyArray' => [ |
160
|
|
|
'groupDefinitions' => [], |
161
|
|
|
], |
162
|
|
|
'single group' => [ |
163
|
|
|
'groupDefinitions' => [ |
164
|
|
|
'groupHandle1' => [ |
165
|
|
|
'name' => 'groupName1', |
166
|
|
|
'hasUrls' => false, |
167
|
|
|
'template' => '', |
168
|
|
|
'maxLevels' => 3, |
169
|
|
|
'locales' => [ |
170
|
|
|
'en' => [ |
171
|
|
|
'urlFormat' => '', |
172
|
|
|
'nestedUrlFormat' => '', |
173
|
|
|
], |
174
|
|
|
], |
175
|
|
|
'fieldLayout' => [ |
176
|
|
|
'fields' => [] |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
//============================================================================================================== |
185
|
|
|
//================================================= MOCKS ==================================================== |
186
|
|
|
//============================================================================================================== |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $groupId |
190
|
|
|
* |
191
|
|
|
* @return Mock|CategoryGroupModel |
192
|
|
|
*/ |
193
|
|
|
private function getMockCategoryGroup($groupId) |
194
|
|
|
{ |
195
|
|
|
$mockCategoryGroup = $this->getMockBuilder(CategoryGroupModel::class) |
196
|
|
|
->disableOriginalConstructor() |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$mockCategoryGroup->expects($this->any()) |
200
|
|
|
->method('__get') |
201
|
|
|
->willReturnMap([ |
202
|
|
|
['id', $groupId], |
203
|
|
|
['fieldLayoutId', $groupId], |
204
|
|
|
['handle', 'groupHandle' . $groupId], |
205
|
|
|
['name', 'groupName' . $groupId], |
206
|
|
|
]); |
207
|
|
|
|
208
|
|
|
$mockCategoryGroup->expects($this->any()) |
209
|
|
|
->method('getLocales') |
210
|
|
|
->willReturn([$this->getMockCategoryGroupLocale()]); |
211
|
|
|
|
212
|
|
|
$mockCategoryGroup->expects($this->any()) |
213
|
|
|
->method('getAllErrors') |
214
|
|
|
->willReturn([ |
215
|
|
|
'ohnoes' => 'horrible error', |
216
|
|
|
]); |
217
|
|
|
|
218
|
|
|
return $mockCategoryGroup; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return Mock|CraftFieldsService |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
private function setMockFieldsService() |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$mockFieldsService = $this->getMockBuilder(FieldsService::class) |
227
|
|
|
->disableOriginalConstructor() |
228
|
|
|
->getMock(); |
229
|
|
|
|
230
|
|
|
$mockFieldsService->expects($this->any()) |
231
|
|
|
->method('getLayoutById') |
232
|
|
|
->with($this->isType('integer')) |
233
|
|
|
->willReturn($this->getMockFieldLayout()); |
234
|
|
|
|
235
|
|
|
$this->setComponent(Craft::app(), 'fields', $mockFieldsService); |
236
|
|
|
|
237
|
|
|
return $mockFieldsService; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return Mock|fields |
242
|
|
|
*/ |
243
|
|
|
private function setMockSchematicFields() |
244
|
|
|
{ |
245
|
|
|
$mockSchematicFields = $this->getMockBuilder(Fields::class) |
246
|
|
|
->disableOriginalConstructor() |
247
|
|
|
->getMock(); |
248
|
|
|
|
249
|
|
|
$mockSchematicFields->expects($this->any()) |
250
|
|
|
->method('getFieldLayoutDefinition') |
251
|
|
|
->with($this->isInstanceOf(FieldLayoutModel::class)) |
252
|
|
|
->willReturn(['fields' => []]); |
253
|
|
|
|
254
|
|
|
$mockSchematicFields->expects($this->any()) |
255
|
|
|
->method('getFieldLayout') |
256
|
|
|
->with($this->isType('array')) |
257
|
|
|
->willReturn($this->getMockFieldLayout()); |
258
|
|
|
|
259
|
|
|
$this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFields); |
260
|
|
|
|
261
|
|
|
return $mockSchematicFields; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return Mock|CategoriesService |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
private function setMockCategoriesService() |
|
|
|
|
268
|
|
|
{ |
269
|
|
|
$mockCategoriesService = $this->getMockBuilder(CategoriesService::class) |
270
|
|
|
->disableOriginalConstructor() |
271
|
|
|
->getMock(); |
272
|
|
|
|
273
|
|
|
$mockCategoriesService->expects($this->any()) |
274
|
|
|
->method('getAllGroups') |
275
|
|
|
->with('handle') |
276
|
|
|
->willReturn([]); |
277
|
|
|
|
278
|
|
|
$this->setComponent(Craft::app(), 'categories', $mockCategoriesService); |
279
|
|
|
|
280
|
|
|
return $mockCategoriesService; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return Mock|FieldLayoutModel |
286
|
|
|
*/ |
287
|
|
|
private function getMockFieldLayout() |
288
|
|
|
{ |
289
|
|
|
$mockFieldLayout = $this->getMockBuilder(FieldLayoutModel::class) |
290
|
|
|
->disableOriginalConstructor() |
291
|
|
|
->getMock(); |
292
|
|
|
|
293
|
|
|
return $mockFieldLayout; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return Mock|CategoryGroupLocaleModel |
298
|
|
|
*/ |
299
|
|
|
private function getMockCategoryGroupLocale() |
300
|
|
|
{ |
301
|
|
|
$mockCategoryGroupLocale = $this->getMockBuilder(CategoryGroupLocaleModel::class) |
302
|
|
|
->disableOriginalConstructor() |
303
|
|
|
->getMock(); |
304
|
|
|
|
305
|
|
|
$mockCategoryGroupLocale->expects($this->any()) |
306
|
|
|
->method('__get') |
307
|
|
|
->willReturnMap([ |
308
|
|
|
['locale', 'en'], |
309
|
|
|
]); |
310
|
|
|
|
311
|
|
|
return $mockCategoryGroupLocale; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return Mock|DbConnection |
316
|
|
|
*/ |
317
|
|
|
private function setMockDbConnection() |
318
|
|
|
{ |
319
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
320
|
|
|
->disableOriginalConstructor() |
321
|
|
|
->setMethods(['createCommand']) |
322
|
|
|
->getMock(); |
323
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
324
|
|
|
|
325
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
326
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
327
|
|
|
|
328
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
return $mockDbConnection; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return Mock|DbCommand |
336
|
|
|
*/ |
337
|
|
|
private function getMockDbCommand() |
338
|
|
|
{ |
339
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
340
|
|
|
->disableOriginalConstructor() |
341
|
|
|
->setMethods(['insertOrUpdate']) |
342
|
|
|
->getMock(); |
343
|
|
|
|
344
|
|
|
return $mockDbCommand; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
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.