Completed
Pull Request — master (#97)
by Bart
24:46 queued 21s
created

SourcesTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 335
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testGetMappedSourcesFromIdToHandle() 0 7 1
A testGetMappedSourcesFromHandleToId() 0 7 1
B provideValidSources() 0 110 1
A setMockSectionsService() 0 17 1
A setMockCategoriesService() 0 17 1
A setMockUserGroupsService() 0 17 1
A setMockSchematicAssetSources() 0 17 1
A setMockTagsService() 0 17 1
A setMockFieldsService() 0 17 1
A setMockGlobalsService() 0 17 1
A getMockObject() 0 8 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\BaseTest;
6
use Craft\CategoriesService;
7
use Craft\Craft;
8
use Craft\FieldsService;
9
use Craft\GlobalsService;
10
use Craft\SectionsService;
11
use Craft\TagsService;
12
use Craft\UserGroupsService;
13
use PHPUnit_Framework_MockObject_MockObject as Mock;
14
use stdClass;
15
16
/**
17
 * Class SourcesTest.
18
 *
19
 * @author    Nerds & Company
20
 * @copyright Copyright (c) 2015-2017, Nerds & Company
21
 * @license   MIT
22
 *
23
 * @link      http://www.nerds.company
24
 *
25
 * @coversDefaultClass NerdsAndCompany\Schematic\Services\Sources
26
 * @covers ::<!public>
27
 */
28
class SourcesTest extends BaseTest
29
{
30
    /**
31
     * Set up mock services
32
     */
33
    protected function setUp()
34
    {
35
        $this->setMockSectionsService();
36
        $this->setMockCategoriesService();
37
        $this->setMockUserGroupsService();
38
        $this->setMockSchematicAssetSources();
39
        $this->setMockTagsService();
40
        $this->setMockFieldsService();
41
        $this->setMockGlobalsService();
42
    }
43
44
    /**
45
     * @covers ::getMappedSources
46
     * @covers ::getSource
47
     * @dataProvider provideValidSources
48
     *
49
     * @param array  $idSources
50
     * @param array  $handleSources
51
     * @param string $fieldType
52
     */
53
    public function testGetMappedSourcesFromIdToHandle(array $idSources, array $handleSources, $fieldType = false)
54
    {
55
        $schematicSourcesService = new Sources();
56
57
        $result = $schematicSourcesService->getMappedSources($fieldType, $idSources, 'id', 'handle');
58
        $this->assertSame($handleSources, $result);
59
    }
60
61
    /**
62
     * @covers ::getMappedSources
63
     * @covers ::getSource
64
     * @dataProvider provideValidSources
65
     *
66
     * @param array  $idSources
67
     * @param array  $handleSources
68
     * @param string $fieldType
69
     */
70
    public function testGetMappedSourcesFromHandleToId(array $idSources, array $handleSources, $fieldType = false)
71
    {
72
        $schematicSourcesService = new Sources();
73
74
        $result = $schematicSourcesService->getMappedSources($fieldType, $handleSources, 'handle', 'id');
75
        $this->assertSame($idSources, $result);
76
    }
77
78
    //==============================================================================================================
79
    //==============================================  PROVIDERS  ===================================================
80
    //==============================================================================================================
81
82
    /**
83
     * @return array
84
     */
85
    public function provideValidSources()
86
    {
87
        return [
88
            'emptySources' => [
89
                'sources' => [],
90
                'expectedResult' => [],
91
            ],
92
            'allSources' => [
93
                'idSources' => ['*'],
94
                'handleSources' => ['*'],
95
            ],
96
            'sectionSources' => [
97
                'idSources' => [
98
                    'singles',
99
                    'section:1',
100
                    'section:3',
101
                ],
102
                'handleSources' => [
103
                    'singles',
104
                    'section:sectionHandle-1',
105
                    'section:sectionHandle-3',
106
                ],
107
            ],
108
            'userSources' => [
109
                'idSources' => [
110
                    'group:1',
111
                    'group:3',
112
                ],
113
                'handleSources' => [
114
                    'group:userGroupHandle-1',
115
                    'group:userGroupHandle-3',
116
                ],
117
                'fieldType' => 'Users',
118
            ],
119
            'categoryGroupSources' => [
120
                'idSources' => [
121
                    'group:1',
122
                    'group:3',
123
                ],
124
                'handleSources' => [
125
                    'group:categoryGroupHandle-1',
126
                    'group:categoryGroupHandle-3',
127
                ],
128
            ],
129
            'categoryGroupPermissions' => [
130
                'idSources' => [
131
                    'editCategories:1',
132
                    'editCategories:2',
133
                ],
134
                'handleSources' => [
135
                    'editCategories:categoryGroupHandle-1',
136
                    'editCategories:categoryGroupHandle-2',
137
                ],
138
            ],
139
            'folderSources' => [
140
                'idSources' => [
141
                    'folder:1',
142
                    'folder:2',
143
                ],
144
                'handleSources' => [
145
                    'folder:assetSourceHandle-1',
146
                    'folder:assetSourceHandle-2',
147
                ],
148
            ],
149
            'folderPermissions' => [
150
                'idSources' => [
151
                    'createSubfoldersInAssetSource:1',
152
                    'removeFromAssetSource:2',
153
                    'uploadToAssetSource:1',
154
                    'viewAssetSource:2',
155
                ],
156
                'handleSources' => [
157
                    'createSubfoldersInAssetSource:assetSourceHandle-1',
158
                    'removeFromAssetSource:assetSourceHandle-2',
159
                    'uploadToAssetSource:assetSourceHandle-1',
160
                    'viewAssetSource:assetSourceHandle-2',
161
                ],
162
            ],
163
            'tagGroupSources' => [
164
                'idSources' => [
165
                    'taggroup:1',
166
                    'taggroup:2',
167
                ],
168
                'handleSources' => [
169
                    'taggroup:tagGroupHandle-1',
170
                    'taggroup:tagGroupHandle-2',
171
                ],
172
            ],
173
            'fieldSources' => [
174
                'idSources' => [
175
                    'field:1',
176
                    'field:2',
177
                ],
178
                'handleSources' => [
179
                    'field:fieldHandle-1',
180
                    'field:fieldHandle-2',
181
                ],
182
            ],
183
            'globalSetPermissions' => [
184
                'idSources' => [
185
                    'editGlobalSet:1',
186
                    'editGlobalSet:2',
187
                ],
188
                'handleSources' => [
189
                    'editGlobalSet:globalSetHandle-1',
190
                    'editGlobalSet:globalSetHandle-2',
191
                ],
192
            ],
193
        ];
194
    }
195
196
    //==============================================================================================================
197
    //=================================================  MOCKS  ====================================================
198
    //==============================================================================================================
199
200
    /**
201
     * Set the mock sections service with getSectionById and getSectionByHandle
202
     */
203
    private function setMockSectionsService()
204
    {
205
        $mock = $this->getMockBuilder(SectionsService::class)
206
            ->disableOriginalConstructor()
207
            ->getMock();
208
209
        $mock->expects($this->any())->method('getSectionById')->willReturnCallback(function ($id) {
210
            return $this->getMockObject($id, 'sectionHandle-'.$id);
211
        });
212
213
        $mock->expects($this->any())->method('getSectionByHandle')->willReturnCallback(function ($handle) {
214
            $id = explode('-', $handle)[1];
215
            return $this->getMockObject($id, $handle);
216
        });
217
218
        Craft::app()->setComponent('sections', $mock);
219
    }
220
221
    /**
222
     * Set the mock categories service with getGroupById and getGroupByHandle
223
     */
224
    private function setMockCategoriesService()
225
    {
226
        $mock = $this->getMockBuilder(CategoriesService::class)
227
            ->disableOriginalConstructor()
228
            ->getMock();
229
230
        $mock->expects($this->any())->method('getGroupById')->willReturnCallback(function ($id) {
231
            return $this->getMockObject($id, 'categoryGroupHandle-'.$id);
232
        });
233
234
        $mock->expects($this->any())->method('getGroupByHandle')->willReturnCallback(function ($handle) {
235
            $id = explode('-', $handle)[1];
236
            return $this->getMockObject($id, $handle);
237
        });
238
239
        Craft::app()->setComponent('categories', $mock);
240
    }
241
242
    /**
243
     * Set the mock usergroups service with getGroupById and getGroupByHandle
244
     */
245
    private function setMockUserGroupsService()
246
    {
247
        $mock = $this->getMockBuilder(UserGroupsService::class)
248
            ->disableOriginalConstructor()
249
            ->getMock();
250
251
        $mock->expects($this->any())->method('getGroupById')->willReturnCallback(function ($id) {
252
            return $this->getMockObject($id, 'userGroupHandle-'.$id);
253
        });
254
255
        $mock->expects($this->any())->method('getGroupByHandle')->willReturnCallback(function ($handle) {
256
            $id = explode('-', $handle)[1];
257
            return $this->getMockObject($id, $handle);
258
        });
259
260
        Craft::app()->setComponent('userGroups', $mock);
261
    }
262
263
    /**
264
     * Set the mock schematic_assetsources with getGroupById and getGroupByHandle
265
     */
266
    private function setMockSchematicAssetSources()
267
    {
268
        $mock = $this->getMockBuilder(AssetSources::class)
269
            ->disableOriginalConstructor()
270
            ->getMock();
271
272
        $mock->expects($this->any())->method('getSourceById')->willReturnCallback(function ($id) {
273
            return $this->getMockObject($id, 'assetSourceHandle-'.$id);
274
        });
275
276
        $mock->expects($this->any())->method('getSourceByHandle')->willReturnCallback(function ($handle) {
277
            $id = explode('-', $handle)[1];
278
            return $this->getMockObject($id, $handle);
279
        });
280
281
        Craft::app()->setComponent('schematic_assetSources', $mock);
282
    }
283
284
    /**
285
     * Set mock tags service
286
     */
287
    private function setMockTagsService()
288
    {
289
        $mock = $this->getMockBuilder(TagsService::class)
290
            ->disableOriginalConstructor()
291
            ->getMock();
292
293
        $mock->expects($this->any())->method('getTagGroupById')->willReturnCallback(function ($id) {
294
            return $this->getMockObject($id, 'tagGroupHandle-'.$id);
295
        });
296
297
        $mock->expects($this->any())->method('getTagGroupByHandle')->willReturnCallback(function ($handle) {
298
            $id = explode('-', $handle)[1];
299
            return $this->getMockObject($id, $handle);
300
        });
301
302
        Craft::app()->setComponent('tags', $mock);
303
    }
304
305
    /**
306
     * Set mock fields service
307
     */
308
    private function setMockFieldsService()
309
    {
310
        $mock = $this->getMockBuilder(FieldsService::class)
311
            ->disableOriginalConstructor()
312
            ->getMock();
313
314
        $mock->expects($this->any())->method('getFieldById')->willReturnCallback(function ($id) {
315
            return $this->getMockObject($id, 'fieldHandle-'.$id);
316
        });
317
318
        $mock->expects($this->any())->method('getFieldByHandle')->willReturnCallback(function ($handle) {
319
            $id = explode('-', $handle)[1];
320
            return $this->getMockObject($id, $handle);
321
        });
322
323
        Craft::app()->setComponent('fields', $mock);
324
    }
325
326
    /**
327
     * Set mock fields service
328
     */
329
    private function setMockGlobalsService()
330
    {
331
        $mock = $this->getMockBuilder(GlobalsService::class)
332
            ->disableOriginalConstructor()
333
            ->getMock();
334
335
        $mock->expects($this->any())->method('getSetById')->willReturnCallback(function ($id) {
336
            return $this->getMockObject($id, 'globalSetHandle-'.$id);
337
        });
338
339
        $mock->expects($this->any())->method('getSetByHandle')->willReturnCallback(function ($handle) {
340
            $id = explode('-', $handle)[1];
341
            return $this->getMockObject($id, $handle);
342
        });
343
344
        Craft::app()->setComponent('globals', $mock);
345
    }
346
347
    /**
348
     * get a mock object with id and handle
349
     *
350
     * @param  int $id
351
     * @param  string $handle
352
     * @return stdClass
353
     */
354
    private function getMockObject($id, $handle)
355
    {
356
        $mockObject = new stdClass();
357
        $mockObject->id = $id;
358
        $mockObject->handle = $handle;
359
360
        return $mockObject;
361
    }
362
}
363