/** * @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); }
/** * @param $entity * * @return array */ public function getScopeEntityAttachments($entity) { $attachments = []; $oroAttachments = $this->attachmentProvider->getEntityAttachments($entity); foreach ($oroAttachments as $oroAttachment) { $attachments[] = $this->emailAttachmentTransformer->oroToModel($oroAttachment); } return $attachments; }
public function testGetScopeEntityAttachments() { $entity = $this->getMock('\\stdClass'); $oroAttachments = []; $size = 3; for ($i = 0; $i < $size; $i++) { $oroAttachments[] = $this->getMock('Oro\\Bundle\\AttachmentBundle\\Entity\\Attachment'); } $this->attachmentProvider->expects($this->once())->method('getEntityAttachments')->with($entity)->willReturn($oroAttachments); $this->emailAttachmentTransformer->expects($this->exactly($size))->method('oroToModel'); $result = $this->emailAttachmentProvider->getScopeEntityAttachments($entity); $this->assertTrue(is_array($result)); $this->assertEquals($size, sizeof($result)); }
/** * @param $type * @param $getRepositoryCalls * @param $getRepositoryArgument * @param $repoReturnObject * @param $oroToEntityCalls * @param $entityFromUploadedFileCalls * * @dataProvider attachmentProvider */ public function testInitAttachmentEntity($type, $getRepositoryCalls, $getRepositoryArgument, $repoReturnObject, $oroToEntityCalls, $entityFromUploadedFileCalls) { $attachment = $this->getMock('Oro\\Bundle\\EmailBundle\\Form\\Model\\EmailAttachment'); $attachment->expects($this->once())->method('setEmailAttachment'); $uploadedFile = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->enableOriginalConstructor()->setConstructorArgs([__DIR__ . '/../../Fixtures/attachment/test.txt', ''])->getMock(); $attachment->expects($this->any())->method('getFile')->willReturn($uploadedFile); $formEvent = $this->getMockBuilder('Symfony\\Component\\Form\\FormEvent')->disableOriginalConstructor()->getMock(); $formEvent->expects($this->once())->method('getData')->willReturn($attachment); $attachment->expects($this->once())->method('getEmailAttachment')->willReturn(false); $attachment->expects($this->once())->method('getType')->willReturn($type); if ($getRepositoryCalls) { $repo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock(); $this->em->expects($this->exactly($getRepositoryCalls))->method('getRepository')->with($getRepositoryArgument)->willReturn($repo); $repo->expects($this->once())->method('find')->willReturn($repoReturnObject); } $this->emailAttachmentTransformer->expects($this->exactly($oroToEntityCalls))->method('oroToEntity'); $this->emailAttachmentTransformer->expects($this->exactly($entityFromUploadedFileCalls))->method('entityFromUploadedFile'); $this->emailAttachmentType->initAttachmentEntity($formEvent); }
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'); }