From 0652ca9f8b8b614cde902232792036d7588e08cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Mon, 2 Feb 2026 17:09:09 +0100 Subject: [PATCH] feat: reduce number of backups created when processing PDF files --- src/Service/Pdf/InvoiceApprover.php | 8 +- tests/Service/Pdf/InvoiceApproverTest.php | 135 ++++++++++++++++++++++ 2 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 tests/Service/Pdf/InvoiceApproverTest.php diff --git a/src/Service/Pdf/InvoiceApprover.php b/src/Service/Pdf/InvoiceApprover.php index 44f7581..c3fe76d 100644 --- a/src/Service/Pdf/InvoiceApprover.php +++ b/src/Service/Pdf/InvoiceApprover.php @@ -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); } diff --git a/tests/Service/Pdf/InvoiceApproverTest.php b/tests/Service/Pdf/InvoiceApproverTest.php new file mode 100644 index 0000000..f0f1d74 --- /dev/null +++ b/tests/Service/Pdf/InvoiceApproverTest.php @@ -0,0 +1,135 @@ +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; + } +}