Skip to content

Commit

Permalink
Fix Htmlable render media (#2864)
Browse files Browse the repository at this point in the history
* Fix Htmlable render media

* Fix test & add test

* Remove not matched phpstan ignoreErrors rules

Co-authored-by: Quentin <[email protected]>
  • Loading branch information
Balsakup and quentin-acid authored Mar 31, 2022
1 parent 96491e2 commit 0452b7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ parameters:
count: 2
path: src/Conversions/ConversionCollection.php

-
message: "#^Access to an undefined property mixed\\:\\:\\$mediaConversions\\.$#"
count: 1
path: src/Conversions/ConversionCollection.php

-
message: "#^Method Spatie\\\\MediaLibrary\\\\Conversions\\\\ConversionCollection\\:\\:addManipulationToConversion\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -1495,11 +1490,6 @@ parameters:
count: 1
path: src/MediaCollections/Models/Media.php

-
message: "#^Property Spatie\\\\MediaLibrary\\\\MediaCollections\\\\Models\\\\Media\\:\\:\\$casts type has no value type specified in iterable type array\\.$#"
count: 1
path: src/MediaCollections/Models/Media.php

-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
Expand Down
5 changes: 4 additions & 1 deletion src/MediaCollections/HtmlableMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Support\Htmlable;
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\Conversions\ImageGenerators\Image;
use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class HtmlableMedia implements Htmlable, \Stringable
Expand Down Expand Up @@ -43,7 +44,9 @@ public function lazy(): self

public function toHtml()
{
if (! (new Image())->canHandleMime($this->media->mime_type)) {
$imageGenerator = ImageGeneratorFactory::forMedia($this->media) ?? new Image();

if (! $imageGenerator->canHandleMime($this->media->mime_type)) {
return '';
}

Expand Down
13 changes: 12 additions & 1 deletion tests/Feature/Media/ToHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@

test('converting a non image to an image tag will not blow up', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestPdf())
->addMedia($this->getTestTiff())
->toMediaCollection();

expect($media->img())->toEqual('');
});

it('can render pdf thumbnail as an image', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestPdf())
->toMediaCollection();

$this->assertEquals(
"<img src=\"/media/{$media->id}/conversions/test-thumb.jpg\" alt=\"test\">",
$media->img('thumb'),
);
});

it('can render itself with responsive images and a placeholder', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestJpg())
Expand Down

0 comments on commit 0452b7d

Please sign in to comment.