|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
|
6
|
|
|
use Craft\AssetSourcesService; |
|
7
|
|
|
use Craft\AssetTransformsService; |
|
8
|
|
|
use Craft\CategoriesService; |
|
9
|
|
|
use Craft\Craft; |
|
10
|
|
|
use Craft\FieldsService; |
|
11
|
|
|
use Craft\GlobalsService; |
|
12
|
|
|
use Craft\IOHelper; |
|
13
|
|
|
use Craft\PluginsService; |
|
14
|
|
|
use Craft\SectionsService; |
|
15
|
|
|
use Craft\TagsService; |
|
16
|
|
|
use Craft\UserGroupsService; |
|
17
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
|
18
|
|
|
use NerdsAndCompany\Schematic\Models\Data; |
|
19
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
|
20
|
|
|
use PHPUnit_Framework_MockObject_Matcher_Invocation as Invocation; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Schematic_UsersServiceTest. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Nerds & Company |
|
26
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
|
27
|
|
|
* @license MIT |
|
28
|
|
|
* |
|
29
|
|
|
* @link http://www.nerds.company |
|
30
|
|
|
* |
|
31
|
|
|
* @coversDefaultClass NerdsAndCompany\Schematic\Services\Schematic |
|
32
|
|
|
* @covers ::<!public> |
|
33
|
|
|
*/ |
|
34
|
|
|
class SchematicTest extends BaseTest |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var Schematic |
|
38
|
|
|
*/ |
|
39
|
|
|
private $schematicService; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setUp() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->schematicService = new Schematic(); |
|
47
|
|
|
$this->mockServices(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
private function getYamlTestFile() |
|
54
|
|
|
{ |
|
55
|
|
|
return __DIR__.'/../data/test_schema.yml'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
private function getYamlExportFile() |
|
62
|
|
|
{ |
|
63
|
|
|
return __DIR__.'/../data/test_schema_export.yml'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $handle |
|
68
|
|
|
* @param Mock $mock |
|
69
|
|
|
*/ |
|
70
|
|
|
private function setCraftComponent($handle, Mock $mock) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->setComponent(Craft::app(), $handle, $mock); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Mock|FieldsService |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getMockFieldsService() |
|
79
|
|
|
{ |
|
80
|
|
|
$mock = $this->getMockBuilder(FieldsService::class) |
|
81
|
|
|
->disableOriginalConstructor() |
|
82
|
|
|
->getMock(); |
|
83
|
|
|
|
|
84
|
|
|
$mock->expects($this->exactly(1))->method('getAllGroups')->willReturn([]); |
|
85
|
|
|
|
|
86
|
|
|
return $mock; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $class |
|
91
|
|
|
* @param string $method |
|
92
|
|
|
* @param Invocation $invocation |
|
93
|
|
|
* @param mixed $returnValue |
|
94
|
|
|
* |
|
95
|
|
|
* @return Mock |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getDynamicallyMockedService($class, $method, Invocation $invocation, $returnValue) |
|
98
|
|
|
{ |
|
99
|
|
|
$mock = $this->getMockBuilder($class)->disableOriginalConstructor()->getMock(); |
|
100
|
|
|
|
|
101
|
|
|
$mock->expects($invocation)->method($method)->willReturn($returnValue); |
|
102
|
|
|
|
|
103
|
|
|
return $mock; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return Mock|GlobalsService |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getMockGlobalsService() |
|
110
|
|
|
{ |
|
111
|
|
|
$mock = $this->getMockBuilder(GlobalsService::class) |
|
112
|
|
|
->disableOriginalConstructor() |
|
113
|
|
|
->getMock(); |
|
114
|
|
|
|
|
115
|
|
|
$mock->expects($this->exactly(1))->method('getAllSets')->willReturn([]); |
|
116
|
|
|
|
|
117
|
|
|
return $mock; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return Mock|SectionsService |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getMockSectionsService() |
|
124
|
|
|
{ |
|
125
|
|
|
$mock = $this->getMockBuilder(SectionsService::class) |
|
126
|
|
|
->disableOriginalConstructor() |
|
127
|
|
|
->getMock(); |
|
128
|
|
|
|
|
129
|
|
|
$mock->expects($this->exactly(1))->method('getAllSections')->willReturn([]); |
|
130
|
|
|
|
|
131
|
|
|
return $mock; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return Mock|Result |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getMockResultModel() |
|
138
|
|
|
{ |
|
139
|
|
|
$mock = $this->getMockBuilder(Result::class) |
|
140
|
|
|
->disableOriginalConstructor() |
|
141
|
|
|
->getMock(); |
|
142
|
|
|
|
|
143
|
|
|
return $mock; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Creates mock for service. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $service |
|
150
|
|
|
* @param string $handle |
|
151
|
|
|
*/ |
|
152
|
|
|
private function createMockService($service, $handle) |
|
153
|
|
|
{ |
|
154
|
|
|
$mock = $this->getMockBuilder($service) |
|
155
|
|
|
->disableOriginalConstructor() |
|
156
|
|
|
->getMock(); |
|
157
|
|
|
|
|
158
|
|
|
$mock->expects($this->any())->method('import')->willReturn($this->getMockResultModel()); |
|
159
|
|
|
$mock->expects($this->any())->method('export')->willReturn($this->getMockResultModel()); |
|
160
|
|
|
|
|
161
|
|
|
$this->setCraftComponent($handle, $mock); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return Base|Mock |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getMockAbstractService() |
|
168
|
|
|
{ |
|
169
|
|
|
$mock = $this->getMockBuilder(Base::class)->getMock(); |
|
170
|
|
|
|
|
171
|
|
|
$mock->expects($this->any())->method('import')->willReturn($this->getMockResultModel()); |
|
172
|
|
|
$mock->expects($this->any())->method('export')->willReturn($this->getMockResultModel()); |
|
173
|
|
|
|
|
174
|
|
|
return $mock; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return PluginsService|Mock |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getMockPluginsService() |
|
181
|
|
|
{ |
|
182
|
|
|
$mock = $this->getMockBuilder(PluginsService::class) |
|
183
|
|
|
->disableOriginalConstructor() |
|
184
|
|
|
->getMock(); |
|
185
|
|
|
|
|
186
|
|
|
$mock->expects($this->any())->method('call')->willReturn([ |
|
187
|
|
|
'TestPlugin' => [ |
|
188
|
|
|
'test_service' => $this->getMockAbstractService(), |
|
189
|
|
|
], |
|
190
|
|
|
]); |
|
191
|
|
|
|
|
192
|
|
|
return $mock; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return Mock|AssetSourcesService |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getMockAssetSourcesService() |
|
199
|
|
|
{ |
|
200
|
|
|
$mock = $this->getMockBuilder(AssetSourcesService::class) |
|
201
|
|
|
->disableOriginalConstructor() |
|
202
|
|
|
->getMock(); |
|
203
|
|
|
|
|
204
|
|
|
$mock->expects($this->exactly(1))->method('getAllSources')->willReturn([]); |
|
205
|
|
|
|
|
206
|
|
|
return $mock; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return Mock|AssetTransformsService |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getMockAssetTransformsService() |
|
213
|
|
|
{ |
|
214
|
|
|
$mock = $this->getMockBuilder(AssetTransformsService::class) |
|
215
|
|
|
->disableOriginalConstructor() |
|
216
|
|
|
->getMock(); |
|
217
|
|
|
|
|
218
|
|
|
$mock->expects($this->exactly(1))->method('getAllTransforms')->willReturn([]); |
|
219
|
|
|
|
|
220
|
|
|
return $mock; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return Mock|CategoriesService |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getMockCategoriesService() |
|
227
|
|
|
{ |
|
228
|
|
|
$mock = $this->getMockBuilder(CategoriesService::class) |
|
229
|
|
|
->disableOriginalConstructor() |
|
230
|
|
|
->getMock(); |
|
231
|
|
|
|
|
232
|
|
|
$mock->expects($this->exactly(1))->method('getAllGroups')->willReturn([]); |
|
233
|
|
|
|
|
234
|
|
|
return $mock; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return Mock|TagsService |
|
239
|
|
|
*/ |
|
240
|
|
|
public function getMockTagsService() |
|
241
|
|
|
{ |
|
242
|
|
|
$mock = $this->getMockBuilder(TagsService::class) |
|
243
|
|
|
->disableOriginalConstructor() |
|
244
|
|
|
->getMock(); |
|
245
|
|
|
|
|
246
|
|
|
$mock->expects($this->exactly(1))->method('getAllTagGroups')->willReturn([]); |
|
247
|
|
|
|
|
248
|
|
|
return $mock; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Mock all required services. |
|
253
|
|
|
*/ |
|
254
|
|
|
private function mockServices() |
|
255
|
|
|
{ |
|
256
|
|
|
$this->createMockService(Locales::class, 'schematic_locales'); |
|
257
|
|
|
$this->createMockService(AssetSources::class, 'schematic_assetSources'); |
|
258
|
|
|
$this->createMockService(AssetTransforms::class, 'schematic_assetTransforms'); |
|
259
|
|
|
$this->createMockService(Fields::class, 'schematic_fields'); |
|
260
|
|
|
$this->createMockService(GlobalSets::class, 'schematic_globalSets'); |
|
261
|
|
|
$this->createMockService(Plugins::class, 'schematic_plugins'); |
|
262
|
|
|
$this->createMockService(Sections::class, 'schematic_sections'); |
|
263
|
|
|
$this->createMockService(UserGroups::class, 'schematic_userGroups'); |
|
264
|
|
|
$this->createMockService(Users::class, 'schematic_users'); |
|
265
|
|
|
$this->createMockService(CategoryGroups::class, 'schematic_categoryGroups'); |
|
266
|
|
|
$this->createMockService(TagGroups::class, 'schematic_tagGroups'); |
|
267
|
|
|
$this->createMockService(ElementIndexSettings::class, 'schematic_elementIndexSettings'); |
|
268
|
|
|
|
|
269
|
|
|
$mockPluginsService = $this->getMockPluginsService(); |
|
270
|
|
|
$this->setCraftComponent('plugins', $mockPluginsService); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Test import from Yaml. |
|
275
|
|
|
* |
|
276
|
|
|
* @covers ::importFromYaml |
|
277
|
|
|
*/ |
|
278
|
|
|
public function testImportFromYamlWithForce() |
|
279
|
|
|
{ |
|
280
|
|
|
$results = $this->schematicService->importFromYaml($this->getYamlTestFile(), null, true); |
|
281
|
|
|
$this->assertFalse($results->hasErrors()); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param $service |
|
286
|
|
|
* |
|
287
|
|
|
* @return Mock |
|
288
|
|
|
*/ |
|
289
|
|
|
private function getMockAllGroupsMethodService($service) |
|
290
|
|
|
{ |
|
291
|
|
|
return $this->getDynamicallyMockedService($service, 'getAllGroups', $this->exactly(1), []); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Prep export services. |
|
296
|
|
|
*/ |
|
297
|
|
|
private function prepExportMockServices() |
|
298
|
|
|
{ |
|
299
|
|
|
$mockPluginsService = $this->getMockPluginsService(); |
|
300
|
|
|
$this->setCraftComponent('plugins', $mockPluginsService); |
|
301
|
|
|
|
|
302
|
|
|
$mockFieldsService = $this->getMockAllGroupsMethodService(FieldsService::class); |
|
303
|
|
|
$this->setCraftComponent('fields', $mockFieldsService); |
|
304
|
|
|
|
|
305
|
|
|
$mockSectionsService = $this->getMockSectionsService(); |
|
306
|
|
|
$this->setCraftComponent('sections', $mockSectionsService); |
|
307
|
|
|
|
|
308
|
|
|
$mockGlobalsService = $this->getMockGlobalsService(); |
|
309
|
|
|
$this->setCraftComponent('globals', $mockGlobalsService); |
|
310
|
|
|
|
|
311
|
|
|
$mockUserGroupsService = $this->getMockAllGroupsMethodService(UserGroupsService::class); |
|
312
|
|
|
$this->setCraftComponent('userGroups', $mockUserGroupsService); |
|
313
|
|
|
|
|
314
|
|
|
$mockAssetSourcesService = $this->getMockAssetSourcesService(); |
|
315
|
|
|
$this->setCraftComponent('assetSources', $mockAssetSourcesService); |
|
316
|
|
|
|
|
317
|
|
|
$mockAssetTransformsService = $this->getMockAssetTransformsService(); |
|
318
|
|
|
$this->setCraftComponent('assetTransforms', $mockAssetTransformsService); |
|
319
|
|
|
|
|
320
|
|
|
$mockCategoriesService = $this->getMockCategoriesService(); |
|
321
|
|
|
$this->setCraftComponent('categories', $mockCategoriesService); |
|
322
|
|
|
|
|
323
|
|
|
$mockTagsService = $this->getMockTagsService(); |
|
324
|
|
|
$this->setCraftComponent('tags', $mockTagsService); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Test export to yml. |
|
329
|
|
|
* |
|
330
|
|
|
* @covers ::exportToYaml |
|
331
|
|
|
*/ |
|
332
|
|
|
public function testExportFromYaml() |
|
333
|
|
|
{ |
|
334
|
|
|
$this->prepExportMockServices(); |
|
335
|
|
|
|
|
336
|
|
|
$results = $this->schematicService->exportToYaml($this->getYamlExportFile()); |
|
337
|
|
|
$this->assertFalse($results->hasErrors()); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Test export to yml excluding data types. |
|
342
|
|
|
* |
|
343
|
|
|
* @covers ::exportToYaml |
|
344
|
|
|
*/ |
|
345
|
|
|
public function testExportToYamlExcludingDataTypes() |
|
346
|
|
|
{ |
|
347
|
|
|
$this->prepExportMockServices(); |
|
348
|
|
|
|
|
349
|
|
|
$exportableDataTypes = Schematic::getExportableDataTypes(); |
|
350
|
|
|
|
|
351
|
|
|
$dataTypesToExport = array_diff($exportableDataTypes, ['pluginData']); |
|
352
|
|
|
|
|
353
|
|
|
$results = $this->schematicService->exportToYaml($this->getYamlExportFile(), $dataTypesToExport); |
|
354
|
|
|
$this->assertFalse($results->hasErrors()); |
|
355
|
|
|
|
|
356
|
|
|
// Read and process the recently created export YAML file. |
|
357
|
|
|
$yaml = IOHelper::getFileContents($this->getYamlExportFile()); |
|
358
|
|
|
$dataModel = Data::fromYaml($yaml, []); |
|
359
|
|
|
|
|
360
|
|
|
// Make sure the excluded data type was not exported. |
|
361
|
|
|
$this->assertEmpty($dataModel->pluginData); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Test export to yml with error writing to file. |
|
366
|
|
|
* |
|
367
|
|
|
* @covers ::exportToYaml |
|
368
|
|
|
*/ |
|
369
|
|
|
public function testExportFromYamlWithFileError() |
|
370
|
|
|
{ |
|
371
|
|
|
$this->prepExportMockServices(); |
|
372
|
|
|
|
|
373
|
|
|
$results = $this->schematicService->exportToYaml('non-existing-folder/not-a-file', 'all', false); |
|
374
|
|
|
$this->assertTrue($results->hasErrors()); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|