public function testCopyAttachmentFile() { $localFilePath = __DIR__ . '/../Fixtures/testFile/test.txt'; $sourceStream = new InMemoryBuffer($this->filesystem, $this->attachment->getFilename()); $sourceStream->open(new StreamMode('wb+')); $sourceStream->write(file_get_contents($localFilePath)); $sourceStream->seek(0); $sourceStream->close(); $resultStream = new InMemoryBuffer($this->filesystem, 'test2.txt'); $this->filesystem->expects($this->at(0))->method('createStream')->with($this->attachment->getFilename())->will($this->returnValue($sourceStream)); $this->filesystem->expects($this->at(1))->method('createStream')->with($this->anything())->will($this->returnValue($resultStream)); $newAttachment = $this->attachmentManager->copyAttachmentFile($this->attachment); $this->assertEquals($this->attachment->getOriginalFilename(), $newAttachment->getOriginalFilename()); $this->assertNotEquals($this->attachment->getFilename(), $newAttachment->getFilename()); $resultStream->open(new StreamMode('rb+')); $resultStream->seek(0); $this->assertEquals('Test data', $resultStream->read(100)); }
/** * @param Product $product * @param Product $productCopy */ protected function cloneChildObjects(Product $product, Product $productCopy) { foreach ($product->getUnitPrecisions() as $unitPrecision) { $productCopy->addUnitPrecision(clone $unitPrecision); } if ($imageFile = $product->getImage()) { $imageFileCopy = $this->attachmentManager->copyAttachmentFile($imageFile); $productCopy->setImage($imageFileCopy); } $attachments = $this->attachmentProvider->getEntityAttachments($product); foreach ($attachments as $attachment) { $attachmentCopy = clone $attachment; $attachmentFileCopy = $this->attachmentManager->copyAttachmentFile($attachment->getFile()); $attachmentCopy->setFile($attachmentFileCopy); $attachmentCopy->setTarget($productCopy); $this->doctrineHelper->getEntityManager($attachmentCopy)->persist($attachmentCopy); } }