Vincent 2 年之前
父节点
当前提交
c5a03ab17c

+ 6 - 3
src/Service/File/Utils/ImageUtils.php

@@ -10,7 +10,7 @@ use Liip\ImagineBundle\Imagine\Filter\FilterManager;
 
 class ImageUtils
 {
-    protected const FILTER_CROP = 'crop_filter';
+    public const FILTER_CROP = 'crop_filter';
 
     public function __construct(
         private DataManager $dataManager,
@@ -21,6 +21,8 @@ class ImageUtils
      * Permet de cropper et retourner une image
      * @param File $file : File contenant la définition de l'image
      * @return string : Image en Base64
+     * @see ImageUtilsTest::testCropImageWithConfig()
+     * @see ImageUtilsTest::testCropImageWithoutConfig()
      */
     public function cropImage(File $file): string{
         //On récupère l'image via le dataloader d'imagine liip (dataloader stream qui s'appuie sur gaufrette)
@@ -38,9 +40,10 @@ class ImageUtils
     /**
      * Définit et retourne le tableau de config servant à cropper
      * @param string $config : Configuration du File
-     * @return array[] : tableau de configuration
+     * @return array<string, array<string, array<string, array<int, int>>>> : tableau de configuration
+     * @see ImageUtilsTest::testGetCroppingConfig()
      */
-    private function getCroppingConfig(string $config): array{
+    protected function getCroppingConfig(string $config): array{
         $config = json_decode($config, true);
         return [
             'filters' => [

+ 52 - 26
tests/Unit/Service/File/Storage/LocalStorageTest.php

@@ -2,7 +2,6 @@
 
 namespace App\Tests\Unit\Service\File\Storage;
 
-use ApiPlatform\Core\Api\IriConverterInterface;
 use App\Entity\Access\Access;
 use App\Entity\Core\File;
 use App\Entity\Organization\Organization;
@@ -11,6 +10,7 @@ use App\Enum\Core\FileStatusEnum;
 use App\Enum\Core\FileTypeEnum;
 use App\Repository\Access\AccessRepository;
 use App\Service\File\Storage\LocalStorage;
+use App\Service\File\Utils\ImageUtils;
 use DateTime;
 use Doctrine\ORM\EntityManagerInterface;
 use Gaufrette\Filesystem;
@@ -37,14 +37,14 @@ class LocalStorageTest extends TestCase
     private EntityManagerInterface $entityManager;
     private AccessRepository $accessRepository;
     private Filesystem $filesystem;
-    private IriConverterInterface $iriConverter;
+    private ImageUtils $imageUtils;
 
     public function setUp(): void
     {
         $this->filesystemMap = $this->getMockBuilder(FilesystemMap::class)->disableOriginalConstructor()->getMock();
         $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
         $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
-        $this->iriConverter = $this->getMockBuilder(IriConverterInterface::class)->disableOriginalConstructor()->getMock();
+        $this->imageUtils = $this->getMockBuilder(ImageUtils::class)->disableOriginalConstructor()->getMock();
 
         $this->filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
         $this->filesystemMap->method('get')->with(TestableLocalStorage::FS_KEY)->willReturn($this->filesystem);
@@ -55,7 +55,7 @@ class LocalStorageTest extends TestCase
      */
     public function testExists(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['exists'])
             ->getMock();
 
@@ -72,7 +72,7 @@ class LocalStorageTest extends TestCase
      */
     public function testExistsInexistant(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['exists'])
             ->getMock();
 
@@ -91,7 +91,7 @@ class LocalStorageTest extends TestCase
     public function testListByOwner(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['listByOwner'])
             ->getMock();
 
@@ -110,15 +110,41 @@ class LocalStorageTest extends TestCase
     /**
      * @see LocalStorage::read()
      */
-    public function testRead(): void
+    public function testReadPdf(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['read'])
             ->getMock();
 
         $file = $this->getMockBuilder(File::class)->getMock();
         $file->method('getSlug')->willReturn('foo');
+        $file->method('getMimeType')->willReturn('application/pdf');
+
+        $this->filesystem->method('read')->with('foo')->willReturn('12345679');
+
+        $this->assertEquals(
+            '12345679',
+            $fileStorage->read($file)
+        );
+    }
+
+    /**
+     * @see LocalStorage::read()
+     */
+    public function testReadImage(): void
+    {
+        $file = $this->getMockBuilder(File::class)->getMock();
+        $file->method('getSlug')->willReturn('foo');
+        $file->method('getMimeType')->willReturn('image/jpeg');
+
+        $imageUtils = $this->imageUtils;
+        $imageUtils->method('cropImage')->with($file)->willReturn('12345679');
+
+        $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $imageUtils])
+            ->setMethodsExcept(['read'])
+            ->getMock();
 
         $this->filesystem->method('read')->with('foo')->willReturn('12345679');
 
@@ -134,7 +160,7 @@ class LocalStorageTest extends TestCase
     public function testPrepareFile(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['prepareFile'])
             ->getMock();
 
@@ -175,7 +201,7 @@ class LocalStorageTest extends TestCase
     public function testPrepareFileDefaultValues(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['prepareFile'])
             ->getMock();
 
@@ -204,7 +230,7 @@ class LocalStorageTest extends TestCase
     public function testPrepareFileNoFlush(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['prepareFile'])
             ->getMock();
 
@@ -233,7 +259,7 @@ class LocalStorageTest extends TestCase
      */
     public function testWriteFileNewFile(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['writeFile'])
             ->getMock();
 
@@ -287,7 +313,7 @@ class LocalStorageTest extends TestCase
      */
     public function testWriteFileExistingFile(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['writeFile'])
             ->getMock();
 
@@ -340,7 +366,7 @@ class LocalStorageTest extends TestCase
     public function testWriteFileExistingButMissingFile(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['writeFile'])
             ->getMock();
 
@@ -373,7 +399,7 @@ class LocalStorageTest extends TestCase
      */
     public function testWriteFileWithAccessOwner(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['writeFile'])
             ->getMock();
 
@@ -416,7 +442,7 @@ class LocalStorageTest extends TestCase
     public function testWriteFileWithNoName(): void
     {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['writeFile'])
             ->getMock();
 
@@ -436,7 +462,7 @@ class LocalStorageTest extends TestCase
      */
     public function testMakeFile(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['makeFile'])
             ->getMock();
 
@@ -472,7 +498,7 @@ class LocalStorageTest extends TestCase
      */
     public function testSoftdelete(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['softDelete'])
             ->getMock();
 
@@ -496,7 +522,7 @@ class LocalStorageTest extends TestCase
      */
     public function testHardDelete(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['hardDelete'])
             ->getMock();
 
@@ -513,7 +539,7 @@ class LocalStorageTest extends TestCase
      */
     public function testHardDeleteFailed(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['hardDelete'])
             ->getMock();
 
@@ -533,7 +559,7 @@ class LocalStorageTest extends TestCase
      */
     public function testGetPrefixAccess(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['getPrefix'])
             ->getMock();
 
@@ -554,7 +580,7 @@ class LocalStorageTest extends TestCase
      */
     public function testGetPrefixOrganization(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['getPrefix'])
             ->getMock();
 
@@ -571,7 +597,7 @@ class LocalStorageTest extends TestCase
      */
     public function testGetPrefixPerson(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['getPrefix'])
             ->getMock();
 
@@ -588,7 +614,7 @@ class LocalStorageTest extends TestCase
      */
     public function testGetPrefixTemp(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['getPrefix'])
             ->getMock();
 
@@ -605,7 +631,7 @@ class LocalStorageTest extends TestCase
      */
     public function testGetPrefixWithType(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['getPrefix'])
             ->getMock();
 
@@ -622,7 +648,7 @@ class LocalStorageTest extends TestCase
      */
     public function testGetOrganizationAndPersonFromOwner(): void {
         $fileStorage = $this->getMockBuilder(TestableLocalStorage::class)
-            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->iriConverter])
+            ->setConstructorArgs([$this->filesystemMap, $this->entityManager, $this->accessRepository, $this->imageUtils])
             ->setMethodsExcept(['getOrganizationAndPersonFromOwner'])
             ->getMock();
 

+ 117 - 0
tests/Unit/Service/File/Utils/ImageUtilsTest.php

@@ -0,0 +1,117 @@
+<?php
+
+namespace App\tests\Unit\Service\File\Utils;
+
+use App\Entity\Core\File;
+use App\Service\File\Utils\ImageUtils;
+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\TestCase;
+
+class ImageUtilsTest extends TestCase
+{
+    use TestToolsTrait;
+
+    private DataManager $dataManager;
+    private FilterManager $filterManager;
+
+    public function setUp(): void
+    {
+        $this->dataManager = $this->getMockBuilder(DataManager::class)->disableOriginalConstructor()->getMock();
+        $this->filterManager = $this->getMockBuilder(FilterManager::class)->disableOriginalConstructor()->getMock();
+    }
+
+    public function getImageUtilsMockFor(string $methodName) {
+        return $this->getMockBuilder(TestableImageUtils::class)
+            ->setConstructorArgs([$this->dataManager, $this->filterManager])
+            ->setMethodsExcept([$methodName])
+            ->getMock();
+    }
+
+    /**
+     * @see ImageUtils::getCroppingConfig()
+     * @return void
+     * @throws \ReflectionException
+     */
+    public function testGetCroppingConfig(): void {
+        $imageUtils = $this->getImageUtilsMockFor('getCroppingConfig');
+
+        $config = '{"width": 100, "height": 100, "x": 10, "y": 10}';
+        $result =[
+            'filters' => [
+                'crop' => [
+                    'size' => [100, 100],
+                    'start' => [10, 10]
+                ]
+            ]
+        ];
+
+        $this->assertEquals($result, $this->invokeMethod($imageUtils, 'getCroppingConfig', [$config]));
+    }
+
+
+    /**
+     * @see ImageUtils::cropImage()
+     * @return void
+     */
+    public function testCropImageWithoutConfig()
+    {
+        $file = new File();
+        $file->setPath('example.jpg');
+        $file->setConfig('');
+
+        $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);
+
+        $result = $imageUtils->cropImage($file);
+
+        $this->assertEquals('mocked_binary_data', $result);
+    }
+
+    /**
+     * @see ImageUtils::cropImage()
+     * @return void
+     */
+    public function testCropImageWithConfig()
+    {
+        $file = new File();
+        $file->setPath('example.jpg');
+        $file->setConfig('{"width": 100, "height": 100, "x": 10, "y": 10}');
+        $config  = [
+            'filters' => [
+                'crop' => [
+                    'size' => [100, 100],
+                    'start' => [10, 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->filterManager->expects($this->once())
+            ->method('applyFilter')
+            ->with($binary, ImageUtils::FILTER_CROP, $config)
+            ->willReturn($binary);
+
+        $result = $imageUtils->cropImage($file);
+
+        $this->assertEquals('mocked_binary_data', $result);
+    }
+}