Bläddra i källkod

complete unit tests

Olivier Massot 1 år sedan
förälder
incheckning
500fa9dd5a

+ 2 - 4
src/Service/Export/Encoder/DocXEncoder.php

@@ -43,11 +43,9 @@ class DocXEncoder implements EncoderInterface
         try {
             $this->phpDocx->createDocx($tempFilename);
 
-            return file_get_contents($tempFilename);
+            return $this->fileUtils->getFileContent($tempFilename);
         } finally {
-            if (is_file($tempFilename)) {
-                unlink($tempFilename);
-            }
+            $this->fileUtils->unlinkIfExist($tempFilename);
         }
     }
 }

+ 11 - 1
src/Service/Utils/FileUtils.php

@@ -5,7 +5,6 @@ declare(strict_types=1);
 namespace App\Service\Utils;
 
 use App\Entity\Core\File;
-use Exception;
 use Mimey\MimeTypes;
 use RuntimeException;
 
@@ -68,4 +67,15 @@ class FileUtils
         }
         return $tempDir . '/' . $prefix . uniqid() . '.' . $ext;
     }
+
+    public function unlinkIfExist(string $path): void
+    {
+        if (file_exists($path)) {
+            unlink($path);
+        }
+    }
+
+    public function getFileContent(string $path): string {
+        return file_get_contents($path);
+    }
 }

+ 105 - 0
tests/Unit/Service/Export/Encoder/DocXEncoderTest.php

@@ -0,0 +1,105 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Tests\Unit\Service\Export\Encoder;
+
+use App\Service\Export\Encoder\DocXEncoder;
+use App\Service\Utils\FileUtils;
+use Phpdocx\Create\CreateDocx;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+class DocXEncoderTest extends TestCase
+{
+    private MockObject | CreateDocx $phpDocx;
+    private MockObject | FileUtils $fileUtils;
+
+    public function setUp(): void {
+        $this->phpDocx = $this->getMockBuilder(CreateDocx::class)->disableOriginalConstructor()->getMock();
+        $this->fileUtils = $this->getMockBuilder(FileUtils::class)->disableOriginalConstructor()->getMock();
+    }
+
+    public function testSupport(): void
+    {
+        $encoder = $this->getMockBuilder(DocXEncoder::class)
+            ->setConstructorArgs([$this->phpDocx, $this->fileUtils])
+            ->setMethodsExcept(['support'])
+            ->getMock();
+
+        $this->assertTrue($encoder->support('docx'));
+        $this->assertFalse($encoder->support('txt'));
+    }
+
+    public function testEncode(): void {
+        $encoder = $this->getMockBuilder(DocXEncoder::class)
+            ->setConstructorArgs([$this->phpDocx, $this->fileUtils])
+            ->setMethodsExcept(['encode'])
+            ->getMock();
+
+        $this->fileUtils
+            ->expects(self::once())
+            ->method('getTempFilename')
+            ->with('docx')
+            ->willReturn('tmp/temp.docx');
+
+        $this->phpDocx
+            ->expects($this->once())
+            ->method('embedHtml')
+            ->with('<div>content</div>');
+
+        $this->phpDocx
+            ->expects(self::once())
+            ->method('createDocx')
+            ->with('tmp/temp.docx');
+
+        $this->fileUtils
+            ->expects(self::once())
+            ->method('getFileContent')
+            ->with('tmp/temp.docx')
+            ->willReturn('%%encoded%%');
+
+        $this->fileUtils
+            ->expects(self::once())
+            ->method('unlinkIfExist')
+            ->with('tmp/temp.docx');
+
+        $this->assertEquals(
+            '%%encoded%%',
+            $encoder->encode('<div>content</div>')
+        );
+    }
+
+    public function testEncodeWithError(): void {
+        $encoder = $this->getMockBuilder(DocXEncoder::class)
+            ->setConstructorArgs([$this->phpDocx, $this->fileUtils])
+            ->setMethodsExcept(['encode'])
+            ->getMock();
+
+        $this->fileUtils
+            ->method('getTempFilename')
+            ->with('docx')
+            ->willReturn('tmp/temp.docx');
+
+        $this->phpDocx
+            ->method('embedHtml')
+            ->with('<div>content</div>');
+
+        $this->phpDocx
+            ->method('createDocx')
+            ->with('tmp/temp.docx');
+
+        $this->fileUtils
+            ->method('getFileContent')
+            ->willThrowException(new \Exception('error'));
+
+        $this->fileUtils
+            ->expects(self::once())
+            ->method('unlinkIfExist')
+            ->with('tmp/temp.docx');
+
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('error');
+
+        $encoder->encode('<div>content</div>');
+    }
+}