/** * @param FormEvent $event * * @throws FormException */ public function initAttachmentEntity(FormEvent $event) { /** @var AttachmentModel $attachment */ $attachment = $event->getData(); // this check is necessary due to inability to capture file input dialog cancel event if (!$attachment) { return; } if (!$attachment->getEmailAttachment()) { switch ($attachment->getType()) { case AttachmentModel::TYPE_ATTACHMENT: $repo = $this->em->getRepository('OroAttachmentBundle:Attachment'); $oroAttachment = $repo->find($attachment->getId()); $emailAttachment = $this->emailAttachmentTransformer->oroToEntity($oroAttachment); break; case AttachmentModel::TYPE_EMAIL_ATTACHMENT: $repo = $this->em->getRepository('OroEmailBundle:EmailAttachment'); $emailAttachment = $repo->find($attachment->getId()); break; case AttachmentModel::TYPE_UPLOADED: $emailAttachment = $this->emailAttachmentTransformer->entityFromUploadedFile($attachment->getFile()); break; default: throw new FormException(sprintf('Invalid attachment type: %s', $attachment->getType())); } $attachment->setEmailAttachment($emailAttachment); } $event->setData($attachment); }
public function testEntityFromUploadedFile() { $fileContent = "test attachment\n"; $uploadedFile = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->enableOriginalConstructor()->setConstructorArgs([__DIR__ . '/../Fixtures/attachment/test.txt', ''])->getMock(); $uploadedFile->expects($this->once())->method('getMimeType')->willReturn('text/plain'); $uploadedFile->expects($this->once())->method('getClientOriginalName')->willReturn('test.txt'); $uploadedFile->expects($this->once())->method('getRealPath')->willReturn(__DIR__ . '/../Fixtures/attachment/test.txt'); $attachmentEntity = $this->emailAttachmentTransformer->entityFromUploadedFile($uploadedFile); $this->assertInstanceOf('Oro\\Bundle\\EmailBundle\\Entity\\EmailAttachment', $attachmentEntity); $content = $attachmentEntity->getContent(); $this->assertEquals(base64_encode($fileContent), $content->getContent()); $this->assertEquals('base64', $content->getContentTransferEncoding()); $this->assertEquals($attachmentEntity->getContentType(), 'text/plain'); $this->assertEquals($attachmentEntity->getFileName(), 'test.txt'); }