Completed
Pull Request — master (#114)
by Bart
01:33
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;
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 ModelProcessor();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...rvices\ModelProcessor() of type object<NerdsAndCompany\S...ervices\ModelProcessor> is incompatible with the declared type object<NerdsAndCompany\S...ervices\CategoryGroups> of property $service.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
        $actualResult = $this->service->export($groups);
57
58
        $this->assertSame($expectedResult, $actualResult);
59
    }
60
61
    /**
62
     * @dataProvider provideValidCategoryGroupDefinitions
63
     *
64
     * @param array $groupDefinitions
65
     */
66
    public function testSuccessfulImport(array $groupDefinitions, array $existingGroups, int $saveCount)
67
    {
68
        $this->expectSaves($saveCount);
69
        $this->expectDeletes(0);
70
71
        $this->service->import($groupDefinitions, $existingGroups);
72
    }
73
74
    /**
75
     * @dataProvider provideValidCategoryGroupDefinitions
76
     *
77
     * @param array $groupDefinitions
78
     */
79
    public function testImportWithForceOption(array $groupDefinitions, array $existingGroups, int $saveCount, int $deleteCount)
80
    {
81
        Schematic::$force = true;
82
        $this->expectSaves($saveCount);
83
        $this->expectDeletes($deleteCount);
84
85
        $this->service->import($groupDefinitions, $existingGroups);
86
    }
87
88
    //==============================================================================================================
89
    //==============================================  PROVIDERS  ===================================================
90
    //==============================================================================================================
91
92
    /**
93
     * @return array
94
     */
95
    public function provideValidCategoryGroups()
