Completed
Push — master ( 27039d...db9c49 )
by Bart
13s
created

testImportFromYamlExcludingDataTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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
     * Test import from yml excluding data types.
286
     *
287
     * @covers ::exportToYaml
288
     */
289
    public function testImportFromYamlExcludingDataTypes()
290
    {
291
        //We do not want to import these DataTypes, so these import functions should not be called.
292
        Craft::app()->schematic_users->expects($this->exactly(0))->method('import');
293
        Craft::app()->schematic_plugins->expects($this->exactly(0))->method('import');
294
295
        //We do want to import these DataTypes, so these import functions should be called.
296
        Craft::app()->schematic_userGroups->expects($this->exactly(1))->method('import');
297
        Craft::app()->schematic_assetSources->expects($this->exactly(1))->method('import');
298
299
        $results = $this->schematicService->importFromYaml($this->getYamlTestFile(), null, false, ['assetSources', 'userGroups']);
300
        $this->assertFalse($results->hasErrors());
301
    }
302
303
    /**
304
     * @param $service
305
     *
306
     * @return Mock
307
     */
308
    private function getMockAllGroupsMethodService($service)
309
    {
310
        return $this->getDynamicallyMockedService($service, 'getAllGroups', $this->exactly(1), []);
311
    }
312
313
    /**
314
     * Prep export services.
315
     */
316
    private function prepExportMockServices()
317
    {
318
        $mockPluginsService = $this->getMockPluginsService();
319
        $this->setCraftComponent('plugins', $mockPluginsService);
320
321
        $mockFieldsService = $this->getMockAllGroupsMethodService(FieldsService::class);
322
        $this->setCraftComponent('fields', $mockFieldsService);
323
324
        $mockSectionsService = $this->getMockSectionsService();
325
        $this->setCraftComponent('sections', $mockSectionsService);
326
327
        $mockGlobalsService = $this->getMockGlobalsService();
328
        $this->setCraftComponent('globals', $mockGlobalsService);
329
330
        $mockUserGroupsService = $this->getMockAllGroupsMethodService(UserGroupsService::class);
331
        $this->setCraftComponent('userGroups', $mockUserGroupsService);
332
333
        $mockAssetSourcesService = $this->getMockAssetSourcesService();
334
        $this->setCraftComponent('assetSources', $mockAssetSourcesService);
335
336
        $mockAssetTransformsService = $this->getMockAssetTransformsService();
337
        $this->setCraftComponent('assetTransforms', $mockAssetTransformsService);
338
339
        $mockCategoriesService = $this->getMockCategoriesService();
340
        $this->setCraftComponent('categories', $mockCategoriesService);
341
342
        $mockTagsService = $this->getMockTagsService();
343
        $this->setCraftComponent('tags', $mockTagsService);
344
    }
345
346
    /**
347
     * Test export to yml.
348
     *
349
     * @covers ::exportToYaml
350
     */
351
    public function testExportFromYaml()
352
    {
353
        $this->prepExportMockServices();
354
355
        $results = $this->schematicService->exportToYaml($this->getYamlExportFile());
356
        $this->assertFalse($results->hasErrors());
357
    }
358
359
    /**
360
     * Test export to yml excluding data types.
361
     *
362
     * @covers ::exportToYaml
363
     */
364
    public function testExportToYamlExcludingDataTypes()
365
    {
366
        $this->prepExportMockServices();
367
368
        $exportableDataTypes = Schematic::getExportableDataTypes();
369
370
        $dataTypesToExport = array_diff($exportableDataTypes, ['pluginData']);
371
372
        $results = $this->schematicService->exportToYaml($this->getYamlExportFile(), $dataTypesToExport);
373
        $this->assertFalse($results->hasErrors());
374
375
        // Read and process the recently created export YAML file.
376
        $yaml = IOHelper::getFileContents($this->getYamlExportFile());
377
        $dataModel = Data::fromYaml($yaml, []);
378
379
        // Make sure the excluded data type was not exported.
380
        $this->assertEmpty($dataModel->pluginData);
381
    }
382
383
    /**
384
     * Test export to yml with error writing to file.
385
     *
386
     * @covers ::exportToYaml
387
     */
388
    public function testExportFromYamlWithFileError()
389
    {
390
        $this->prepExportMockServices();
391
392
        $results = $this->schematicService->exportToYaml('non-existing-folder/not-a-file', 'all', false);
393
        $this->assertTrue($results->hasErrors());
394
    }
395
}
396