143 lines
4.6 KiB
PHP
143 lines
4.6 KiB
PHP
<?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 App\Service\Upload\UploadHandler;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
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;
|
|
private UploadHandler&MockObject $uploadHandler;
|
|
|
|
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->uploadHandler = $this->createMock(UploadHandler::class);
|
|
|
|
$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], $this->uploadHandler);
|
|
}
|
|
|
|
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);
|
|
|
|
$this->uploadHandler->method('getResolvedUploadFilepath')->with($invoice)->willReturn($this->pdfPath);
|
|
$this->uploadHandler->method('getUploadFilepath')->with($invoice)->willReturn($this->pdfPath);
|
|
|
|
return $disposition;
|
|
}
|
|
}
|