Completed
Pull Request — master (#91)
by Bob Olde
03:55
created

AssetSourcesTest::provideValidAssetSources()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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