Completed
Pull Request — master (#55)
by Bart
04:31
created

SchematicTest::prepExportMockServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\CategoriesService;
6
use Craft\Craft;
7
use Craft\BaseTest;
8
use Craft\FieldsService;
9
use Craft\GlobalsService;
10
use Craft\SectionsService;
11
use Craft\PluginsService;
12
use Craft\UserGroupsService;
13
use NerdsAndCompany\Schematic\Models\Result;
14
use PHPUnit_Framework_MockObject_MockObject as Mock;
15
use PHPUnit_Framework_MockObject_Matcher_Invocation as Invocation;
16
17
/**
18
 * Class Schematic_UsersServiceTest.
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\Schematic
27
 * @covers ::<!public>
28
 */
29
class SchematicTest extends BaseTest
30
{
31
    /**
32
     * @var Schematic
33
     */
34
    private $schematicService;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setUp()
40
    {
41
        $this->schematicService = new Schematic();
42
        $this->mockServices();
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    private function getYamlTestFile()
49
    {
50
        return __DIR__.'/../data/test_schema.yml';
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    private function getYamlExportFile()
57
    {
58
        return __DIR__.'/../data/test_schema_export.yml';
59
    }
60
61
    /**
62
     * @param string $handle
63
     * @param Mock   $mock
64
     */
65
    private function setCraftComponent($handle, Mock $mock)
66
    {
67
        $this->setComponent(Craft::app(), $handle, $mock);
68
    }
69
70
    /**
71
     * @return Mock|FieldsService
72
     */
73
    public function getMockFieldsService()
74
    {
75
        $mock = $this->getMockBuilder(FieldsService::class)
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $mock->expects($this->exactly(1))->method('getAllGroups')->willReturn([]);
80
81
        return $mock;
82
    }
83
84
    /**
85
     * @param string     $class
86
     * @param string     $method
87
     * @param Invocation $invocation
88
     * @param mixed      $returnValue
89
     *
90
     * @return Mock
91
     */
92
    public function getDynamicallyMockedService($class, $method, Invocation $invocation, $returnValue)
93
    {
94
        $mock = $this->getMockBuilder($class)->disableOriginalConstructor()->getMock();
95
96
        $mock->expects($invocation)->method($method)->willReturn($returnValue);
97
98
        return $mock;
99
    }
100
101
    /**
102
     * @return Mock|GlobalsService
103
     */
104
    public function getMockGlobalsService()
105
    {
106
        $mock = $this->getMockBuilder(GlobalsService::class)
107
            ->disableOriginalConstructor()
108
            ->getMock();
109
110
        $mock->expects($this->exactly(1))->method('getAllSets')->willReturn([]);
111
112
        return $mock;
113
    }
114
115
    /**
116
     * @return Mock|SectionsService
117
     */
118
    public function getMockSectionsService()
119
    {
120
        $mock = $this->getMockBuilder(SectionsService::class)
121
            ->disableOriginalConstructor()
122
            ->getMock();
123
124
        $mock->expects($this->exactly(1))->method('getAllSections')->willReturn([]);
125
126
        return $mock;
127
    }
128
129
    /**
130
     * @return Mock|Result
131
     */
132
    public function getMockResultModel()
133
    {
134
        $mock = $this->getMockBuilder(Result::class)
135
            ->disableOriginalConstructor()
136
            ->getMock();
137
138
        return $mock;
139
    }
140
141
    /**
142
     * Creates mock for service.
143
     *
144
     * @param string $service
145
     * @param string $handle
146
     */
147
    private function createMockService($service, $handle)
148
    {
149
        $mock = $this->getMockBuilder($service)
150
            ->disableOriginalConstructor()
151
            ->getMock();
152
153
        $mock->expects($this->any())->method('import')->willReturn($this->getMockResultModel());
154
        $mock->expects($this->any())->method('export')->willReturn($this->getMockResultModel());
155
156
        $this->setCraftComponent($handle, $mock);
157
    }
158
159
    /**
160
     * @return Base|Mock
161
     */
162
    public function getMockAbstractService()
163
    {
164
        $mock = $this->getMockBuilder(Base::class)->getMock();
165
166
        $mock->expects($this->any())->method('import')->willReturn($this->getMockResultModel());
167
        $mock->expects($this->any())->method('export')->willReturn($this->getMockResultModel());
168
169
        return $mock;
170
    }
171
172
    /**
173
     * @return PluginsService|Mock
174
     */
175
    public function getMockPluginsService()
176
    {
177
        $mock = $this->getMockBuilder(PluginsService::class)
178
            ->disableOriginalConstructor()
179
            ->getMock();
180
181
        $mock->expects($this->any())->method('call')->willReturn([
182
            'TestPlugin' => [
183
                'test_service' => $this->getMockAbstractService(),
184
            ],
185
        ]);
186
187
        return $mock;
188
    }
189
190
    /**
191
     * @return Mock|CategoriesService
192
     */
193
    public function getMockCategoriesService()
194
    {
195
        $mock = $this->getMockBuilder(CategoriesService::class)
196
            ->disableOriginalConstructor()
197
            ->getMock();
198
199
        $mock->expects($this->exactly(1))->method('getAllGroups')->willReturn([]);
200
201
        return $mock;
202
    }
203
204
    /**
205
     * Mock all required services.
206
     */
207
    private function mockServices()
208
    {
209
        $this->createMockService(Locales::class, 'schematic_locales');
210
        $this->createMockService(AssetSources::class, 'schematic_assetSources');
211
        $this->createMockService(Fields::class, 'schematic_fields');
212
        $this->createMockService(GlobalSets::class, 'schematic_globalSets');
213
        $this->createMockService(Plugins::class, 'schematic_plugins');
214
        $this->createMockService(Sections::class, 'schematic_sections');
215
        $this->createMockService(UserGroups::class, 'schematic_userGroups');
216
        $this->createMockService(Users::class, 'schematic_users');
217
        $this->createMockService(CategoryGroups::class, 'schematic_categoryGroups');
218
        $this->createMockService(ElementIndexSettings::class, 'schematic_elementIndexSettings');
219
220
        $mockPluginsService = $this->getMockPluginsService();
221
        $this->setCraftComponent('plugins', $mockPluginsService);
222
    }
223
224
    /**
225
     * Test import from Yaml.
226
     *
227
     * @covers ::importFromYaml
228
     */
229
    public function testImportFromYamlWithForce()
230
    {
231
        $results = $this->schematicService->importFromYaml($this->getYamlTestFile(), null, true);
232
        $this->assertFalse($results->hasErrors());
233
    }
234
235
    /**
236
     * @param $service
237
     *
238
     * @return Mock
239
     */
240
    private function getMockAllGroupsMethodService($service)
241
    {
242
        return $this->getDynamicallyMockedService($service, 'getAllGroups', $this->exactly(1), []);
243
    }
244
245
    /**
246
     * Prep export services.
247
     */
248
    private function prepExportMockServices()
249
    {
250
        $mockPluginsService = $this->getMockPluginsService();
251
        $this->setCraftComponent('plugins', $mockPluginsService);
252
253
        $mockFieldsService = $this->getMockAllGroupsMethodService(FieldsService::class);
254
        $this->setCraftComponent('fields', $mockFieldsService);
255
256
        $mockSectionsService = $this->getMockSectionsService();
257
        $this->setCraftComponent('sections', $mockSectionsService);
258
259
        $mockGlobalsService = $this->getMockGlobalsService();
260
        $this->setCraftComponent('globals', $mockGlobalsService);
261
262
        $mockUserGroupsService = $this->getMockAllGroupsMethodService(UserGroupsService::class);
263
        $this->setCraftComponent('userGroups', $mockUserGroupsService);
264
265
        $mockCategoriesService = $this->getMockCategoriesService();
266
        $this->setCraftComponent('categories', $mockCategoriesService);
267
    }
268
269
    /**
270
     * Test export to yml.
271
     *
272
     * @covers ::exportToYaml
273
     */
274
    public function testExportFromYaml()
275
    {
276
        $this->prepExportMockServices();
277
278
        $results = $this->schematicService->exportToYaml($this->getYamlExportFile());
279
        $this->assertFalse($results->hasErrors());
280
    }
281
282
    /**
283
     * Test export to yml with error writing to file.
284
     *
285
     * @covers ::exportToYaml
286
     */
287
    public function testExportFromYamlWithFileError()
288
    {
289
        $this->prepExportMockServices();
290
291
        $results = $this->schematicService->exportToYaml('non-existing-folder/not-a-file', false);
292
        $this->assertTrue($results->hasErrors());
293
    }
294
}
295