1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\TagsService; |
7
|
|
|
use Craft\TagGroupLocaleModel; |
8
|
|
|
use Craft\TagGroupModel; |
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 TagGroupsTest. |
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\TagGroups |
27
|
|
|
* @covers ::__construct |
28
|
|
|
* @covers ::<!public> |
29
|
|
|
*/ |
30
|
|
|
class TagGroupsTest extends BaseTest |
31
|
|
|
{ |
32
|
|
|
//============================================================================================================== |
33
|
|
|
//================================================= TESTS ==================================================== |
34
|
|
|
//============================================================================================================== |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers ::export |
38
|
|
|
* @dataProvider provideValidTagGroups |
39
|
|
|
* |
40
|
|
|
* @param TagGroupModel[] $groups |
41
|
|
|
* @param array $expectedResult |
42
|
|
|
*/ |
43
|
|
|
public function testSuccessfulExport(array $groups, array $expectedResult = []) |
44
|
|
|
{ |
45
|
|
|
$this->setMockFieldsService(); |
46
|
|
|
$this->setMockSchematicFields(); |
47
|
|
|
|
48
|
|
|
$schematicTagGroupsService = new TagGroups(); |
49
|
|
|
|
50
|
|
|
$actualResult = $schematicTagGroupsService->export($groups); |
51
|
|
|
|
52
|
|
|
$this->assertSame($expectedResult, $actualResult); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @covers ::import |
57
|
|
|
* @dataProvider provideValidTagGroupDefinitions |
58
|
|
|
* |
59
|
|
|
* @param array $groupDefinitions |
60
|
|
|
*/ |
61
|
|
|
public function testSuccessfulImport(array $groupDefinitions) |
62
|
|
|
{ |
63
|
|
|
$this->setMockTagsService(); |
64
|
|
|
$this->setMockDbConnection(); |
65
|
|
|
$this->setMockSchematicFields(); |
66
|
|
|
|
67
|
|
|
$schematicUserGroupsService = new TagGroups(); |
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 provideValidTagGroupDefinitions |
78
|
|
|
* |
79
|
|
|
* @param array $groupDefinitions |
80
|
|
|
*/ |
81
|
|
|
public function testImportWithForceOption(array $groupDefinitions) |
82
|
|
|
{ |
83
|
|
|
$this->setMockTagsService(); |
84
|
|
|
$this->setMockDbConnection(); |
85
|
|
|
$this->setMockSchematicFields(); |
86
|
|
|
|
87
|
|
|
$schematicUserGroupsService = new TagGroups(); |
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 provideValidTagGroups() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'emptyArray' => [ |
106
|
|
|
'TagGroups' => [], |
107
|
|
|
'expectedResult' => [], |
108
|
|
|
], |
109
|
|
|
'single group' => [ |
110
|
|
|
'TagGroups' => [ |
111
|
|
|
'group1' => $this->getMockTagGroup(1), |
112
|
|
|
], |
113
|
|
|
'expectedResult' => [ |
114
|
|
|
'groupHandle1' => [ |
115
|
|
|
'name' => 'groupName1', |
116
|
|
|
'fieldLayout' => [ |
117
|
|
|
'fields' => [] |
118
|
|
|
], |
119
|
|
|
], |
120
|
|
|
], |
121
|
|
|
], |
122
|
|
|
'multiple groups' => [ |
123
|
|
|
'TagGroups' => [ |
124
|
|
|
'group1' => $this->getMockTagGroup(1), |
125
|
|
|
'group2' => $this->getMockTagGroup(2), |
126
|
|
|
], |
127
|
|
|
'expectedResult' => [ |
128
|
|
|
'groupHandle1' => [ |
129
|
|
|
'name' => 'groupName1', |
130
|
|
|
'fieldLayout' => [ |
131
|
|
|
'fields' => [] |
132
|
|
|
], |
133
|
|
|
], |
134
|
|
|
'groupHandle2' => [ |
135
|
|
|
'name' => 'groupName2', |
136
|
|
|
'fieldLayout' => [ |
137
|
|
|
'fields' => [] |
138
|
|
|
], |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function provideValidTagGroupDefinitions() |
149
|
|
|
{ |
150
|
|
|
return [ |
151
|
|
|
'emptyArray' => [ |
152
|
|
|
'groupDefinitions' => [], |
153
|
|
|
], |
154
|
|
|
'single group' => [ |
155
|
|
|
'groupDefinitions' => [ |
156
|
|
|
'groupHandle1' => [ |
157
|
|
|
'name' => 'groupName1', |
158
|
|
|
'fieldLayout' => [ |
159
|
|
|
'fields' => [] |
160
|
|
|
], |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
//============================================================================================================== |
168
|
|
|
//================================================= MOCKS ==================================================== |
169
|
|
|
//============================================================================================================== |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $groupId |
173
|
|
|
* |
174
|
|
|
* @return Mock|TagGroupModel |
175
|
|
|
*/ |
176
|
|
|
private function getMockTagGroup($groupId) |
177
|
|
|
{ |
178
|
|
|
$mockTagGroup = $this->getMockBuilder(TagGroupModel::class) |
179
|
|
|
->setMethods(['__get', 'getAllErrors', 'getFieldLayout']) |
180
|
|
|
//->disableOriginalConstructor() |
181
|
|
|
->getMock(); |
182
|
|
|
|
183
|
|
|
$mockTagGroup->expects($this->any()) |
184
|
|
|
->method('__get') |
185
|
|
|
->willReturnMap([ |
186
|
|
|
['id', $groupId], |
187
|
|
|
['fieldLayoutId', $groupId], |
188
|
|
|
['handle', 'groupHandle' . $groupId], |
189
|
|
|
['name', 'groupName' . $groupId], |
190
|
|
|
]); |
191
|
|
|
|
192
|
|
|
$mockTagGroup->expects($this->any()) |
193
|
|
|
->method('getAllErrors') |
194
|
|
|
->willReturn([ |
195
|
|
|
'ohnoes' => 'horrible error', |
196
|
|
|
]); |
197
|
|
|
|
198
|
|
|
$mockTagGroup->expects($this->any()) |
199
|
|
|
->method('getFieldLayout') |
200
|
|
|
->willReturn($this->getMockFieldLayout()); |
201
|
|
|
|
202
|
|
|
return $mockTagGroup; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return Mock|CraftFieldsService |
207
|
|
|
*/ |
208
|
|
|
private function setMockFieldsService() |
209
|
|
|
{ |
210
|
|
|
$mockFieldsService = $this->getMockBuilder(FieldsService::class) |
211
|
|
|
->disableOriginalConstructor() |
212
|
|
|
->getMock(); |
213
|
|
|
|
214
|
|
|
$mockFieldsService->expects($this->any()) |
215
|
|
|
->method('getLayoutById') |
216
|
|
|
->with($this->isType('integer')) |
217
|
|
|
->willReturn($this->getMockFieldLayout()); |
218
|
|
|
|
219
|
|
|
$this->setComponent(Craft::app(), 'fields', $mockFieldsService); |
220
|
|
|
|
221
|
|
|
return $mockFieldsService; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return Mock|fields |
226
|
|
|
*/ |
227
|
|
|
private function setMockSchematicFields() |
228
|
|
|
{ |
229
|
|
|
$mockSchematicFields = $this->getMockBuilder(Fields::class) |
230
|
|
|
->disableOriginalConstructor() |
231
|
|
|
->getMock(); |
232
|
|
|
|
233
|
|
|
$mockSchematicFields->expects($this->any()) |
234
|
|
|
->method('getFieldLayoutDefinition') |
235
|
|
|
->with($this->isInstanceOf(FieldLayoutModel::class)) |
236
|
|
|
->willReturn(['fields' => []]); |
237
|
|
|
|
238
|
|
|
$mockSchematicFields->expects($this->any()) |
239
|
|
|
->method('getFieldLayout') |
240
|
|
|
->with($this->isType('array')) |
241
|
|
|
->willReturn($this->getMockFieldLayout()); |
242
|
|
|
|
243
|
|
|
$this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFields); |
244
|
|
|
|
245
|
|
|
return $mockSchematicFields; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return Mock|TagsService |
250
|
|
|
*/ |
251
|
|
|
private function setMockTagsService() |
252
|
|
|
{ |
253
|
|
|
$mockTagsService = $this->getMockBuilder(TagsService::class) |
254
|
|
|
->disableOriginalConstructor() |
255
|
|
|
->setMethods(['getAllTagGroups', 'saveTagGroup', 'deleteTagGroupById']) |
256
|
|
|
->getMock(); |
257
|
|
|
|
258
|
|
|
$mockTagsService->expects($this->any()) |
259
|
|
|
->method('getAllTagGroups') |
260
|
|
|
->with('handle') |
261
|
|
|
->willReturn([]); |
262
|
|
|
|
263
|
|
|
$this->setComponent(Craft::app(), 'tags', $mockTagsService); |
264
|
|
|
|
265
|
|
|
return $mockTagsService; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return Mock|FieldLayoutModel |
271
|
|
|
*/ |
272
|
|
|
private function getMockFieldLayout() |
273
|
|
|
{ |
274
|
|
|
$mockFieldLayout = $this->getMockBuilder(FieldLayoutModel::class) |
275
|
|
|
->disableOriginalConstructor() |
276
|
|
|
->getMock(); |
277
|
|
|
|
278
|
|
|
return $mockFieldLayout; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return Mock|DbConnection |
283
|
|
|
*/ |
284
|
|
|
private function setMockDbConnection() |
285
|
|
|
{ |
286
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
287
|
|
|
->disableOriginalConstructor() |
288
|
|
|
->setMethods(['createCommand']) |
289
|
|
|
->getMock(); |
290
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
291
|
|
|
|
292
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
293
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
294
|
|
|
|
295
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
return $mockDbConnection; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @return Mock|DbCommand |
303
|
|
|
*/ |
304
|
|
|
private function getMockDbCommand() |
305
|
|
|
{ |
306
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
307
|
|
|
->disableOriginalConstructor() |
308
|
|
|
->setMethods(['insertOrUpdate']) |
309
|
|
|
->getMock(); |
310
|
|
|
|
311
|
|
|
return $mockDbCommand; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|