Vincent 2 лет назад
Родитель
Сommit
6bb481e2d8

+ 2 - 2
src/Entity/Core/File.php

@@ -348,12 +348,12 @@ class File
         return $this;
     }
 
-    public function getConfig(): string
+    public function getConfig(): ?string
     {
         return $this->config;
     }
 
-    public function setConfig(string $config): self
+    public function setConfig(?string $config): self
     {
         $this->config = $config;
         return $this;

+ 13 - 4
src/Service/File/Utils/ImageUtils.php

@@ -7,6 +7,7 @@ namespace App\Service\File\Utils;
 use App\Entity\Core\File;
 use Liip\ImagineBundle\Imagine\Data\DataManager;
 use Liip\ImagineBundle\Imagine\Filter\FilterManager;
+use Psr\Log\LoggerInterface;
 
 class ImageUtils
 {
@@ -14,8 +15,9 @@ class ImageUtils
 
     public function __construct(
         private DataManager $dataManager,
-        private FilterManager $filterManager
-    ){}
+        private FilterManager $filterManager,
+        private LoggerInterface $logger
+){}
 
     /**
      * Permet de cropper et retourner une image
@@ -30,8 +32,15 @@ class ImageUtils
 
         //Si on a une config, on créer l'image
         if(!empty($file->getConfig())){
-            //On applique le filtre crop et on retourne l'image
-            $binary = $this->filterManager->applyFilter($binary, self::FILTER_CROP, $this->getCroppingConfig($file->getConfig()));
+            try{
+                //On applique le filtre crop et on retourne l'image
+                $binary = $this->filterManager->applyFilter($binary, self::FILTER_CROP, $this->getCroppingConfig($file->getConfig()));
+            }catch(\Exception $e){
+                //Si nous avons une erreur, nous retournons l'image sans crop et on log.
+                $this->logger->error('Error while cropping image');
+                $this->logger->error($e->getMessage());
+                return $binary->getContent();
+            }
         }
 
         return $binary->getContent();

+ 41 - 5
tests/Unit/Service/File/Utils/ImageUtilsTest.php

@@ -8,24 +8,28 @@ use App\Tests\Unit\TestToolsTrait;
 use Liip\ImagineBundle\Imagine\Data\DataManager;
 use Liip\ImagineBundle\Imagine\Filter\FilterManager;
 use Liip\ImagineBundle\Model\Binary;
+use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
 
 class ImageUtilsTest extends TestCase
 {
     use TestToolsTrait;
 
-    private DataManager $dataManager;
-    private FilterManager $filterManager;
+    private MockObject | DataManager $dataManager;
+    private MockObject | FilterManager $filterManager;
+    private MockObject | LoggerInterface $logger;
 
     public function setUp(): void
     {
         $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
         $this->filterManager = $this->getMockBuilder(FilterManager::class)->disableOriginalConstructor()->getMock();
+        $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
     }
 
     public function getImageUtilsMockFor(string $methodName) {
-        return $this->getMockBuilder(TestableImageUtils::class)
-            ->setConstructorArgs([$this->dataManager, $this->filterManager])
+        return $this->getMockBuilder(ImageUtils::class)
+            ->setConstructorArgs([$this->dataManager, $this->filterManager, $this->logger])
             ->setMethodsExcept([$methodName])
             ->getMock();
     }
@@ -60,7 +64,7 @@ class ImageUtilsTest extends TestCase
     {
         $file = new File();
         $file->setPath('example.jpg');
-        $file->setConfig('');
+        $file->setConfig(null);
 
         $imageUtils = $this->getImageUtilsMockFor('cropImage');
 
@@ -110,6 +114,38 @@ class ImageUtilsTest extends TestCase
             ->with($binary, ImageUtils::FILTER_CROP, $config)
             ->willReturn($binary);
 
+        $this->logger->expects($this->never())->method('error');
+
+        $result = $imageUtils->cropImage($file);
+
+        $this->assertEquals('mocked_binary_data', $result);
+    }
+
+
+    /**
+     * @see ImageUtils::cropImage()
+     * @return void
+     */
+    public function testCropImageWithErrorConfig()
+    {
+        $file = new File();
+        $file->setPath('example.jpg');
+        $file->setConfig('{"hei": 100, "x": 10, "y": 10}');
+
+        $imageUtils = $this->getImageUtilsMockFor('cropImage');
+
+        $binary = $this->createMock(Binary::class);
+        $binary->method('getContent')->willReturn('mocked_binary_data');
+
+        $this->dataManager->expects($this->once())
+            ->method('find')
+            ->with(ImageUtils::FILTER_CROP, $file->getPath())
+            ->willReturn($binary);
+
+        $this->logger->expects($this->exactly(2))
+            ->method('error')
+        ;
+
         $result = $imageUtils->cropImage($file);
 
         $this->assertEquals('mocked_binary_data', $result);