Skip to content

Commit

Permalink
Add support for preview image in media serializer (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Schoonderwaldt authored Mar 26, 2021
1 parent cf92090 commit 76da5e9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Content/Serializer/MediaSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,28 @@ public function serialize(MediaInterface $media, string $locale, ?SerializationC
unset($mediaData['adminUrl']);
unset($mediaData['_hash']);

$formatMediaApi = $apiMedia;
/** @var MediaInterface|null $previewImage */
$previewImage = $media->getPreviewImage();
if ($previewImage) {
$formatMediaApi = new Media($previewImage, $locale);
}

/** @var string $fileName */
$fileName = $apiMedia->getName();
$fileName = $formatMediaApi->getName();

// replace extension of filename with preferred media extension if possible
$preferredExtension = $this->imageConverter->getSupportedOutputImageFormats($apiMedia->getMimeType())[0] ?? null;
$preferredExtension = $this->imageConverter->getSupportedOutputImageFormats($formatMediaApi->getMimeType())[0] ?? null;
if ($preferredExtension) {
$fileName = pathinfo($fileName)['filename'] . '.' . $preferredExtension;
}

$mediaData['formatUri'] = $this->formatCache->getMediaUrl(
$apiMedia->getId(),
$formatMediaApi->getId(),
$fileName,
'{format}',
$apiMedia->getVersion(),
$apiMedia->getSubVersion()
$formatMediaApi->getVersion(),
$formatMediaApi->getSubVersion()
);

$this->referenceStore->add($apiMedia->getId());
Expand Down
66 changes: 66 additions & 0 deletions Tests/Unit/Content/Serializer/MediaSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Sulu\Bundle\HeadlessBundle\Content\Serializer\MediaSerializer;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\MediaSerializerInterface;
use Sulu\Bundle\MediaBundle\Api\Media;
use Sulu\Bundle\MediaBundle\Entity\File;
use Sulu\Bundle\MediaBundle\Entity\FileVersion;
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
use Sulu\Bundle\MediaBundle\Media\FormatCache\FormatCacheInterface;
use Sulu\Bundle\MediaBundle\Media\ImageConverter\ImageConverterInterface;
Expand Down Expand Up @@ -128,6 +130,70 @@ public function testSerialize(): void
], $result);
}

public function testSerializeWithPreviewImage(): void
{
$locale = 'en';
$media = $this->prophesize(MediaInterface::class);

// expected and unexpected object calls
$apiMedia = $this->prophesize(Media::class);
$apiMedia->getId()->willReturn(1)->shouldBeCalled();

$previewMediaFileVersion = $this->prophesize(FileVersion::class);
$previewMediaFileVersion->getName()->willReturn('preview-media.png')->shouldBeCalled();
$previewMediaFileVersion->getMimeType()->willReturn('image/png')->shouldBeCalled();
$previewMediaFileVersion->getVersion()->willReturn(1)->shouldBeCalled();
$previewMediaFileVersion->getSubVersion()->willReturn(0)->shouldBeCalled();

$previewMediaFile = $this->prophesize(File::class);
$previewMediaFile->getVersion()->willReturn(1)->shouldBeCalled();
$previewMediaFile->getFileVersion(1)->willReturn($previewMediaFileVersion->reveal())->shouldBeCalled();

$previewMedia = $this->prophesize(MediaInterface::class);
$previewMedia->getFiles()->willReturn([$previewMediaFile->reveal()])->shouldBeCalled();

$previewMedia->getId()->willReturn(1)->shouldBeCalled();
$media->getPreviewImage()->willReturn($previewMedia->reveal())->shouldBeCalled();

$apiMediaArgument = Argument::that(function (Media $apiMedia) use ($media, $locale) {
return $apiMedia->getEntity() === $media->reveal() && $locale === $apiMedia->getLocale();
});

// expected and unexpected service calls
$this->mediaManager->addFormatsAndUrl($apiMediaArgument)
->willReturn($apiMedia->reveal())
->shouldBeCalled();

$this->arraySerializer->serialize($apiMedia->reveal(), null)->willReturn([
'id' => 1,
'formats' => [],
'storageOptions' => [],
'thumbnails' => [],
'versions' => [],
'downloadCounter' => [],
'_hash' => [],
])->shouldBeCalled();

$this->imageConverter->getSupportedOutputImageFormats('image/png')
->willReturn([])
->shouldBeCalled();

$this->formatCache->getMediaUrl(1, 'preview-media.png', '{format}', 1, 0)
->willReturn('/media/1/{format}/preview-media.png?v=1-0')
->shouldBeCalled();

$this->referenceStore->add(1)
->shouldBeCalled();

// call test function
$result = $this->mediaSerializer->serialize($media->reveal(), $locale);

$this->assertSame([
'id' => 1,
'formatUri' => '/media/1/{format}/preview-media.png?v=1-0',
], $result);
}

public function testSerializeWithContext(): void
{
$locale = 'en';
Expand Down

0 comments on commit 76da5e9

Please sign in to comment.