Completed
Pull Request — master (#114)
by Bart
02:23
created

UserGroupTest::testDeleteRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\console\Application;
7
use craft\models\UserGroup as UserGroupModel;
8
use craft\models\Section as SectionModel;
9
use craft\models\CategoryGroup as CategoryGroupModel;
10
use craft\base\Volume as VolumeModel;
11
use Codeception\Test\Unit;
12
13
/**
14
 * Class UserGroupTest.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2017, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class UserGroupTest extends Unit
23
{
24
    /**
25
     * @var UserGroups
26
     */
27
    private $converter;
28
29
    /**
30
     * Set the converter.
31
     *
32
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
33
     */
34
    protected function _before()
35
    {
36
        $this->converter = new UserGroup();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \NerdsAndCompany\Sch...ters\Models\UserGroup() of type object<NerdsAndCompany\S...rters\Models\UserGroup> is incompatible with the declared type object<NerdsAndCompany\S...ters\Models\UserGroups> of property $converter.

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...
37
    }
38
39
    //==============================================================================================================
40
    //=================================================  TESTS  ====================================================
41
    //==============================================================================================================
42
43
    /**
44
     * @dataProvider provideUserGroups
45
     *
46
     * @param UserGroupModel $group
47
     * @param array          $definition
48
     * @param array          $groupPermissions
49
     * @param array          $allPermissions
50
     */
51
    public function testGetRecordDefinition(UserGroupModel $group, array $definition, array $groupPermissions, array $allPermissions)
52
    {
53
        Craft::$app->userPermissions->expects($this->exactly(1))
54
                                    ->method('getPermissionsByGroupId')
55
                                    ->with($group->id)
56
                                    ->willReturn($groupPermissions);
57
58
        Craft::$app->userPermissions->expects($this->exactly(1))
59
                                    ->method('getAllPermissions')
60
                                    ->willReturn($allPermissions);
61
62
        Craft::$app->sections->expects($this->exactly(1))
63
                             ->method('getSectionById')
64
                             ->with(1)
65
                             ->willReturn($this->getMockSection(1));
66
67
        Craft::$app->categories->expects($this->exactly(1))
68
                               ->method('getGroupById')
69
                               ->with(2)
70
                               ->willReturn($this->getMockCategoryGroup(2));
71
72
        Craft::$app->volumes->expects($this->exactly(1))
73
                            ->method('getVolumeById')
74
                            ->with(3)
75
                            ->willReturn($this->getmockVolume(3));
76
77
        $result = $this->converter->getRecordDefinition($group);
78
79
        $this->assertSame($definition, $result);
80
    }
81
82
    /**
83
     * @dataProvider provideUserGroups
84
     *
85
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
86
     *
87
     * @param UserGroupModel $group
88
     * @param array          $definition
89
     * @param array          $groupPermissions
90
     * @param array          $allPermissions
91
     * @param bool           $valid
92
     */
93
    public function testSaveRecord(UserGroupModel $group, array $definition, array $groupPermissions, array $allPermissions, bool $valid)
0 ignored issues
show
Unused Code introduced by
The parameter $groupPermissions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $allPermissions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        Craft::$app->userGroups->expects($this->exactly(1))
96
                               ->method('saveGroup')
97
                               ->with($group)
98
                               ->willReturn($valid);
99
100
        $mappedPermissions = ['createEntries:1', 'editCategories:2', 'performUpdates', 'viewVolume:3'];
101
102
        if ($valid) {
103
            Craft::$app->userPermissions->expects($this->exactly(1))
104
                                        ->method('saveGroupPermissions')
105
                                        ->with($group->id, $mappedPermissions)
106
                                        ->willReturn(true);
107
108
            Craft::$app->sections->expects($this->exactly(1))
109
                                 ->method('getSectionByHandle')
110
                                 ->with('section1')
111
                                 ->willReturn($this->getMockSection(1));
112
113
            Craft::$app->categories->expects($this->exactly(1))
114
                                   ->method('getGroupByHandle')
115
                                   ->with('group2')
116
                                   ->willReturn($this->getMockCategoryGroup(2));
117
118
            Craft::$app->volumes->expects($this->exactly(1))
119
                                ->method('getVolumeByHandle')
120
                                ->with('volume3')
121
                                ->willReturn($this->getmockVolume(3));
122
        }
123
124
        $result = $this->converter->saveRecord($group, $definition);
125
126
        $this->assertSame($valid, $result);
127
    }
128
129
    /**
130
     * @dataProvider provideUserGroups
131
     *
132
     * @param UserGroupModel $group
133
     */
134
    public function testDeleteRecord(UserGroupModel $group)
135
    {
136
        Craft::$app->userGroups->expects($this->exactly(1))
137
                               ->method('deleteGroupById')
138
                               ->with($group->id);
139
140
        $this->converter->deleteRecord($group);
141
    }
142
143
    //==============================================================================================================
144
    //==============================================  PROVIDERS  ===================================================
145
    //==============================================================================================================
