Files
myep-team/tests/Model/UploadSessionDtoTest.php
T
2026-09-09 11:10:57 +02:00

93 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Model;
use App\Entity\Upload;
use App\Model\UploadDto;
use App\Model\UploadSessionDto;
use PHPUnit\Framework\TestCase;
/**
* The upload session is one flat list shared by every dropzone on a page, and the teamer's
* disposition detail page has two of them at once: the Honorarnote and its Belege. Before the
* uploads carried their mapping, a consumer could only take getUploads()->first() - so submitting
* the invoice form could persist a receipt as the Honorarnote. That is what these pin down.
*/
class UploadSessionDtoTest extends TestCase
{
public function testUploadsAreSelectedByTheMappingTheyCameFrom(): void
{
$session = $this->createSession();
$this->assertSame(
['honorarnote.pdf'],
$this->filenames($session->getUploadsByType(Upload::TYPE_INVOICE))
);
$this->assertSame(
['bahn.pdf', 'taxi.jpg'],
$this->filenames($session->getUploadsByType(Upload::TYPE_RECEIPT))
);
}
public function testRemovingOneMappingLeavesTheOtherAlone(): void
{
$session = $this->createSession();
$session->removeUploadsByType(Upload::TYPE_RECEIPT);
$this->assertCount(0, $session->getUploadsByType(Upload::TYPE_RECEIPT));
$this->assertSame(['honorarnote.pdf'], $this->filenames($session->getUploadsByType(Upload::TYPE_INVOICE)));
}
/**
* A session written before UploadDto carried a type deserializes with a null one. Such an
* entry must match no mapping at all rather than being claimed by the first consumer to ask -
* that would be the very ambiguity the type removes.
*/
public function testAnUploadWithoutAMappingMatchesNothing(): void
{
$session = new UploadSessionDto();
$session->addUpload(new UploadDto('uuid-legacy', 'legacy.pdf', 'legacy.pdf', 'application/pdf', 1));
$this->assertCount(0, $session->getUploadsByType(Upload::TYPE_INVOICE));
$this->assertCount(0, $session->getUploadsByType(Upload::TYPE_RECEIPT));
$this->assertCount(1, $session->getUploads());
}
private function createSession(): UploadSessionDto
{
$session = new UploadSessionDto();
$session
->addUpload($this->createUpload('uuid-1', 'honorarnote.pdf', Upload::TYPE_INVOICE))
->addUpload($this->createUpload('uuid-2', 'bahn.pdf', Upload::TYPE_RECEIPT))
->addUpload($this->createUpload('uuid-3', 'taxi.jpg', Upload::TYPE_RECEIPT))
;
return $session;
}
private function createUpload(string $uuid, string $filename, string $type): UploadDto
{
return new UploadDto($uuid, $filename, $filename, 'application/pdf', 1024, $type);
}
/**
* @param \Doctrine\Common\Collections\Collection<int, UploadDto> $uploads
*
* @return array<int, string>
*/
private function filenames(iterable $uploads): array
{
$filenames = [];
foreach ($uploads as $upload) {
$filenames[] = $upload->getFilename();
}
return $filenames;
}
}