Completed
Pull Request — master (#91)
by Bob Olde
04:31
created

AssetSourcesTest::testImportWithForceOption()   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 1
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
                        'name' => 'assetSourceName1',
115
                        'fieldLayout' => [
116
                            'fields' => [],
117
                        ],
118
                    ],
119
                ],
120
            ],
121
            'multiple asset sources' => [
122
                'AssetSources' => [
123
                    'assetSource1' => $this->getMockAssetSource(1),
124
                    'assetSource2' => $this->getMockAssetSource(2),
125
                ],
126
                'expectedResult' => [
127
                    'assetSourceHandle1' => [
128
                        'name' => 'assetSourceName1',
129
                        'fieldLayout' => [
130
                            'fields' => [],
131
                        ],
132
                    ],
133
                    'assetSourceHandle2' => [
134
                        'name' => 'assetSourceName2',
135
                        'fieldLayout' => [
136
                            'fields' => [],
137
                        ],
138
                    ],
139
                ],
140
            ],
141
        ];
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function provideValidAssetSourceDefinitions()
148
    {
149
        return [
150
            'emptyArray' => [
151
                'assetSourceDefinitions' => [],
152
            ],
153
            'single group' => [
154
                'assetSourceDefinitions' => [
155
                    'assetSourceHandle1' => [
156
                        'name' => 'assetSourceName1',
157
                        'fieldLayout' => [
158
                            'fields' => [],
159
                        ],
160
                    ],
161
                ],
162
            ],
163
        ];
164
    }
165
166
    //==============================================================================================================
167
    //=================================================  MOCKS  ====================================================
168
    //==============================================================================================================
169
170
    /**
171
     * @param string $assetSourceId
172
     *
173
     * @return Mock|AssetSourceModel
174
     */
175
    private function getMockAssetSource($assetSourceId)
176
    {
177
        $mockAssetSource = $this->getMockBuilder(AssetSourceModel::class)
178
            ->disableOriginalConstructor()
179
            ->getMock();
180
181
        $mockAssetSource->expects($this->any())
182
            ->method('__get')
183
            ->willReturnMap([
184
                ['id', $assetSourceId],
185
                ['fieldLayoutId', $assetSourceId],
186
                ['handle', 'assetSourceHandle'.$assetSourceId],
187
                ['name', 'assetSourceName'.$assetSourceId],
188
            ]);
189
190
        $mockAssetSource->expects($this->any())
191
            ->method('getAllErrors')
192
            ->willReturn([
193
                'ohnoes' => 'horrible error',
194
            ]);
195
196
        return $mockAssetSource;
197
    }
198
199
    /**
200
     * @return Mock|CraftFieldsService
201
     */
202
    private function setMockFieldsService()
203
    {
204
        $mockFieldsService = $this->getMockBuilder(FieldsService::class)
205
            ->disableOriginalConstructor()
206
            ->getMock();
207
208
        $mockFieldsService->expects($this->any())
209
            ->method('getLayoutById')
210
            ->with($this->isType('integer'))
211
            ->willReturn($this->getMockFieldLayout());
212
213
        $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
214
215
        return $mockFieldsService;
216
    }
217
218
    /**
219
     * @return Mock|fields
220
     */
221
    private function setMockSchematicFields()
222
    {
223
        $mockSchematicFields = $this->getMockBuilder(Fields::class)
224
            ->disableOriginalConstructor()
225
            ->getMock();
226
227
        $mockSchematicFields->expects($this->any())
228
            ->method('getFieldLayoutDefinition')
229
            ->with($this->isInstanceOf(FieldLayoutModel::class))
230
            ->willReturn(['fields' => []]);
231
232
        $mockSchematicFields->expects($this->any())
233
            ->method('getFieldLayout')
234
            ->with($this->isType('array'))
235
            ->willReturn($this->getMockFieldLayout());
236
237
        $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFields);
238
239
        return $mockSchematicFields;
240
    }
241
242
    /**
243
     * @return Mock|AssetSourcesService
244
     */
245
    private function setMockAssetSourcesService()
246
    {
247
        $mockAssetSourcesService = $this->getMockBuilder(AssetSourcesService::class)
248
            ->disableOriginalConstructor()
249
            ->setMethods(['getAllSources', 'saveSource', 'deleteSourceById'])
250
            ->getMock();
251
252
        $mockAssetSourcesService->expects($this->any())
253
            ->method('getAllSources')
254
            ->with('handle')
255
            ->willReturn([]);
256
257
        $this->setComponent(Craft::app(), 'assetSources', $mockAssetSourcesService);
258
259
        return $mockAssetSourcesService;
260
    }
261
262
    /**
263
     * @return Mock|FieldLayoutModel
264
     */
265
    private function getMockFieldLayout()
266
    {
267
        $mockFieldLayout = $this->getMockBuilder(FieldLayoutModel::class)
268
            ->disableOriginalConstructor()
269
            ->getMock();
270
271
        return $mockFieldLayout;
272
    }
273
274
    /**
275
     * @return Mock|DbConnection
276
     */
277
    private function setMockDbConnection()
278
    {
279
        $mockDbConnection = $this->getMockBuilder(DbConnection::class)
280
            ->disableOriginalConstructor()
281
            ->setMethods(['createCommand'])
282
            ->getMock();
283
        $mockDbConnection->autoConnect = false; // Do not auto connect
284
285
        $mockDbCommand = $this->getMockDbCommand();
286
        $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
287
288
        Craft::app()->setComponent('db', $mockDbConnection);
289
290
        return $mockDbConnection;
291
    }
292
293
    /**
294
     * @return Mock|DbCommand
295
     */
296
    private function getMockDbCommand()
297
    {
298
        $mockDbCommand = $this->getMockBuilder(DbCommand::class)
299
            ->disableOriginalConstructor()
300
            ->setMethods(['insertOrUpdate'])
301
            ->getMock();
302
303
        return $mockDbCommand;
304
    }
305
}
306