Skip to content

Commit

Permalink
do not throw exception if there are no deferred images
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vo committed Sep 28, 2020
1 parent 71ffe3f commit b73ba71
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
16 changes: 10 additions & 6 deletions core-bundle/src/Image/Studio/ImageResult.php
Expand Up @@ -154,12 +154,6 @@ public function getFilePath($absolute = false): string
*/
public function createIfDeferred(): void
{
$resizer = $this->resizer();

if (!$resizer instanceof DeferredResizerInterface) {
throw new \RuntimeException('The "contao.image.resizer" service does not support deferred resizing.');
}

$picture = $this->getPicture();

$array_flatten = static function (array $array): array {
Expand Down Expand Up @@ -187,6 +181,16 @@ static function ($image): bool {
$array_flatten(array_map($findDeferredImages, $picture->getSources()))
);

if (empty($deferredImages)) {
return;
}

$resizer = $this->resizer();

if (!$resizer instanceof DeferredResizerInterface) {
throw new \RuntimeException('The "contao.image.resizer" service does not support deferred resizing.');
}

$resizedPaths = [];

/** @var DeferredImageInterface $deferredImage */
Expand Down
60 changes: 59 additions & 1 deletion core-bundle/tests/Image/Studio/ImageResultTest.php
Expand Up @@ -18,6 +18,7 @@
use Contao\CoreBundle\Image\Studio\ImageResult;
use Contao\CoreBundle\Tests\TestCase;
use Contao\Image\DeferredImage;
use Contao\Image\DeferredImageInterface;
use Contao\Image\DeferredResizerInterface;
use Contao\Image\Image;
use Contao\Image\ImageDimensions;
Expand Down Expand Up @@ -200,7 +201,6 @@ public function testGetFilePathFromImageResource(): void
*/
public function testCreateIfDeferred(array $img, array $sources, array $expectedDeferredImages): void
{
/** @var PictureInterface&MockObject $picture */
$picture = $this->createMock(PictureInterface::class);
$picture
->expects($this->once())
Expand Down Expand Up @@ -342,7 +342,26 @@ public function provideDeferredImages(): \Generator

public function testCreateIfDeferredFailsWithoutDeferredResizer(): void
{
$picture = $this->createMock(PictureInterface::class);
$picture
->expects($this->once())
->method('getSources')
->with()
->willReturn([])
;

$picture
->expects($this->once())
->method('getImg')
->with()
->willReturn(['src' => $this->createMock(DeferredImageInterface::class)])
;

$pictureFactory = $this->createMock(PictureFactoryInterface::class);
$pictureFactory
->method('create')
->willReturn($picture)
;

$nonDeferredResizer = $this->createMock(Resizer::class);

Expand All @@ -363,6 +382,45 @@ public function testCreateIfDeferredFailsWithoutDeferredResizer(): void
$imageResult->createIfDeferred();
}

public function testCreateIfDeferredDoesNotFailWithoutDeferredResizerIfThereAreNoDeferredImages(): void
{
$picture = $this->createMock(PictureInterface::class);
$picture
->expects($this->once())
->method('getSources')
->with()
->willReturn([])
;

$picture
->expects($this->once())
->method('getImg')
->with()
->willReturn([])
;

$pictureFactory = $this->createMock(PictureFactoryInterface::class);
$pictureFactory
->method('create')
->willReturn($picture)
;

$nonDeferredResizer = $this->createMock(Resizer::class);

$locator = $this->createMock(ContainerInterface::class);
$locator
->method('get')
->willReturnMap([
['contao.image.picture_factory', $pictureFactory],
['contao.image.resizer', $nonDeferredResizer],
])
;

$imageResult = new ImageResult($locator, '/project/dir', '/project/dir/image.jpg');

$imageResult->createIfDeferred();
}

/**
* @return PictureFactoryInterface&MockObject
*/
Expand Down

0 comments on commit b73ba71

Please sign in to comment.