146
147
    /**
148
     * @return array
149
     */
150
    public function provideUserGroups()
151
    {
152
        $mockUserGroup = $this->getMockUserGroup(1);
153
154
        return [
155
            'valid user group' => [
156
                'group' => $mockUserGroup,
157
                'definition' => $this->getMockUserGroupDefinition($mockUserGroup),
0 ignored issues
show
Documentation introduced by
$mockUserGroup is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\UserGroup>.

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...
158
                'groupPermissions' => [
159
                    'createentries:1',
160
                    'editcategories:2',
161
                    'performupdates',
162
                    'viewvolume:3',
163
                ],
164
                'allPermissions' => [
165
                    ['createEntries:1' => []],
166
                    ['editCategories:2' => []],
167
                    ['performUpdates' => []],
168
                    ['viewVolume:3' => []],
169
                ],
170
                'validSave' => true,
171
            ],
172
            'invalid user group' => [
173
                'group' => $mockUserGroup,
174
                'definition' => $this->getMockUserGroupDefinition($mockUserGroup),
0 ignored issues
show
Documentation introduced by
$mockUserGroup is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\models\UserGroup>.

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...
175
                'groupPermissions' => [
176
                    'createentries:1',
177
                    'editcategories:2',
178
                    'performupdates',
179
                    'viewvolume:3',
180
                ],
181
                'allPermissions' => [
182
                    ['createEntries:1' => []],
183
                    ['editCategories:2' => []],
184
                    ['performUpdates' => []],
185
                    ['viewVolume:3' => []],
186
                ],
187
                'validSave' => false,
188
            ],
189
        ];
190
    }
191
192
    //==============================================================================================================
193
    //================================================  HELPERS  ===================================================
194
    //==============================================================================================================
195
196
    /**
197
     * @param UserGroupModel $userGroup
198
     *
199
     * @return array
200
     */
201
    private function getMockUserGroupDefinition(UserGroupModel $userGroup)
202
    {
203
        return [
204
          'class' => get_class($userGroup),
205
          'attributes' => [
206
              'name' => 'userGroupName'.$userGroup->id,
207
              'handle' => 'userGroupHandle'.$userGroup->id,
208
          ],
209
          'permissions' => [
210
              'createEntries:section1',
211
              'editCategories:group2',
212
              'performUpdates',
213
              'viewVolume:volume3',
214
          ],
215
        ];
216
    }
217
218
    /**
219
     * @param int $userGroupId
220
     *
221
     * @return UserGroupModel
222
     */
223
    private function getMockUserGroup(int $userGroupId)
224
    {
225
        $mockApp = $this->getMockBuilder(Application::class)
226
                        ->disableOriginalConstructor()
227
                        ->getMock();
228
229
        $mockApp->expects($this->exactly(1))
230
                ->method('requireEdition')
231
                ->with(Craft::Pro)
232
                ->willReturn(true);
233
234
        Craft::$app = $mockApp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mockApp of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<craft\web\Applica...ft\console\Application> of property $app.

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...
235
236
        $mockUserGroup = $this->getmockBuilder(UserGroupModel::class)
237
                              ->setMethods(['__toString'])
238
                              ->getMock();
239
240
        $mockUserGroup->id = $userGroupId;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
241
        $mockUserGroup->handle = 'userGroupHandle'.$userGroupId;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
242
        $mockUserGroup->name = 'userGroupName'.$userGroupId;
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
243
244
        return $mockUserGroup;
245
    }
246
247
    /**
248
     * @param int $sectionId
249
     *
250
     * @return Mock|SectionModel
251
     */
252
    private function getMockSection(int $sectionId)
253
    {
254
        $mockSection = $this->getMockBuilder(SectionModel::class)
255
                           ->disableOriginalConstructor()
256
                           ->getMock();
257
258
        $mockSection->id = $sectionId;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
259
        $mockSection->handle = 'section'.$sectionId;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
260
261
        return $mockSection;
262
    }
263
264
    /**
265
     * @param int $groupId
266
     *
267
     * @return Mock|CategoryGroupModel
268
     */
269
    private function getMockCategoryGroup(int $groupId)
270
    {
271
        $mockGroup = $this->getMockBuilder(CategoryGroupModel::class)
272
                          ->disableOriginalConstructor()
273
                          ->getMock();
274
275
        $mockGroup->id = $groupId;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
276
        $mockGroup->handle = 'group'.$groupId;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
277
278
        return $mockGroup;
279
    }
280
281
    /**
282
     * @param int $volumeId
283
     *
284
     * @return Mock|VolumeModel
285
     */
286
    private function getMockVolume(int $volumeId)
287
    {
288
        $mockVolume = $this->getMockBuilder(VolumeModel::class)
289
                          ->disableOriginalConstructor()
290
                          ->getMock();
291
292
        $mockVolume->id = $volumeId;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
293
        $mockVolume->handle = 'volume'.$volumeId;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
294
295
        return $mockVolume;
296
    }
297
}
298