Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

AssetsTest::provideAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Fields;
4
5
use Craft;
6
use craft\fields\Assets as AssetsField;
7
use craft\base\Volume;
8
use Codeception\Test\Unit;
9
10
/**
11
 * Class AssetsTest.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2017, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class AssetsTest extends Unit
20
{
21
    /**
22
     * @var Assets
23
     */
24
    private $converter;
25
26
    /**
27
     * Set the converter.
28
     *
29
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
30
     */
31
    protected function _before()
32
    {
33
        $this->converter = new Assets();
34
    }
35
36
    //==============================================================================================================
37
    //=================================================  TESTS  ====================================================
38
    //==============================================================================================================
39
40
    /**
41
     * @dataProvider provideAssets
42
     *
43
     * @param AssetsField      $assets
44
     * @param array            $definition
45
     * @param Mock|Volume|null $mockVolume
46
     */
47
    public function testGetRecordDefinition(AssetsField $assets, array $definition, $mockVolume)
48
    {
49
        Craft::$app->volumes->expects($this->any())
50
                            ->method('getVolumeById')
51
                            ->with($mockVolume->id)
52
                            ->willReturn($mockVolume);
53
54
        $result = $this->converter->getRecordDefinition($assets);
55
56
        $this->assertSame($definition, $result);
57
    }
58
59
    /**
60
     * @dataProvider provideAssets
61
     *
62
     * @param AssetsField      $assets
63
     * @param array            $definition
64
     * @param Mock|Volume|null $mockVolume
65
     */
66
    public function testSetRecordAttributes(AssetsField $assets, array $definition, $mockVolume)
67
    {
68
        Craft::$app->volumes->expects($this->any())
69
                            ->method('getVolumeByHandle')
70
                            ->willReturn($mockVolume);
71
72
        $newAssets = new AssetsField();
73
74
        $this->converter->setRecordAttributes($newAssets, $definition, []);
75
76
        $this->assertSame($assets->name, $newAssets->name);
77
        $this->assertSame($assets->handle, $newAssets->handle);
78
        $this->assertSame($assets->defaultUploadLocationSource, $newAssets->defaultUploadLocationSource);
79
        $this->assertSame($assets->singleUploadLocationSource, $newAssets->singleUploadLocationSource);
80
        $this->assertSame($assets->sources, $newAssets->sources);
81
    }
82
83
    //==============================================================================================================
84
    //==============================================  PROVIDERS  ===================================================
85
    //==============================================================================================================
86
87
    /**
88
     * @return array
89
     */
90
    public function provideAssets()
91
    {
92
        $mockAssets1 = $this->getMockAssets(1, 1);
93
        $mockVolume1 = $this->getMockVolume(1);
94
95
        return [
96
            'assets' => [
97
                'Assets' => $mockAssets1,
98
                'definition' => $this->getMockAssetsDefinition($mockAssets1),
0 ignored issues
show
Documentation introduced by
$mockAssets1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<craft\fields\Assets>.

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...
99
                'volume' => $mockVolume1,
100
            ],
101
        ];
102
    }
103
104
    //==============================================================================================================
105
    //================================================  HELPERS  ===================================================
106
    //==============================================================================================================
107
108
    /**
109
     * @param AssetsField $mockAssets
110
     *
111
     * @return array
112
     */
113
    private function getMockAssetsDefinition(AssetsField $mockAssets)
114
    {
115
        return [
116
            'class' => get_class($mockAssets),
117
            'attributes' => [
118
                'useSingleFolder' => null,
119
                'defaultUploadLocationSource' => 'folder:volumeHandle1',
120
                'defaultUploadLocationSubpath' => null,
121
                'singleUploadLocationSource' => 'folder:volumeHandle1',
122
                'singleUploadLocationSubpath' => null,
123
                'restrictFiles' => null,
124
                'allowedKinds' => null,
125
                'sources' => [
126
                    'folder:volumeHandle1',
127
                ],
128
                'source' => null,
129
                'viewMode' => null,
130
                'limit' => null,
131
                'selectionLabel' => null,
132
                'localizeRelations' => false,
133
                'allowMultipleSources' => true,
134
                'allowLimit' => true,
135
                'name' => 'assetsName'.$mockAssets->id,
136
                'handle' => 'assetsHandle'.$mockAssets->id,
137
                'instructions' => null,
138
                'translationMethod' => 'none',
139
                'translationKeyFormat' => null,
140
                'oldHandle' => null,
141
                'columnPrefix' => null,
142
                'required' => false,
143
                'sortOrder' => null,
144
            ],
145
            'group' => 'fieldGroup1',
146
        ];
147
    }
148
149
    /**
150
     * @param int $assetsId
151
     * @param int $groupId
152
     *
153
     * @return Mock|AssetsField
154
     */
155
    private function getMockAssets(int $assetsId, int $groupId)
156
    {
157
        $mockAssets = $this->getMockBuilder(AssetsField::class)
158
                           ->setMethods(['getGroup', 'getBlockTypes'])
159
                           ->disableOriginalConstructor()
160
                           ->getMock();
161
162
        $mockAssets->id = $assetsId;
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...
163
        $mockAssets->groupId = $assetsId;
0 ignored issues
show
Bug introduced by
Accessing groupId 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...
164
        $mockAssets->handle = 'assetsHandle'.$assetsId;
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...
165
        $mockAssets->name = 'assetsName'.$assetsId;
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...
166
        $mockAssets->defaultUploadLocationSource = 'folder:'.$assetsId;
0 ignored issues
show
Bug introduced by
Accessing defaultUploadLocationSource 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...
167
        $mockAssets->singleUploadLocationSource = 'folder:'.$assetsId;
0 ignored issues
show
Bug introduced by
Accessing singleUploadLocationSource 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...
168
        $mockAssets->sources = ['folder:'.$assetsId];
0 ignored issues
show
Bug introduced by
Accessing sources 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...
169
170
        $mockAssets->expects($this->any())
171
                 ->method('getGroup')
172
                 ->willReturn($this->getMockFieldGroup($groupId));
173
174
        return $mockAssets;
175
    }
176
177
    /**
178
     * Get a mock Field group.
179
     *
180
     * @param int $groupId
181
     *
182
     * @return Mock|FieldGroup
183
     */
184
    private function getMockFieldGroup(int $groupId)
185
    {
186
        $mockGroup = $this->getMockBuilder(FieldGroup::class)
187
                          ->disableOriginalConstructor()
188
                          ->getmock();
189
190
        $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...
191
        $mockGroup->name = 'fieldGroup'.$groupId;
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...
192
193
        return $mockGroup;
194
    }
195
196
    /**
197
     * Get a mock volume.
198
     *
199
     * @param int $volumeId
200
     *
201
     * @return Mock|Volume
202
     */
203
    private function getMockVolume(int $volumeId)
204
    {
205
        $mockVolume = $this->getMockBuilder(Volume::class)
206
                           ->disableOriginalConstructor()
207
                           ->getmock();
208
209
        $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...
210
        $mockVolume->handle = 'volumeHandle'.$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...
211
212
        return $mockVolume;
213
    }
214
}
215