function it_normalizes_a_file_for_versioning(FileInfoInterface $fileInfo)
 {
     $fileInfo->getKey()->willReturn('key/of/file.txt');
     $fileInfo->getOriginalFilename()->willReturn('the file.txt');
     $fileInfo->getHash()->willReturn('98s7qf987a6f4sdqf');
     $this->normalize($fileInfo)->shouldReturn(['filePath' => 'key/of/file.txt', 'originalFilename' => 'the file.txt', 'hash' => '98s7qf987a6f4sdqf']);
 }
 function it_normalizes_media(FileInfoInterface $fileInfo)
 {
     $fileInfo->getKey()->willReturn('key/of/the/file.pdf');
     $fileInfo->getOriginalFilename()->willReturn('myfile.pdf');
     $fileInfo->getId()->willReturn(152);
     $this->normalize($fileInfo, 'mongodb_json', [])->shouldReturn(['id' => 152, 'key' => 'key/of/the/file.pdf', 'originalFilename' => 'myfile.pdf']);
 }
 function it_allows_to_get_errors_if_the_copy_went_wrong($mediaFetcher, $filesystemProvider, $fileExporterPath, FileInfoInterface $fileInfo, FileInfoInterface $fileInfo2, ArrayCollection $productValuesCollection, \ArrayIterator $valuesIterator, ProductValueInterface $productValue, ProductValueInterface $productValue2, AttributeInterface $attribute, FilesystemInterface $filesystem)
 {
     $fileInfo->getStorage()->willReturn('storageAlias');
     $fileInfo->getKey()->willReturn('a/b/c/d/product.jpg');
     $fileInfo->getOriginalFilename()->willReturn('my product.jpg');
     $fileInfo2->getStorage()->willReturn('storageAlias');
     $fileInfo2->getKey()->willReturn('wrong-path.jpg');
     $fileInfo2->getOriginalFilename()->willReturn('my-second-media.jpg');
     $productValue->getAttribute()->willReturn($attribute);
     $productValue->getMedia()->willReturn($fileInfo);
     $productValue->getLocale()->willReturn('en_US');
     $productValue->getScope()->willReturn(null);
     $productValue2->getAttribute()->willReturn($attribute);
     $productValue2->getMedia()->willReturn($fileInfo2);
     $productValue2->getLocale()->willReturn('fr_FR');
     $productValue2->getScope()->willReturn('ecommerce');
     $attribute->getAttributeType()->willReturn('pim_catalog_image');
     $attribute->getCode()->willReturn('my_picture');
     $productValuesCollection->getIterator()->willReturn($valuesIterator);
     $valuesIterator->rewind()->shouldBeCalled();
     $valuesCount = 2;
     $valuesIterator->valid()->will(function () use(&$valuesCount) {
         return $valuesCount-- > 0;
     });
     $valuesIterator->next()->shouldBeCalled();
     $valuesIterator->current()->will(new ReturnPromise([$productValue, $productValue2]));
     $filesystemProvider->getFilesystem('storageAlias')->willReturn($filesystem);
     $mediaFetcher->fetch($filesystem, 'a/b/c/d/product.jpg', ['filePath' => $this->directory . 'files/the_sku/my_picture/en_US/', 'filename' => 'my product.jpg'])->willThrow(new FileTransferException());
     $fileExporterPath->generate(['locale' => 'en_US', 'scope' => null], ['identifier' => 'the_sku', 'code' => 'my_picture'])->willReturn('files/the_sku/my_picture/en_US/');
     $mediaFetcher->fetch($filesystem, 'wrong-path.jpg', ['filePath' => $this->directory . 'files/the_sku/my_picture/fr_FR/ecommerce/', 'filename' => 'my-second-media.jpg'])->willThrow(new \LogicException('Something went wrong.'));
     $fileExporterPath->generate(['locale' => 'fr_FR', 'scope' => 'ecommerce'], ['identifier' => 'the_sku', 'code' => 'my_picture'])->willReturn('files/the_sku/my_picture/fr_FR/ecommerce/');
     $this->fetchAll($productValuesCollection, $this->directory, 'the_sku');
     $this->getErrors()->shouldBeEqualTo([['message' => 'The media has not been found or is not currently available', 'media' => ['from' => 'a/b/c/d/product.jpg', 'to' => ['filePath' => $this->directory . 'files/the_sku/my_picture/en_US/', 'filename' => 'my product.jpg'], 'storage' => 'storageAlias']], ['message' => 'The media has not been copied. Something went wrong.', 'media' => ['from' => 'wrong-path.jpg', 'to' => ['filePath' => $this->directory . 'files/the_sku/my_picture/fr_FR/ecommerce/', 'filename' => 'my-second-media.jpg'], 'storage' => 'storageAlias']]]);
 }
 /**
  * Validate if file size is allowed.
  *
  * @param FileInfoInterface $fileInfo
  * @param Constraint        $constraint
  */
 protected function validateFileSize(FileInfoInterface $fileInfo, Constraint $constraint)
 {
     // comes from Symfony\Component\Validator\Constraints\FileValidator
     if ($constraint->maxSize) {
         $limitInBytes = $constraint->maxSize;
         if ($fileInfo->getSize() > $limitInBytes) {
             $factorizeSizes = $this->factorizeSizes($fileInfo->getSize(), $limitInBytes, $constraint->binaryFormat);
             list($sizeAsString, $limitAsString, $suffix) = $factorizeSizes;
             $this->context->buildViolation($constraint->maxSizeMessage)->setParameter('{{ file }}', $this->formatValue($fileInfo->getOriginalFilename()))->setParameter('{{ size }}', $sizeAsString)->setParameter('{{ limit }}', $limitAsString)->setParameter('{{ suffix }}', $suffix)->setCode(File::TOO_LARGE_ERROR)->addViolation();
             return;
         }
     }
 }
 function it_does_not_validate_size($context, File $constraint, FileInfoInterface $fileInfo, ConstraintViolationBuilderInterface $violation)
 {
     $constraint->maxSize = '1M';
     $fileInfo->getId()->willReturn(12);
     $fileInfo->getUploadedFile()->willReturn(null);
     $fileInfo->getExtension()->willReturn('jpg');
     $fileInfo->getSize()->willReturn(1075200);
     $fileInfo->getOriginalFilename()->willReturn('my file.jpg');
     $context->buildViolation($constraint->maxSizeMessage)->shouldBeCalled()->willReturn($violation);
     $violation->setParameter('{{ file }}', Argument::any())->shouldBeCalled()->willReturn($violation);
     $violation->setParameter('{{ size }}', Argument::any())->shouldBeCalled()->willReturn($violation);
     $violation->setParameter('{{ limit }}', Argument::any())->shouldBeCalled()->willReturn($violation);
     $violation->setParameter('{{ suffix }}', Argument::any())->shouldBeCalled()->willReturn($violation);
     $violation->setCode(Argument::any())->shouldBeCalled()->willReturn($violation);
     $violation->addViolation()->shouldBeCalled();
     $this->validate($fileInfo, $constraint);
 }
 function it_normalizes_a_file(FileInfoInterface $fileInfo)
 {
     $fileInfo->getKey()->willReturn('key/of/file.txt');
     $fileInfo->getOriginalFilename()->willReturn('original file name.txt');
     $this->normalize($fileInfo, Argument::cetera())->shouldReturn(['filePath' => 'key/of/file.txt', 'originalFilename' => 'original file name.txt']);
 }