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 $uploads * * @return array */ private function filenames(iterable $uploads): array { $filenames = []; foreach ($uploads as $upload) { $filenames[] = $upload->getFilename(); } return $filenames; } }