1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
4
|
|
|
|
5
|
|
|
use Craft\BaseTest; |
6
|
|
|
use Craft\AssetTransformsService; |
7
|
|
|
use Craft\AssetTransformModel; |
8
|
|
|
use Craft\Craft; |
9
|
|
|
use Craft\DbCommand; |
10
|
|
|
use Craft\DbConnection; |
11
|
|
|
use NerdsAndCompany\Schematic\Models\Result; |
12
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AssetTransformsTest. |
16
|
|
|
* |
17
|
|
|
* @author Nerds & Company |
18
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
19
|
|
|
* @license MIT |
20
|
|
|
* |
21
|
|
|
* @see http://www.nerds.company |
22
|
|
|
* |
23
|
|
|
* @coversDefaultClass \NerdsAndCompany\Schematic\Services\AssetTransforms |
24
|
|
|
* @covers ::__construct |
25
|
|
|
* @covers ::<!public> |
26
|
|
|
*/ |
27
|
|
|
class AssetTransformsTest extends BaseTest |
28
|
|
|
{ |
29
|
|
|
//============================================================================================================== |
30
|
|
|
//================================================= TESTS ==================================================== |
31
|
|
|
//============================================================================================================== |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers ::export |
35
|
|
|
* @dataProvider provideValidAssetTransforms |
36
|
|
|
* |
37
|
|
|
* @param AssetTransformModel[] $assetTransforms |
38
|
|
|
* @param array $expectedResult |
39
|
|
|
*/ |
40
|
|
|
public function testSuccessfulExport(array $assetTransforms, array $expectedResult = []) |
41
|
|
|
{ |
42
|
|
|
$schematicAssetTransformsService = new AssetTransforms(); |
43
|
|
|
|
44
|
|
|
$actualResult = $schematicAssetTransformsService->export($assetTransforms); |
45
|
|
|
|
46
|
|
|
$this->assertSame($expectedResult, $actualResult); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers ::import |
51
|
|
|
* @dataProvider provideValidAssetTransformDefinitions |
52
|
|
|
* |
53
|
|
|
* @param array $assetTransformDefinitions |
54
|
|
|
*/ |
55
|
|
|
public function testSuccessfulImport(array $assetTransformDefinitions) |
56
|
|
|
{ |
57
|
|
|
$this->setMockAssetTransformsService(); |
58
|
|
|
$this->setMockDbConnection(); |
59
|
|
|
|
60
|
|
|
$schematicAssetTransformsService = new AssetTransforms(); |
61
|
|
|
|
62
|
|
|
$import = $schematicAssetTransformsService->import($assetTransformDefinitions); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(Result::class, $import); |
65
|
|
|
$this->assertFalse($import->hasErrors()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @covers ::import |
70
|
|
|
* @dataProvider provideValidAssetTransformDefinitions |
71
|
|
|
* |
72
|
|
|
* @param array $assetTransformDefinitions |
73
|
|
|
*/ |
74
|
|
|
public function testImportWithForceOption(array $assetTransformDefinitions) |
75
|
|
|
{ |
76
|
|
|
$this->setMockAssetTransformsService(); |
77
|
|
|
$this->setMockDbConnection(); |
78
|
|
|
|
79
|
|
|
$schematicAssetTransformsService = new AssetTransforms(); |
80
|
|
|
|
81
|
|
|
$import = $schematicAssetTransformsService->import($assetTransformDefinitions, true); |
82
|
|
|
|
83
|
|
|
$this->assertInstanceOf(Result::class, $import); |
84
|
|
|
$this->assertFalse($import->hasErrors()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//============================================================================================================== |
88
|
|
|
//============================================== PROVIDERS =================================================== |
89
|
|
|
//============================================================================================================== |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function provideValidAssetTransforms() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
'emptyArray' => [ |
98
|
|
|
'AssetTransforms' => [], |
99
|
|
|
'expectedResult' => [], |
100
|
|
|
], |
101
|
|
|
'single asset source' => [ |
102
|
|
|
'AssetTransforms' => [ |
103
|
|
|
'assetTransform1' => $this->getMockAssetTransform(1), |
104
|
|
|
], |
105
|
|
|
'expectedResult' => [ |
106
|
|
|
'assetTransformHandle1' => [ |
107
|
|
|
'name' => 'assetTransformName1', |
108
|
|
|
'width' => null, |
109
|
|
|
'height' => null, |
110
|
|
|
'format' => null, |
111
|
|
|
'dimensionChangeTime' => null, |
112
|
|
|
'mode' => null, |
113
|
|
|
'position' => null, |
114
|
|
|
'quality' => null, |
115
|
|
|
], |
116
|
|
|
], |
117
|
|
|
], |
118
|
|
|
'multiple asset sources' => [ |
119
|
|
|
'AssetTransforms' => [ |
120
|
|
|
'assetTransform1' => $this->getMockAssetTransform(1), |
121
|
|
|
'assetTransform2' => $this->getMockAssetTransform(2), |
122
|
|
|
], |
123
|
|
|
'expectedResult' => [ |
124
|
|
|
'assetTransformHandle1' => [ |
125
|
|
|
'name' => 'assetTransformName1', |
126
|
|
|
'width' => null, |
127
|
|
|
'height' => null, |
128
|
|
|
'format' => null, |
129
|
|
|
'dimensionChangeTime' => null, |
130
|
|
|
'mode' => null, |
131
|
|
|
'position' => null, |
132
|
|
|
'quality' => null, |
133
|
|
|
], |
134
|
|
|
'assetTransformHandle2' => [ |
135
|
|
|
'name' => 'assetTransformName2', |
136
|
|
|
'width' => null, |
137
|
|
|
'height' => null, |
138
|
|
|
'format' => null, |
139
|
|
|
'dimensionChangeTime' => null, |
140
|
|
|
'mode' => null, |
141
|
|
|
'position' => null, |
142
|
|
|
'quality' => null, |
143
|
|
|
], |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function provideValidAssetTransformDefinitions() |
153
|
|
|
{ |
154
|
|
|
return [ |
155
|
|
|
'emptyArray' => [ |
156
|
|
|
'assetTransformDefinitions' => [], |
157
|
|
|
], |
158
|
|
|
'single group' => [ |
159
|
|
|
'assetTransformDefinitions' => [ |
160
|
|
|
'assetTransformHandle1' => [ |
161
|
|
|
'name' => 'assetTransformName1', |
162
|
|
|
'width' => 100, |
163
|
|
|
'height' => 100, |
164
|
|
|
'format' => 'jpg', |
165
|
|
|
'dimensionChangeTime' => null, |
166
|
|
|
'mode' => 'crop', |
167
|
|
|
'position' => 'center-center', |
168
|
|
|
'quality' => 75, |
169
|
|
|
], |
170
|
|
|
], |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
//============================================================================================================== |
176
|
|
|
//================================================= MOCKS ==================================================== |
177
|
|
|
//============================================================================================================== |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $assetTransformId |
181
|
|
|
* |
182
|
|
|
* @return Mock|AssetTransformModel |
183
|
|
|
*/ |
184
|
|
|
private function getMockAssetTransform($assetTransformId) |
185
|
|
|
{ |
186
|
|
|
$mockAssetTransform = $this->getMockBuilder(AssetTransformModel::class) |
187
|
|
|
->disableOriginalConstructor() |
188
|
|
|
->getMock(); |
189
|
|
|
|
190
|
|
|
$mockAssetTransform->expects($this->any()) |
191
|
|
|
->method('__get') |
192
|
|
|
->willReturnMap([ |
193
|
|
|
['id', $assetTransformId], |
194
|
|
|
['handle', 'assetTransformHandle'.$assetTransformId], |
195
|
|
|
['name', 'assetTransformName'.$assetTransformId], |
196
|
|
|
]); |
197
|
|
|
|
198
|
|
|
$mockAssetTransform->expects($this->any()) |
199
|
|
|
->method('getAllErrors') |
200
|
|
|
->willReturn([ |
201
|
|
|
'ohnoes' => 'horrible error', |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
return $mockAssetTransform; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return Mock|AssetTransformsService |
209
|
|
|
*/ |
210
|
|
|
private function setMockAssetTransformsService() |
211
|
|
|
{ |
212
|
|
|
$mockAssetTransformsService = $this->getMockBuilder(AssetTransformsService::class) |
213
|
|
|
->disableOriginalConstructor() |
214
|
|
|
->setMethods(['getAllTransforms', 'saveTransform', 'deleteTransform']) |
215
|
|
|
->getMock(); |
216
|
|
|
|
217
|
|
|
$mockAssetTransformsService->expects($this->any()) |
218
|
|
|
->method('getAllTransforms') |
219
|
|
|
->with('handle') |
220
|
|
|
->willReturn([]); |
221
|
|
|
|
222
|
|
|
$this->setComponent(Craft::app(), 'assetTransforms', $mockAssetTransformsService); |
223
|
|
|
|
224
|
|
|
return $mockAssetTransformsService; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return Mock|DbConnection |
229
|
|
|
*/ |
230
|
|
|
private function setMockDbConnection() |
231
|
|
|
{ |
232
|
|
|
$mockDbConnection = $this->getMockBuilder(DbConnection::class) |
233
|
|
|
->disableOriginalConstructor() |
234
|
|
|
->setMethods(['createCommand']) |
235
|
|
|
->getMock(); |
236
|
|
|
$mockDbConnection->autoConnect = false; // Do not auto connect |
237
|
|
|
|
238
|
|
|
$mockDbCommand = $this->getMockDbCommand(); |
239
|
|
|
$mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand); |
240
|
|
|
|
241
|
|
|
Craft::app()->setComponent('db', $mockDbConnection); |
242
|
|
|
|
243
|
|
|
return $mockDbConnection; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return Mock|DbCommand |
248
|
|
|
*/ |
249
|
|
|
private function getMockDbCommand() |
250
|
|
|
{ |
251
|
|
|
$mockDbCommand = $this->getMockBuilder(DbCommand::class) |
252
|
|
|
->disableOriginalConstructor() |
253
|
|
|
->setMethods(['insertOrUpdate']) |
254
|
|
|
->getMock(); |
255
|
|
|
|
256
|
|
|
return $mockDbCommand; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|