function it_duplicates_product_media($filesystem, ProductMediaInterface $source, ProductMediaInterface $target, File $newFile) { $target->setFile(Argument::any())->shouldBeCalled(); $source->getOriginalFilename()->willReturn('akeneo.jpg'); $target->getOriginalFilename()->willReturn('akeneo.jpg'); // upload $target->getFile()->willReturn($newFile); $newFile->getPathname()->willReturn('/tmp/tmp-phpspec'); // write a fake file in tmp $adapter = new LocalAdapter('/tmp'); $fs = new Filesystem($adapter); $fs->write('tmp-phpspec', '', true); $source->getFilename()->willReturn('akeneo.jpg'); $newFile->getFilename()->willReturn('akeneo.jpg'); $filesystem->write('prefix-akeneo.jpg', '', false)->shouldBeCalled(); $target->setOriginalFilename('akeneo.jpg')->shouldBeCalled(); $target->setFilename('prefix-akeneo.jpg')->shouldBeCalled(); $filesystem->has('akeneo.jpg')->willReturn(true); $target->getFilename()->willReturn('akeneo.jpg'); $newFile->getMimeType()->willReturn('jpg'); $target->setMimeType('jpg')->shouldBeCalled(); // update original file name $source->getOriginalFilename()->willReturn('akeneo.jpg'); $target->setOriginalFilename('akeneo.jpg')->shouldBeCalled(); $this->duplicate($source, $target, 'prefix'); }
/** * Duplicate a media information into another one * * @param ProductMediaInterface $source * @param ProductMediaInterface $target * @param string $filenamePrefix */ public function duplicate(ProductMediaInterface $source, ProductMediaInterface $target, $filenamePrefix) { if (null === ($path = $this->getFilePath($source))) { throw new \LogicException('File path should not be null'); } $target->setFile(new File($path)); $filename = $this->generateFilename($source->getOriginalFilename(), $filenamePrefix); $this->upload($target, $filename); $target->setOriginalFilename($source->getOriginalFilename()); }