Completed
Pull Request — master (#114)
by Bart
05:42
created

getMockCategoryGroupDefinition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\models\CategoryGroup;
7
use craft\models\CategoryGroup_SiteSettings;
8
use craft\models\FieldLayout;
9
use craft\models\Site;
10
use craft\services\Fields;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, NerdsAndCompany\Schematic\Services\Fields.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Codeception\Test\Unit;
12
use NerdsAndCompany\Schematic\Schematic;
13
14
/**
15
 * Class CategoryGroupsTest.
16
 *
17
 * @author    Nerds & Company
18
 * @copyright Copyright (c) 2015-2017, Nerds & Company
19
 * @license   MIT
20
 *
21
 * @see      http://www.nerds.company
22
 */
23
class CategoryGroupsTest extends Unit
24
{
25
    /**
26
     * @var CategoryGroups
27
     */
28
    private $service;
29
30
    /**
31
     * Set the service.
32
     *
33
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
34
     */
35
    protected function _before()
36
    {
37
        Craft::$app->sites->expects($this->any())
38
                  ->method('getSiteByHandle')
39
                  ->willReturn($this->getMockSite());
40
41
        $this->service = new CategoryGroups();
42
    }
43
44
    //==============================================================================================================
45
    //=================================================  TESTS  ====================================================
46
    //==============================================================================================================
47
48
    /**
49
     * @dataProvider provideValidCategoryGroups
50
     *
51
     * @param CategoryGroupModel[] $groups
52
     * @param array                $expectedResult
53
     */
54
    public function testSuccessfulExport(array $groups, array $expectedResult = [])
55
    {
56
        $this->expectList($groups);
57
58
        $actualResult = $this->service->export();
59
60
        $this->assertSame($expectedResult, $actualResult);
61
    }
62
63
    /**
64
     * @dataProvider provideValidCategoryGroupDefinitions
65
     *
66
     * @param array $groupDefinitions
67
     */
68
    public function testSuccessfulImport(array $groupDefinitions, array $existingGroups, int $saveCount)
69
    {
70
        $this->expectList($existingGroups);
71
        $this->expectSaves($saveCount);
72
        $this->expectDeletes(0);
73
74
        $this->service->import($groupDefinitions);
75
    }
76
77
    /**
78
     * @dataProvider provideValidCategoryGroupDefinitions
79
     *
80
     * @param array $groupDefinitions
81
     */
82
    public function testImportWithForceOption(array $groupDefinitions, array $existingGroups, int $saveCount, int $deleteCount)
83
    {
84
        Schematic::$force = true;
85
        $this->expectList($existingGroups);
86
        $this->expectSaves($saveCount);
87
        $this->expectDeletes($deleteCount);
88
89
        $this->service->import($groupDefinitions);
90
    }
91
92
    //==============================================================================================================
93
    //==============================================  PROVIDERS  ===================================================
94
    //==============================================================================================================
95
96
    /**
97
     * @return array
98
     */
99
    public function provideValidCategoryGroups()
100
    {
101
        $mockCategoryGroup1 = $this->getMockCategoryGroup(1);
102
        $mockCategoryGroup2 = $this->getMockCategoryGroup(2);
103
104
        return [
105
            'emptyArray' => [
106
                'CategoryGroups' => [],
107
                'expectedResult' => [],
108
            ],
109
            'single group' => [
110
                'CategoryGroups' => [
111
                    'group1' => $mockCategoryGroup1,
112
                ],
113
                'expectedResult' => [
114
                    'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1),
115
                ],
116
            ],
117
            'multiple groups' => [
118
                'CategoryGroups' => [
119
                    'group1' => $mockCategoryGroup1,
120
                    'group2' => $mockCategoryGroup2,
121
                ],
122
                'expectedResult' => [
123
                    'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1),
124
                    'groupHandle2' => $this->getMockCategoryGroupDefinition($mockCategoryGroup2),
125
                ],
126
            ],
127
        ];
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function provideValidCategoryGroupDefinitions()
134
    {
135
        $mockCategoryGroup1 = $this->getMockCategoryGroup(1);
136
        $mockCategoryGroup2 = $this->getMockCategoryGroup(2);
137
138
        return [
139
            'emptyArray' => [
140
                'groupDefinitions' => [],
141
                'existingGroups' => [
142
                    $mockCategoryGroup1,
143
                ],
144
                'saveCount' => 0,
145
                'deleteCount' => 1,
146
            ],
147
            'single group' => [
148
                'groupDefinitions' => [
149
                    'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1),
150
                    'groupHandle2' => $this->getMockCategoryGroupDefinition($mockCategoryGroup2),
151
                ],
152
                'existingGroups' => [
153
                    $mockCategoryGroup1,
154
                ],
155
                'saveCount' => 1,
156
                'deleteCount' => 0,
157
            ],
158
        ];
159
    }
160
161
    //==============================================================================================================
162
    //================================================  HELPERS  ===================================================
163
    //==============================================================================================================
164
165
    /**
166
     * @param CategoryGroup $$group
0 ignored issues
show
Bug introduced by
There is no parameter named $$group. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
167
     *
168
     * @return array
169
     */
170
    private function getMockCategoryGroupDefinition(CategoryGroup $mockCategoryGroup)
171
    {
172
        return [
173
            'class' => get_class($mockCategoryGroup),
174
            'attributes' => [
175
                'name' => $mockCategoryGroup->name,
176
                'handle' => $mockCategoryGroup->handle,
177
                'maxLevels' => 3,
178
            ],
179
            'fieldLayout' => [
180
                'fields' => [],
181
            ],
182
            'siteSettings' => [
183
                '' => [
184
                    'class' => get_class($mockCategoryGroup->getSiteSettings()[0]),
185
                    'attributes' => [
186
                        'hasUrls' => null,
187
                        'uriFormat' => null,
188
                        'template' => null,
189
                    ],
190
                ],
191
            ],
192
        ];
193
    }
194
195
    /**
196
     * @param string $groupId
197
     *
198
     * @return Mock|CategoryGroup
199
     */
200
    private function getMockCategoryGroup($groupId)
201
    {
202
        $mockGroup = $this->getMockBuilder(CategoryGroup::class)
203
                                    ->setMethods(['getFieldLayout', 'getSiteSettings'])
204
                                    ->getMock();
205
        $mockGroup->setAttributes([
206
            'id' => $groupId,
207
            'fieldLayoutId' => $groupId,
208
            'handle' => 'groupHandle'.$groupId,
209
            'name' => 'groupName'.$groupId,
210
            'maxLevels' => 3,
211
        ]);
212
213
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
214
215
        $mockGroup->expects($this->any())
216
                  ->method('getFieldLayout')
217
                  ->willReturn($mockFieldLayout);
218
219
        $mockSiteSettings = $this->getMockSiteSettings();
220
221
        $mockGroup->expects($this->any())
222
                  ->method('getSiteSettings')
223
                  ->willReturn([$mockSiteSettings]);
224
225
        return $mockGroup;
226
    }
227
228
    /**
229
     * Get mock siteSettings.
230
     *
231
     * @param string $class
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
232
     *
233
     * @return Mock|CategoryGroup_SiteSettings
234
     */
235
    private function getMockSiteSettings()
236
    {
237
        $mockSiteSettings = $this->getMockBuilder(CategoryGroup_SiteSettings::class)
238
                                 ->setMethods(['getSite'])
239
                                 ->getMock();
240
241
        $mockSiteSettings->expects($this->any())
242
          ->method('getSite')
243
          ->willReturn($this->getMockSite());
244
245
        return $mockSiteSettings;
246
    }
247
248
    private function getMockSite()
249
    {
250
        return $this->getMockBuilder(Site::class)->getMock();
251
    }
252
253
    /**
254
     * Expect a list of category groups.
255
     *
256
     * @param CategoryGroup[] $categoryGroups
257
     */
258
    private function expectList(array $categoryGroups)
259
    {
260
        Craft::$app->categories
261
                   ->expects($this->exactly(1))
262
                   ->method('getAllGroups')
263
                   ->willReturn($categoryGroups);
264
    }
265
266
    /**
267
     * Expect a number of group saves.
268
     *
269
     * @param int $saveCount
270
     */
271
    private function expectSaves(int $saveCount)
272
    {
273
        Craft::$app->categories
274
                   ->expects($this->exactly($saveCount))
275
                   ->method('saveGroup')
276
                   ->willReturn(true);
277
    }
278
279
    /**
280
     * Expect a number of group deletes.
281
     *
282
     * @param int $deleteCount
283
     */
284
    private function expectDeletes(int $deleteCount)
285
    {
286
        Craft::$app->categories
287
                    ->expects($this->exactly($deleteCount))
288
                    ->method('deleteGroupById');
289
    }
290
}
291