Skip to content

Commit 4c787b5

Browse files
committed
Add tests
1 parent a44fcad commit 4c787b5

6 files changed

Lines changed: 92 additions & 2 deletions

File tree

demo/app/Sharp/Posts/PostForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function buildFormFields(FieldsContainer $formFields): void
6767
SharpFormEditorUpload::make()
6868
->setStorageDisk('local')
6969
->setStorageBasePath('data/posts/{id}/embed')
70-
->setMaxFileSize(1)
70+
->setMaxFileSize(2)
7171
->setHasLegend()
7272
)
7373
->setMaxLength(2000)

src/Form/Eloquent/Uploads/Transformers/SharpUploadModelFormAttributeTransformer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,16 @@ public function apply($value, $instance = null, $attribute = null)
7171
return $instance->$attribute
7272
->map(function ($upload) {
7373
$array = $this->transformUpload($upload);
74-
$fileAttrs = ['name', 'path', 'disk', 'thumbnail', 'size', 'filters', 'mime_type'];
74+
$fileAttrs = [
75+
'name',
76+
'path',
77+
'disk',
78+
'thumbnail',
79+
'playable_preview_url',
80+
'size',
81+
'filters',
82+
'mime_type',
83+
];
7584

7685
return array_merge(
7786
['file' => Arr::only($array, $fileAttrs) ?: null],

tests/Unit/Form/Eloquent/Uploads/Transformers/Fakes/FakePicturable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ public function picture(): MorphOne
1616
return $this->morphOne(SharpUploadModel::class, 'model');
1717
}
1818

19+
public function video(): MorphOne
20+
{
21+
return $this->morphOne(SharpUploadModel::class, 'model');
22+
}
23+
1924
public function pictures(): MorphMany
2025
{
2126
return $this->morphMany(SharpUploadModel::class, 'model');
2227
}
28+
29+
public function songs(): MorphMany
30+
{
31+
return $this->morphMany(SharpUploadModel::class, 'model');
32+
}
2333
}

tests/Unit/Form/Eloquent/Uploads/Transformers/SharpUploadModelFormAttributeTransformerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use Code16\Sharp\Form\Eloquent\Uploads\SharpUploadModel;
44
use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
55
use Code16\Sharp\Tests\Unit\Form\Eloquent\Uploads\Transformers\Fakes\FakePicturable;
6+
use Illuminate\Http\UploadedFile;
7+
use Illuminate\Support\Carbon;
68
use Illuminate\Support\Facades\Storage;
79

810
beforeEach(function () {
@@ -35,6 +37,7 @@
3537
'disk' => 'local',
3638
'size' => $upload->size,
3739
'thumbnail' => $upload->thumbnail(200, 200),
40+
'playable_preview_url' => null,
3841
'mime_type' => 'image/png',
3942
],
4043
$transformer->apply('', $picturable, 'picture'),
@@ -73,6 +76,7 @@
7376
'size' => $upload->size,
7477
'mime_type' => 'image/png',
7578
'thumbnail' => $upload->thumbnail(200, 200),
79+
'playable_preview_url' => null,
7680
'filters' => [
7781
'crop' => [
7882
'height' => .5,
@@ -116,6 +120,7 @@
116120
'disk' => 'local',
117121
'size' => $upload1->size,
118122
'thumbnail' => $upload1->thumbnail(200, 200),
123+
'playable_preview_url' => null,
119124
'mime_type' => 'image/png',
120125
],
121126
'id' => $upload1->id,
@@ -126,6 +131,7 @@
126131
'disk' => 'local',
127132
'size' => $upload2->size,
128133
'thumbnail' => $upload2->thumbnail(200, 200),
134+
'playable_preview_url' => null,
129135
'mime_type' => 'image/png',
130136
],
131137
'id' => $upload2->id,
@@ -175,6 +181,7 @@
175181
'disk' => 'local',
176182
'size' => $upload1->size,
177183
'thumbnail' => $upload1->thumbnail(200, 200),
184+
'playable_preview_url' => null,
178185
'filters' => $filters,
179186
'mime_type' => 'image/png',
180187
],
@@ -186,13 +193,71 @@
186193
'disk' => 'local',
187194
'size' => $upload2->size,
188195
'thumbnail' => $upload2->thumbnail(200, 200),
196+
'playable_preview_url' => null,
189197
'mime_type' => 'image/png',
190198
],
191199
'id' => $upload2->id,
192200
],
193201
]);
194202
});
195203

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+
196261
describe('dynamicInstance', function () {
197262
it('allows to fake a sharpUpload and transform a single upload', function () {
198263
$file = createImage();
@@ -215,6 +280,7 @@
215280
'disk' => 'local',
216281
'size' => 120,
217282
'thumbnail' => (new SharpUploadModel($uploadData))->thumbnail(200, 200),
283+
'playable_preview_url' => null,
218284
'filters' => [],
219285
'mime_type' => 'image/png',
220286
],

tests/Unit/Form/Fields/Formatters/EditorFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
'/storage/thumbnails/data/Posts/1/200-200_q-90/image.jpg?%s',
156156
Storage::disk('public')->lastModified('/thumbnails/data/Posts/1/200-200_q-90/image.jpg')
157157
),
158+
'playable_preview_url' => null,
158159
'size' => 120,
159160
'mime_type' => 'image/jpeg',
160161
'filters' => null,
@@ -168,6 +169,7 @@
168169
'path' => 'data/Posts/1/doc.pdf',
169170
'disk' => 'local',
170171
'thumbnail' => null,
172+
'playable_preview_url' => null,
171173
'size' => 120,
172174
'mime_type' => 'application/pdf',
173175
'filters' => null,
@@ -287,6 +289,7 @@
287289
'path' => 'data/Posts/1/image.jpg',
288290
'disk' => 'local',
289291
'thumbnail' => $thumbnail,
292+
'playable_preview_url' => null,
290293
'size' => 120,
291294
'mime_type' => 'image/jpeg',
292295
'filters' => null,

tests/Unit/Show/Fields/Formatters/TextFormatterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
'/storage/thumbnails/data/Posts/1/200-200_q-90/image.jpg?%s',
6262
Storage::disk('public')->lastModified('/thumbnails/data/Posts/1/200-200_q-90/image.jpg')
6363
),
64+
'playable_preview_url' => null,
6465
'size' => 120,
6566
'mime_type' => 'image/jpeg',
6667
'filters' => null,
@@ -74,6 +75,7 @@
7475
'path' => 'data/Posts/1/doc.pdf',
7576
'disk' => 'local',
7677
'thumbnail' => null,
78+
'playable_preview_url' => null,
7779
'size' => 120,
7880
'mime_type' => 'application/pdf',
7981
'filters' => null,

0 commit comments

Comments
 (0)