Completed
Pull Request — master (#130)
by Bart
01:25
created

AssetsTest::getMockAssetsDefinition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 30
nc 1
nop 1
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
     * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
31
     */
32
    protected function _before()
33
    {
34
        $this->converter = new Assets();
35
    }
36
37
    //==============================================================================================================
38
    //=================================================  TESTS  ====================================================
39
    //==============================================================================================================
40
41
    /**
42
     * @dataProvider provideAssets
43
     *
44
     * @param AssetsField      $assets
45
     * @param array            $definition
46
     * @param Mock|Volume|null $mockVolume
47
     */
48
    public function testGetRecordDefinition(AssetsField $assets, array $definition, $mockVolume)
49
    {
50
        Craft::$app->volumes->expects($this->any())
51
                            ->method('getVolumeById')
52
                            ->with($mockVolume->id)
53
                            ->willReturn($mockVolume);
54
55
        $result = $this->converter->getRecordDefinition($assets);
56
57
        $this->assertSame($definition, $result);
58
    }
59
60
    /**
61
     * @dataProvider provideAssets
62
     *
63
     * @param AssetsField      $assets
64
     * @param array            $definition
65
     * @param Mock|Volume|null $mockVolume
66
     */
67
    public function testSetRecordAttributes(AssetsField $assets, array $definition, $mockVolume)
68
    {
69
        Craft::$app->volumes->expects($this->any())
70
                            ->method('getVolumeByHandle')
71
                            ->willReturn($mockVolume);
72
73
        $newAssets = new AssetsField();
74
75
        $this->converter->setRecordAttributes($newAssets, $definition, []);
76
77
        $this->assertSame($assets->name, $newAssets->name);
78
        $this->assertSame($assets->handle, $newAssets->handle);
79
        $this->assertSame($assets->defaultUploadLocationSource, $newAssets->defaultUploadLocationSource);
80
        $this->assertSame($assets->singleUploadLocationSource, $newAssets->singleUploadLocationSource);
81
        $this->assertSame($assets->sources, $newAssets->sources);
82
    }
83
84
    //==============================================================================================================
85
    //==============================================  PROVIDERS  ===================================================
86
    //==============================================================================================================
87
88
    /**
89
     * @return array
90
     */
91
    public function provideAssets()
92
    {
93
        $mockAssets1 = $this->getMockAssets(1, 1);
94
        $mockVolume1 = $this->getMockVolume(1);
95
96
        return [
97
            'assets' => [
98
                'Assets' => $mockAssets1,
99
                'definition' => $this->getMockAssetsDefinition($mockAssets1),
100
                'volume' => $mockVolume1,
101
            ],
102
        ];
103
    }
104
105
    //==============================================================================================================
106
    //================================================  HELPERS  ===================================================
107
    //==============================================================================================================
108
109
    /**
110
     * @param AssetsField $mockAssets
111
     *
112
     * @return array
113
     */
114
    private function getMockAssetsDefinition(AssetsField $mockAssets)
115
    {
116
        return [
117
            'class' => get_class($mockAssets),
118
            'attributes' => [
119
                'useSingleFolder' => null,
120
                'defaultUploadLocationSource' => 'folder:volumeHandle1',
121
                'defaultUploadLocationSubpath' => null,
122
                'singleUploadLocationSource' => 'folder:volumeHandle1',
123
                'singleUploadLocationSubpath' => null,
124
                'restrictFiles' => null,
125
                'allowedKinds' => null,
126
                'sources' => [
127
                    'folder:volumeHandle1',
128
                ],
129
                'source' => null,
130
                'viewMode' => null,
131
                'limit' => null,
132
                'selectionLabel' => null,
133
                'localizeRelations' => false,
134
                'allowMultipleSources' => true,
135
                'allowLimit' => true,
136
                'name' => 'assetsName'.$mockAssets->id,
137
                'handle' => 'assetsHandle'.$mockAssets->id,
138
                'instructions' => null,
139
                'translationMethod' => 'none',
140
                'translationKeyFormat' => null,
141
                'oldHandle' => null,
142
                'columnPrefix' => null,
143
                'required' => false,
144
                'sortOrder' => null,
145
            ],
146
            'group' => 'fieldGroup1',
147
        ];
148
    }
149
150
    /**
151
     * @param int $assetsId
152
     * @param int $groupId
153
     *
154
     * @return Mock|AssetsField
155
     */
156
    private function getMockAssets(int $assetsId, int $groupId)
157
    {
158
        $mockAssets = $this->getMockBuilder(AssetsField::class)
159
                           ->setMethods(['getGroup', 'getBlockTypes'])
160
                           ->disableOriginalConstructor()
161
                           ->getMock();
162
163
        $mockAssets->id = $assetsId;
164
        $mockAssets->groupId = $assetsId;
165
        $mockAssets->handle = 'assetsHandle'.$assetsId;
166
        $mockAssets->name = 'assetsName'.$assetsId;
167
        $mockAssets->defaultUploadLocationSource = 'folder:'.$assetsId;
168
        $mockAssets->singleUploadLocationSource = 'folder:'.$assetsId;
169
        $mockAssets->sources = ['folder:'.$assetsId];
170
171
        $mockAssets->expects($this->any())
172
                 ->method('getGroup')
173
                 ->willReturn($this->getMockFieldGroup($groupId));
174
175
        return $mockAssets;
176
    }
177
178
    /**
179
     * Get a mock Field group.
180
     *
181
     * @param int $groupId
182
     *
183
     * @return Mock|FieldGroup
184
     */
185
    private function getMockFieldGroup(int $groupId)
186
    {
187
        $mockGroup = $this->getMockBuilder(FieldGroup::class)
188
                          ->disableOriginalConstructor()
189
                          ->getmock();
190
191
        $mockGroup->id = $groupId;
192
        $mockGroup->name = 'fieldGroup'.$groupId;
193
194
        return $mockGroup;
195
    }
196
197
    /**
198
     * Get a mock volume.
199
     *
200
     * @param int $volumeId
201
     *
202
     * @return Mock|Volume
203
     */
204
    private function getMockVolume(int $volumeId)
205
    {
206
        $mockVolume = $this->getMockBuilder(Volume::class)
207
                           ->disableOriginalConstructor()
208
                           ->getmock();
209
210
        $mockVolume->id = $volumeId;
211
        $mockVolume->handle = 'volumeHandle'.$volumeId;
212
213
        return $mockVolume;
214
    }
215
}
216