96
    {
97
        $mockCategoryGroup1 = $this->getMockCategoryGroup(1);
98
        $mockCategoryGroup2 = $this->getMockCategoryGroup(2);
99
100
        return [
101
            'emptyArray' => [
102
                'categoryGroups' => [],
103
                'expectedResult' => [],
104
            ],
105
            'single group' => [
106
                'categoryGroups' => [
107
                    'group1' => $mockCategoryGroup1,
108
                ],
109
                'expectedResult' => [
110
                    'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1),
0 ignored issues
show
Documentation introduced by
$mockCategoryGroup1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\CategoryGroup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
                ],
112
            ],
113
            'multiple groups' => [
114
                'categoryGroups' => [
115
                    'group1' => $mockCategoryGroup1,
116
                    'group2' => $mockCategoryGroup2,
117
                ],
118
                'expectedResult' => [
119
                    'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1),
0 ignored issues
show
Documentation introduced by
$mockCategoryGroup1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\CategoryGroup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
                    'groupHandle2' => $this->getMockCategoryGroupDefinition($mockCategoryGroup2),
0 ignored issues
show
Documentation introduced by
$mockCategoryGroup2 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\CategoryGroup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
                ],
122
            ],
123
        ];
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function provideValidCategoryGroupDefinitions()
130
    {
131
        $mockCategoryGroup1 = $this->getMockCategoryGroup(1);
132
        $mockCategoryGroup2 = $this->getMockCategoryGroup(2);
133
134
        return [
135
            'emptyArray' => [
136
                'groupDefinitions' => [],
137
                'existingGroups' => [
138
                    $mockCategoryGroup1,
139
                ],
140
                'saveCount' => 0,
141
                'deleteCount' => 1,
142
            ],
143
            'single new group' => [
144
                'groupDefinitions' => [
145
                    'groupHandle1' => $this->getMockCategoryGroupDefinition($mockCategoryGroup1),
0 ignored issues
show
Documentation introduced by
$mockCategoryGroup1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\CategoryGroup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
                    'groupHandle2' => $this->getMockCategoryGroupDefinition($mockCategoryGroup2),
0 ignored issues
show
Documentation introduced by
$mockCategoryGroup2 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\CategoryGroup>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
                ],
148
                'existingGroups' => [
149
                    $mockCategoryGroup1,
150
                ],
151
                'saveCount' => 1,
152
                'deleteCount' => 0,
153
            ],
154
        ];
155
    }
156
157
    //==============================================================================================================
158
    //================================================  HELPERS  ===================================================
159
    //==============================================================================================================
160
161
    /**
162
     * @param CategoryGroup $mockCategoryGroup
163
     *
164
     * @return array
165
     */
166
    private function getMockCategoryGroupDefinition(CategoryGroup $mockCategoryGroup)
167
    {
168
        return [
169
            'class' => get_class($mockCategoryGroup),
170
            'attributes' => [
171
                'name' => $mockCategoryGroup->name,
172
                'handle' => $mockCategoryGroup->handle,
173
                'maxLevels' => 3,
174
            ],
175
            'fieldLayout' => [
176
                'fields' => [],
177
            ],
178
            'siteSettings' => [
179
                '' => [
180
                    'class' => get_class($mockCategoryGroup->getSiteSettings()[0]),
181
                    'attributes' => [
182
                        'hasUrls' => null,
183
                        'uriFormat' => null,
184
                        'template' => null,
185
                    ],
186
                ],
187
            ],
188
        ];
189
    }
190
191
    /**
192
     * @param int $groupId
193
     *
194
     * @return Mock|CategoryGroup
195
     */
196
    private function getMockCategoryGroup(int $groupId)
197
    {
198
        $mockGroup = $this->getMockBuilder(CategoryGroup::class)
199
                                    ->setMethods(['getFieldLayout', 'getSiteSettings'])
200
                                    ->getMock();
201
        $mockGroup->setAttributes([
202
            'id' => $groupId,
203
            'fieldLayoutId' => $groupId,
204
            'handle' => 'groupHandle'.$groupId,
205
            'name' => 'groupName'.$groupId,
206
            'maxLevels' => 3,
207
        ]);
208
209
        $mockFieldLayout = $this->getMockBuilder(FieldLayout::class)->getMock();
210
211
        $mockGroup->expects($this->any())
212
                  ->method('getFieldLayout')
213
                  ->willReturn($mockFieldLayout);
214
215
        $mockSiteSettings = $this->getMockSiteSettings();
216
217
        $mockGroup->expects($this->any())
218
                  ->method('getSiteSettings')
219
                  ->willReturn([$mockSiteSettings]);
220
221
        return $mockGroup;
222
    }
223
224
    /**
225
     * Get mock siteSettings.
226
     *
227
     * @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...
228
     *
229
     * @return Mock|CategoryGroup_SiteSettings
230
     */
231
    private function getMockSiteSettings()
232
    {
233
        $mockSiteSettings = $this->getMockBuilder(CategoryGroup_SiteSettings::class)
234
                                 ->setMethods(['getSite'])
235
                                 ->getMock();
236
237
        $mockSiteSettings->expects($this->any())
238
          ->method('getSite')
239
          ->willReturn($this->getMockSite());
240
241
        return $mockSiteSettings;
242
    }
243
244
    /**
245
     * Get a mock site.
246
     *
247
     * @return Mock|Site
248
     */
249
    private function getMockSite()
250
    {
251
        return $this->getMockBuilder(Site::class)->getMock();
252
    }
253
254
    /**
255
     * Expect a number of group saves.
256
     *
257
     * @param int $saveCount
258
     */
259
    private function expectSaves(int $saveCount)
260
    {
261
        Craft::$app->categories
262
                   ->expects($this->exactly($saveCount))
263
                   ->method('saveGroup')
264
                   ->willReturn(true);
265
    }
266
267
    /**
268
     * Expect a number of group deletes.
269
     *
270
     * @param int $deleteCount
271
     */
272
    private function expectDeletes(int $deleteCount)
273
    {
274
        Craft::$app->categories
275
                    ->expects($this->exactly($deleteCount))
276
                    ->method('deleteGroupById');
277
    }
278
}
279