Completed
Push — master ( 7f4e3a...f96930 )
by Bob Olde
04:50
created

AssetSourcesTest::provideValidAssetSources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 1
eloc 35
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\BaseTest;
6
use Craft\AssetSourcesService;
7
use Craft\AssetSourceModel;
8
use Craft\Craft;
9
use Craft\DbCommand;
10
use Craft\DbConnection;
11
use Craft\FieldLayoutModel;
12
use Craft\FieldsService;
13
use NerdsAndCompany\Schematic\Models\Result;
14
use PHPUnit_Framework_MockObject_MockObject as Mock;
15
16
/**
17
 * Class AssetSourcesTest.
18
 *
19
 * @author    Nerds & Company
20
 * @copyright Copyright (c) 2015-2017, Nerds & Company
21
 * @license   MIT
22
 *
23
 * @see      http://www.nerds.company
24
 *
25
 * @coversDefaultClass \NerdsAndCompany\Schematic\Services\AssetSources
26
 * @covers ::__construct
27
 * @covers ::<!public>
28
 */
29
class AssetSourcesTest extends BaseTest
30
{
31
    //==============================================================================================================
32
    //=================================================  TESTS  ====================================================
33
    //==============================================================================================================
34
35
    /**
36
     * @covers ::export
37
     * @dataProvider provideValidAssetSources
38
     *
39
     * @param AssetSourceModel[] $assetSources
40
     * @param array              $expectedResult
41
     */
42
    public function testSuccessfulExport(array $assetSources, array $expectedResult = [])
43
    {
44
        $this->setMockFieldsService();
45
        $this->setMockSchematicFields();
46
47
        $schematicAssetSourcesService = new AssetSources();
48
49
        $actualResult = $schematicAssetSourcesService->export($assetSources);
50
51
        $this->assertSame($expectedResult, $actualResult);
52
    }
53
54
    /**
55
     * @covers ::import
56
     * @dataProvider provideValidAssetSourceDefinitions
57
     *
58
     * @param array $assetSourceDefinitions
59
     */
60
    public function testSuccessfulImport(array $assetSourceDefinitions)
61
    {
62
        $this->setMockAssetSourcesService();
63
        $this->setMockDbConnection();
64
        $this->setMockSchematicFields();
65
66
        $schematicAssetSourcesService = new AssetSources();
67
68
        $import = $schematicAssetSourcesService->import($assetSourceDefinitions);
69
70
        $this->assertInstanceOf(Result::class, $import);
71
        $this->assertFalse($import->hasErrors());
72
    }
73
74
      /**
75
       * @covers ::import
76
       * @dataProvider provideValidAssetSourceDefinitions
77
       *
78
       * @param array $assetSourceDefinitions
79
       */
80
      public function testImportWithForceOption(array $assetSourceDefinitions)
81
      {
82
          $this->setMockAssetSourcesService();
83
          $this->setMockDbConnection();
84
          $this->setMockSchematicFields();
85
86
          $schematicAssetSourcesService = new AssetSources();
87
88
          $import = $schematicAssetSourcesService->import($assetSourceDefinitions, true);
89
90
          $this->assertInstanceOf(Result::class, $import);
91
          $this->assertFalse($import->hasErrors());
92
      }
93
94
    //==============================================================================================================
95
    //==============================================  PROVIDERS  ===================================================
96
    //==============================================================================================================
97
98
    /**
99
     * @return array
100
     */
101
    public function provideValidAssetSources()
102
    {
103
        return [
104
            'emptyArray' => [
105
                'AssetSources' => [],
106
                'expectedResult' => [],
107
            ],
108
            'single asset source' => [
109
                'AssetSources' => [
110
                    'assetSource1' => $this->getMockAssetSource(1),
111
                ],
112
                'expectedResult' => [
113
                    'assetSourceHandle1' => [
114
                        'type' => null,
115
                        'name' => 'assetSourceName1',
116
                        'sortOrder' => null,
117
                        'settings' => null,
118
                        'fieldLayout' => [
119
                            'fields' => [],
120
                        ],
121
                    ],
122
                ],
123
            ],
124
            'multiple asset sources' => [
125
                'AssetSources' => [
126
                    'assetSource1' => $this->getMockAssetSource(1),
127
                    'assetSource2' => $this->getMockAssetSource(2),
128
                ],
129
                'expectedResult' => [
130
                    'assetSourceHandle1' => [
131
                        'type' => null,
132
                        'name' => 'assetSourceName1',
133
                        'sortOrder' => null,
134
                        'settings' => null,
135
                        'fieldLayout' => [
136
                            'fields' => [],
137
                        ],
138
                    ],
139
                    'assetSourceHandle2' => [
140
                        'type' => null,
141
                        'name' => 'assetSourceName2',
142
                        'sortOrder' => null,
143
                        'settings' => null,
144
                        'fieldLayout' => [
145
                            'fields' => [],
146
                        ],
147
                    ],
148
                ],
149
            ],
150
        ];
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function provideValidAssetSourceDefinitions()
157
    {
158
        return [
159
            'emptyArray' => [
160
                'assetSourceDefinitions' => [],
161
            ],
162
            'single group' => [
163
                'assetSourceDefinitions' => [
164
                    'assetSourceHandle1' => [
165
                        'type' => 'Local',
166
                        'name' => 'assetSourceName1',
167
                        'sortOrder' => 1,
168
                        'settings' => array(),
169
                        'fieldLayout' => [
170
                            'fields' => [],
171
                        ],
172
                    ],
173
                ],
174
            ],
175
        ];
176
    }
177
178
    //==============================================================================================================
179
    //=================================================  MOCKS  ====================================================
180
    //==============================================================================================================
181
182
    /**
183
     * @param string $assetSourceId
184
     *
185
     * @return Mock|AssetSourceModel
186
     */
187
    private function getMockAssetSource($assetSourceId)
188
    {
189
        $mockAssetSource = $this->getMockBuilder(AssetSourceModel::class)
190
            ->disableOriginalConstructor()
191
            ->getMock();
192
193
        $mockAssetSource->expects($this->any())
194
            ->method('__get')
195
            ->willReturnMap([
196
                ['id', $assetSourceId],
197
                ['fieldLayoutId', $assetSourceId],
198
                ['handle', 'assetSourceHandle'.$assetSourceId],
199
                ['name', 'assetSourceName'.$assetSourceId],
200
            ]);
201
202
        $mockAssetSource->expects($this->any())
203
            ->method('getAllErrors')
204
            ->willReturn([
205
                'ohnoes' => 'horrible error',
206
            ]);
207
208
        return $mockAssetSource;
209
    }
210
211
    /**
212
     * @return Mock|CraftFieldsService
213
     */
214
    private function setMockFieldsService()
215
    {
216
        $mockFieldsService = $this->getMockBuilder(FieldsService::class)
217
            ->disableOriginalConstructor()
218
            ->getMock();
219
220
        $mockFieldsService->expects($this->any())
221
            ->method('getLayoutById')
222
            ->with($this->isType('integer'))
223
            ->willReturn($this->getMockFieldLayout());
224
225
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
226
227
        return $mockFieldsService;
228
    }
229
230
    /**
231
     * @return Mock|fields
232
     */
233
    private function setMockSchematicFields()
234
    {
235
        $mockSchematicFields = $this->getMockBuilder(Fields::class)
236
            ->disableOriginalConstructor()
237
            ->getMock();
238
239
        $mockSchematicFields->expects($this->any())
240
            ->method('getFieldLayoutDefinition')
241
            ->with($this->isInstanceOf(FieldLayoutModel::class))
242
            ->willReturn(['fields' => []]);
243
244
        $mockSchematicFields->expects($this->any())
245
            ->method('getFieldLayout')
246
            ->with($this->isType('array'))
247
            ->willReturn($this->getMockFieldLayout());
248
249
        $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFields);
250
251
        return $mockSchematicFields;
252
    }
253
254
    /**
255
     * @return Mock|AssetSourcesService
256
     */
257
    private function setMockAssetSourcesService()
258
    {
259
        $mockAssetSourcesService = $this->getMockBuilder(AssetSourcesService::class)
260
            ->disableOriginalConstructor()
261
            ->setMethods(['getAllSources', 'saveSource', 'deleteSourceById'])
262
            ->getMock();
263
264
        $mockAssetSourcesService->expects($this->any())
265
            ->method('getAllSources')
266
            ->with('handle')
267
            ->willReturn([]);
268
269
        $this->setComponent(Craft::app(), 'assetSources', $mockAssetSourcesService);
270
271
        return $mockAssetSourcesService;
272
    }
273
274
    /**
275
     * @return Mock|FieldLayoutModel
276
     */
277
    private function getMockFieldLayout()
278
    {
279
        $mockFieldLayout = $this->getMockBuilder(FieldLayoutModel::class)
280
            ->disableOriginalConstructor()
281
            ->getMock();
282
283
        return $mockFieldLayout;
284
    }
285
286
    /**
287
     * @return Mock|DbConnection
288
     */
289
    private function setMockDbConnection()
290
    {
291
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
292
            ->disableOriginalConstructor()
293
            ->setMethods(['createCommand'])
294
            ->getMock();
295
        $mockDbConnection->autoConnect = false; // Do not auto connect
296
297
        $mockDbCommand = $this->getMockDbCommand();
298
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
299
300
        Craft::app()->setComponent('db', $mockDbConnection);
301
302
        return $mockDbConnection;
303
    }
304
305
    /**
306
     * @return Mock|DbCommand
307
     */
308
    private function getMockDbCommand()
309
    {
310
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
311
            ->disableOriginalConstructor()
312
            ->setMethods(['insertOrUpdate'])
313
            ->getMock();
314
315
        return $mockDbCommand;
316
    }
317
}
318