/** * Create new attachment and assign it to the attachment holder * @param string $filename * @param string $content * @param AttachmentHolder $holder * @return \Diamante\DeskBundle\Model\Attachment\Attachment */ public function createNewAttachment($filename, $content, AttachmentHolder $holder) { $this->validateFilename($filename); $this->validateContent($content); $filenamePrefix = $this->exposeFilenamePrefixFrom($holder); $path = $this->fileStorageService->upload($filenamePrefix . '/' . $filename, $content); $file = new File($path); $hash = $this->generateFileHash($file); if ($this->isImage($file)) { $this->createThumbnail($file, $hash, $filenamePrefix); } $attachment = $this->factory->create($file, $hash); $holder->addAttachment($attachment); $this->repository->store($attachment); return $attachment; }
/** * Create new attachment and assign it to the attachment holder * @param string $filename * @param string $content * @param AttachmentHolder $holder * @param bool $flush * @return Attachment */ public function createNewAttachment($filename, $content, AttachmentHolder $holder, $flush = false) { $this->validateFilename($filename); $this->validateContent($content); $filename = urldecode($filename); $filenamePrefix = $this->exposeFilenamePrefixFrom($holder); $path = $this->fileStorageService->upload($filenamePrefix . '/' . $filename, $content); $file = new File($path); $hash = $this->generateFileHash($file); if ($this->isImage($file)) { $this->createThumbnail($file, $hash, $filenamePrefix); } $attachment = $this->factory->create($file, $hash); $holder->addAttachment($attachment); $this->registry->getManager()->persist($attachment); if (true === $flush) { $this->registry->getManager()->flush(); } return $attachment; }
/** * @test */ public function createNewAttachment() { $filename = 'file.ext'; $content = '_some_dummy_content'; $fileRealPath = 'dummy/file/real/path/' . $filename; $attachmentId = 1; $attachment = new AttachmentStub(new File($fileRealPath)); $attachment->setId($attachmentId); $this->fileStorageService->expects($this->once())->method('upload')->with($this->logicalAnd($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING), $this->stringContains($filename)), $this->equalTo($content))->will($this->returnValue($fileRealPath)); $this->factory->expects($this->once())->method('create')->with($this->logicalAnd($this->isInstanceOf('\\Diamante\\DeskBundle\\Model\\Attachment\\File'), $this->callback(function ($other) use($filename) { /** * @var $other \Diamante\DeskBundle\Model\Attachment\File */ return $filename == $other->getFilename(); })))->will($this->returnValue($attachment)); $this->holder->expects($this->once())->method('addAttachment')->with($this->equalTo($attachment)); $this->repository->expects($this->once())->method('store')->with($this->equalTo($attachment)); $createdAttachment = $this->manager->createNewAttachment($filename, $content, $this->holder); $this->assertNotNull($createdAttachment->getId()); $this->assertEquals($attachmentId, $createdAttachment->getId()); }