function it_uploads_without_removing(MediaInterface $media, Filesystem $filesystem, UploadedFile $file) { $media->hasMedia()->shouldBeCalled()->willReturn(true); $media->getName()->shouldBeCalled()->willReturn(null); $media->getMedia()->shouldBeCalled()->willReturn($file); $file->guessExtension()->shouldBeCalled()->willReturn('md'); $filesystem->has(Argument::containingString('.md'))->shouldBeCalled()->willReturn(false); $media->setName(Argument::any())->shouldBeCalled()->willReturn($media); $media->getMedia()->shouldBeCalled()->willReturn($file); $file->getPathname()->shouldBeCalled()->willReturn(__DIR__ . '/../../../../../README.md'); $filesystem->write(Argument::any(), file_get_contents(__DIR__ . '/../../../../../README.md'))->shouldBeCalled()->willReturn(Argument::type('int')); $this->upload($media); }
/** * {@inheritdoc} */ public function upload(MediaInterface $media, $name = null, $test = false) { if (!$media->hasMedia()) { return; } if (null !== $media->getName()) { $this->remove($media->getName()); } do { $hash = md5(uniqid(mt_rand(), true)); if ($test === false) { $name = $hash . '.' . $media->getMedia()->guessExtension(); } } while ($this->filesystem->has($name)); $media->setName($name); if ($test === false) { $this->filesystem->write($media->getName(), file_get_contents($media->getMedia()->getPathname())); } }