|
3 | 3 | use Code16\Sharp\Form\Eloquent\Uploads\SharpUploadModel; |
4 | 4 | use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer; |
5 | 5 | use Code16\Sharp\Tests\Unit\Form\Eloquent\Uploads\Transformers\Fakes\FakePicturable; |
| 6 | +use Illuminate\Http\UploadedFile; |
| 7 | +use Illuminate\Support\Carbon; |
6 | 8 | use Illuminate\Support\Facades\Storage; |
7 | 9 |
|
8 | 10 | beforeEach(function () { |
|
35 | 37 | 'disk' => 'local', |
36 | 38 | 'size' => $upload->size, |
37 | 39 | 'thumbnail' => $upload->thumbnail(200, 200), |
| 40 | + 'playable_preview_url' => null, |
38 | 41 | 'mime_type' => 'image/png', |
39 | 42 | ], |
40 | 43 | $transformer->apply('', $picturable, 'picture'), |
|
73 | 76 | 'size' => $upload->size, |
74 | 77 | 'mime_type' => 'image/png', |
75 | 78 | 'thumbnail' => $upload->thumbnail(200, 200), |
| 79 | + 'playable_preview_url' => null, |
76 | 80 | 'filters' => [ |
77 | 81 | 'crop' => [ |
78 | 82 | 'height' => .5, |
|
116 | 120 | 'disk' => 'local', |
117 | 121 | 'size' => $upload1->size, |
118 | 122 | 'thumbnail' => $upload1->thumbnail(200, 200), |
| 123 | + 'playable_preview_url' => null, |
119 | 124 | 'mime_type' => 'image/png', |
120 | 125 | ], |
121 | 126 | 'id' => $upload1->id, |
|
126 | 131 | 'disk' => 'local', |
127 | 132 | 'size' => $upload2->size, |
128 | 133 | 'thumbnail' => $upload2->thumbnail(200, 200), |
| 134 | + 'playable_preview_url' => null, |
129 | 135 | 'mime_type' => 'image/png', |
130 | 136 | ], |
131 | 137 | 'id' => $upload2->id, |
|
175 | 181 | 'disk' => 'local', |
176 | 182 | 'size' => $upload1->size, |
177 | 183 | 'thumbnail' => $upload1->thumbnail(200, 200), |
| 184 | + 'playable_preview_url' => null, |
178 | 185 | 'filters' => $filters, |
179 | 186 | 'mime_type' => 'image/png', |
180 | 187 | ], |
|
186 | 193 | 'disk' => 'local', |
187 | 194 | 'size' => $upload2->size, |
188 | 195 | 'thumbnail' => $upload2->thumbnail(200, 200), |
| 196 | + 'playable_preview_url' => null, |
189 | 197 | 'mime_type' => 'image/png', |
190 | 198 | ], |
191 | 199 | 'id' => $upload2->id, |
192 | 200 | ], |
193 | 201 | ]); |
194 | 202 | }); |
195 | 203 |
|
| 204 | +it('transforms an upload with playable preview', function () { |
| 205 | + $this->freezeTime(function (Carbon $time) { |
| 206 | + $upload = new SharpUploadModel([ |
| 207 | + 'file_name' => UploadedFile::fake()->create('video.mp4', 120, 'video/mp4'), |
| 208 | + 'size' => 120, |
| 209 | + 'mime_type' => 'video/mp4', |
| 210 | + 'disk' => 'local', |
| 211 | + ]); |
| 212 | + $picturable = new FakePicturable(['id' => 1]); |
| 213 | + $picturable->setRelation('video', $upload); |
| 214 | + |
| 215 | + $transformer = new SharpUploadModelFormAttributeTransformer(withPlayablePreview: true); |
| 216 | + |
| 217 | + expect($transformer->apply('', $picturable, 'video')) |
| 218 | + ->toEqual([ |
| 219 | + 'id' => $upload->id, |
| 220 | + 'name' => basename($upload->file_name), |
| 221 | + 'path' => $upload->file_name, |
| 222 | + 'disk' => 'local', |
| 223 | + 'size' => $upload->size, |
| 224 | + 'thumbnail' => null, |
| 225 | + 'playable_preview_url' => $upload->file_name.'?expiration='.$time->addMinutes(30)->timestamp, |
| 226 | + 'mime_type' => 'video/mp4', |
| 227 | + ]); |
| 228 | + }); |
| 229 | +}); |
| 230 | + |
| 231 | +it('transforms a list of upload with playable preview', function () { |
| 232 | + $this->freezeTime(function (Carbon $time) { |
| 233 | + $upload1 = new SharpUploadModel([ |
| 234 | + 'file_name' => UploadedFile::fake()->create('audio.mp3', 120, 'audio/mp3'), |
| 235 | + 'size' => 120, |
| 236 | + 'mime_type' => 'audio/mp3', |
| 237 | + 'disk' => 'local', |
| 238 | + ]); |
| 239 | + $picturable = new FakePicturable(['id' => 1]); |
| 240 | + $picturable->setRelation('songs', collect([$upload1])); |
| 241 | + |
| 242 | + $transformer = new SharpUploadModelFormAttributeTransformer(withPlayablePreview: true); |
| 243 | + |
| 244 | + expect($transformer->apply('', $picturable, 'songs'))->toEqual([ |
| 245 | + [ |
| 246 | + 'file' => [ |
| 247 | + 'name' => basename($upload1->file_name), |
| 248 | + 'path' => $upload1->file_name, |
| 249 | + 'disk' => 'local', |
| 250 | + 'size' => $upload1->size, |
| 251 | + 'thumbnail' => null, |
| 252 | + 'playable_preview_url' => $upload1->file_name.'?expiration='.$time->addMinutes(30)->timestamp, |
| 253 | + 'mime_type' => 'audio/mp3', |
| 254 | + ], |
| 255 | + 'id' => $upload1->id, |
| 256 | + ], |
| 257 | + ]); |
| 258 | + }); |
| 259 | +}); |
| 260 | + |
196 | 261 | describe('dynamicInstance', function () { |
197 | 262 | it('allows to fake a sharpUpload and transform a single upload', function () { |
198 | 263 | $file = createImage(); |
|
215 | 280 | 'disk' => 'local', |
216 | 281 | 'size' => 120, |
217 | 282 | 'thumbnail' => (new SharpUploadModel($uploadData))->thumbnail(200, 200), |
| 283 | + 'playable_preview_url' => null, |
218 | 284 | 'filters' => [], |
219 | 285 | 'mime_type' => 'image/png', |
220 | 286 | ], |
|
0 commit comments