feat: reduce number of backups created when processing PDF files
This commit is contained in:
@@ -64,7 +64,7 @@ class InvoiceApprover extends AbstractPdfRenderer
|
||||
{
|
||||
$filesystem = new Filesystem();
|
||||
|
||||
if (!$filesystem->exists($source)) {
|
||||
if (false === $filesystem->exists($source)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -72,7 +72,11 @@ class InvoiceApprover extends AbstractPdfRenderer
|
||||
$filename = pathinfo($source, PATHINFO_FILENAME);
|
||||
$extension = pathinfo($source, PATHINFO_EXTENSION);
|
||||
|
||||
$target = sprintf('%s/%s.bak.%s', $path, $filename, $extension);
|
||||
$target = sprintf('%s/%s.orig.%s', $path, $filename, $extension);
|
||||
|
||||
if (true === $filesystem->exists($target)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$filesystem->copy($source, $target);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Service\Pdf;
|
||||
|
||||
use App\Entity\Application;
|
||||
use App\Entity\Assignment;
|
||||
use App\Entity\Destination;
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\JobProfile;
|
||||
use App\Entity\Teamer;
|
||||
use App\Entity\Upload;
|
||||
use App\Service\Pdf\InvoiceApprover;
|
||||
use App\Service\Pdf\InvoiceRenderer;
|
||||
use App\Service\Pdf\Pdf;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class InvoiceApproverTest extends WebTestCase
|
||||
{
|
||||
private string $projectDir;
|
||||
private string $invoiceDir;
|
||||
private string $pdfPath;
|
||||
private string $origPath;
|
||||
private Filesystem $filesystem;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->projectDir = static::getContainer()->getParameter('kernel.project_dir');
|
||||
$this->invoiceDir = $this->projectDir.'/uploads/invoice/t';
|
||||
$this->pdfPath = $this->invoiceDir.'/test_approver_backup.pdf';
|
||||
$this->origPath = $this->invoiceDir.'/test_approver_backup.orig.pdf';
|
||||
$this->filesystem = new Filesystem();
|
||||
|
||||
$this->filesystem->mkdir($this->invoiceDir);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->filesystem->remove($this->pdfPath);
|
||||
$this->filesystem->remove($this->origPath);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testSkipsBackupWhenOrigExists(): void
|
||||
{
|
||||
$disposition = $this->createDispositionWithInvoicePdf();
|
||||
|
||||
$origContent = file_get_contents($this->pdfPath);
|
||||
$this->filesystem->copy($this->pdfPath, $this->origPath);
|
||||
|
||||
$approver = $this->createApprover();
|
||||
$pdf = $approver->render($disposition);
|
||||
|
||||
$this->assertInstanceOf(Pdf::class, $pdf);
|
||||
$this->assertFileExists($this->origPath);
|
||||
$this->assertStringEqualsFile($this->origPath, $origContent);
|
||||
}
|
||||
|
||||
public function testCreatesOrigBackupWhenNoneExists(): void
|
||||
{
|
||||
$disposition = $this->createDispositionWithInvoicePdf();
|
||||
|
||||
$this->assertFileDoesNotExist($this->origPath);
|
||||
|
||||
$approver = $this->createApprover();
|
||||
$pdf = $approver->render($disposition);
|
||||
|
||||
$this->assertInstanceOf(Pdf::class, $pdf);
|
||||
$this->assertFileExists($this->origPath);
|
||||
}
|
||||
|
||||
private function createApprover(): InvoiceApprover
|
||||
{
|
||||
$translator = $this->createMock(TranslatorInterface::class);
|
||||
|
||||
/** @var TranslatorInterface $translator */
|
||||
return new InvoiceApprover($translator, ['project_dir' => $this->projectDir]);
|
||||
}
|
||||
|
||||
private function createDispositionWithInvoicePdf(): Disposition
|
||||
{
|
||||
$teamer = new Teamer();
|
||||
$teamer
|
||||
->setFirstName('Test')
|
||||
->setLastName('User')
|
||||
;
|
||||
|
||||
$destination = new Destination();
|
||||
$destination
|
||||
->setProduct('Testreise')
|
||||
->setHotel('Testhotel')
|
||||
->setCode('TEST01')
|
||||
->setDateFrom(new \DateTimeImmutable('2025-01-01'))
|
||||
->setDateTo(new \DateTimeImmutable('2025-01-08'))
|
||||
->setCostUnit('100')
|
||||
;
|
||||
|
||||
$jobProfile = new JobProfile();
|
||||
$jobProfile->setName('Testprofil');
|
||||
$jobProfile->setCostUnit('200');
|
||||
|
||||
$assignment = $this->createMock(Assignment::class);
|
||||
$assignment->method('getDestination')->willReturn($destination);
|
||||
$assignment->method('getJobProfile')->willReturn($jobProfile);
|
||||
|
||||
$application = new Application($assignment, $teamer);
|
||||
$disposition = new Disposition($application);
|
||||
|
||||
// Create a minimal valid PDF at the expected path
|
||||
$pdf = new Pdf();
|
||||
$pdf->AddPage();
|
||||
$pdf->SetFont('Arial', '', 12);
|
||||
$pdf->Cell(40, 10, 'Test Invoice');
|
||||
$pdf->Output('F', $this->pdfPath);
|
||||
|
||||
$invoice = new Upload();
|
||||
$invoice
|
||||
->setType(Upload::TYPE_INVOICE)
|
||||
->setMimeType('application/pdf')
|
||||
->setFilename('test_approver_backup.pdf')
|
||||
->setApprovedBy('TESTER')
|
||||
->setApprovedAt(new \DateTimeImmutable())
|
||||
->setStatus(Upload::STATUS_PAID)
|
||||
;
|
||||
$disposition->addDocument($invoice);
|
||||
|
||||
return $disposition;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user