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