function it_normalizes_a_file_for_versioning(FileInterface $file)
 {
     $file->getKey()->willReturn('key/of/file.txt');
     $file->getOriginalFilename()->willReturn('the file.txt');
     $file->getHash()->willReturn('98s7qf987a6f4sdqf');
     $this->normalize($file)->shouldReturn(['filePath' => 'key/of/file.txt', 'originalFilename' => 'the file.txt', 'hash' => '98s7qf987a6f4sdqf']);
 }
 function it_normalizes_a_file_for_product_export($pathGenerator, ProductValueInterface $value, FileInterface $file)
 {
     $pathGenerator->generate($value, Argument::any())->willReturn('path/to/export/file.txt');
     $file->getKey()->willReturn('key/of/file.txt');
     $file->getStorage()->willReturn(FileStorage::CATALOG_STORAGE_ALIAS);
     $this->normalize($file, null, ['identifier' => null, 'value' => $value, 'field_name' => 'picture'])->shouldReturn(['picture' => 'path/to/export/file.txt']);
 }
 function it_normalizes_media(FileInterface $file)
 {
     $file->getKey()->willReturn('key/of/the/file.pdf');
     $file->getOriginalFilename()->willReturn('myfile.pdf');
     $file->getId()->willReturn(152);
     $this->normalize($file, 'mongodb_json', [])->shouldReturn(['id' => 152, 'key' => 'key/of/the/file.pdf', 'originalFilename' => 'myfile.pdf']);
 }
 function it_throws_an_exception_when_the_file_can_not_be_read_on_the_filesystem(FileInterface $file, FilesystemInterface $filesystem)
 {
     $file->getKey()->willReturn('path/to/file.txt');
     $filesystem->has('path/to/file.txt')->willReturn(true);
     $filesystem->readStream('path/to/file.txt')->willReturn(false);
     $this->shouldThrow(new FileTransferException('Unable to fetch the file "path/to/file.txt" from the filesystem.'))->during('fetch', [$file, $filesystem]);
 }
 public function it_throws_an_exception_if_the_file_can_not_be_moved($mountManager, $saver, FileInterface $file)
 {
     $file->getKey()->willReturn('path/to/file.txt');
     $mountManager->move('source://path/to/file.txt', 'destination://path/to/file.txt')->willReturn(false);
     $file->setStorage(Argument::any())->shouldNotBeCalled();
     $saver->save(Argument::any())->shouldNotBeCalled();
     $this->shouldThrow(new FileTransferException('Impossible to move the file "path/to/file.txt" from "source" to "destination".'))->during('move', [$file, 'source', 'destination']);
 }
 /**
  * Validate if file size is allowed.
  *
  * @param FileInterface $file
  * @param Constraint    $constraint
  */
 protected function validateFileSize(FileInterface $file, Constraint $constraint)
 {
     // comes from Symfony\Component\Validator\Constraints\FileValidator
     if ($constraint->maxSize) {
         $limitInBytes = $constraint->maxSize;
         if ($file->getSize() > $limitInBytes) {
             $factorizeSizes = $this->factorizeSizes($file->getSize(), $limitInBytes, $constraint->binaryFormat);
             list($sizeAsString, $limitAsString, $suffix) = $factorizeSizes;
             $this->context->buildViolation($constraint->maxSizeMessage)->setParameter('{{ file }}', $this->formatValue($file->getOriginalFilename()))->setParameter('{{ size }}', $sizeAsString)->setParameter('{{ limit }}', $limitAsString)->setParameter('{{ suffix }}', $suffix)->setCode(File::TOO_LARGE_ERROR)->addViolation();
             return;
         }
     }
 }
 function it_validates_empty_extension_and_size($context, File $constraint, FileInterface $file)
 {
     $constraint->allowedExtensions = [];
     $file->getId()->willReturn(12);
     $file->getUploadedFile()->willReturn(null);
     $file->getExtension()->willReturn('jpg');
     $file->getSize()->willReturn(100);
     $context->buildViolation(Argument::any())->shouldNotBeCalled();
     $this->validate($file, $constraint);
 }
 function it_returns_flat_data_with_media($channelManager, Filesystem $filesystem, ChannelInterface $channel, ProductInterface $product, FileInterface $media1, FileInterface $media2, ProductValueInterface $value1, ProductValueInterface $value2, AttributeInterface $attribute, ProductValueInterface $identifierValue, AttributeInterface $identifierAttribute, $serializer)
 {
     $media1->getKey()->willReturn('key/to/media1.jpg');
     $media2->getKey()->willReturn('key/to/media2.jpg');
     $value1->getAttribute()->willReturn($attribute);
     $value1->getMedia()->willReturn($media1);
     $value2->getAttribute()->willReturn($attribute);
     $value2->getMedia()->willReturn($media2);
     $attribute->getAttributeType()->willReturn('pim_catalog_image');
     $product->getValues()->willReturn([$value1, $value2, $identifierValue]);
     $identifierValue->getAttribute()->willReturn($identifierAttribute);
     $identifierAttribute->getAttributeType()->willReturn('pim_catalog_identifier');
     $product->getIdentifier()->willReturn($identifierValue);
     $identifierValue->getData()->willReturn('data');
     $filesystem->has('key/to/media1.jpg')->willReturn(true);
     $filesystem->has('key/to/media2.jpg')->willReturn(true);
     $serializer->normalize($media1, 'flat', ['field_name' => 'media', 'prepare_copy' => true, 'value' => $value1])->willReturn(['normalized_media1']);
     $serializer->normalize($media2, 'flat', ['field_name' => 'media', 'prepare_copy' => true, 'value' => $value2])->willReturn(['normalized_media2']);
     $serializer->normalize($product, 'flat', ['scopeCode' => 'foobar', 'localeCodes' => ''])->willReturn(['normalized_product']);
     $channelManager->getChannelByCode('foobar')->willReturn($channel);
     $this->setChannel('foobar');
     $this->process($product)->shouldReturn(['media' => [['normalized_media1'], ['normalized_media2']], 'product' => ['normalized_product']]);
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function move(FileInterface $file, $srcFsAlias, $destFsAlias)
 {
     $isFileMoved = $this->mountManager->move(sprintf('%s://%s', $srcFsAlias, $file->getKey()), sprintf('%s://%s', $destFsAlias, $file->getKey()));
     if (!$isFileMoved) {
         throw new FileTransferException(sprintf('Impossible to move the file "%s" from "%s" to "%s".', $file->getKey(), $srcFsAlias, $destFsAlias));
     }
     $file->setStorage($destFsAlias);
     $this->saver->save($file);
 }
 /**
  * {@inheritdoc}
  */
 public function fetch(FileInterface $file, FilesystemInterface $filesystem)
 {
     if (!$filesystem->has($file->getKey())) {
         throw new \LogicException(sprintf('The file "%s" is not present on the filesystem.', $file->getKey()));
     }
     $localPathname = tempnam(sys_get_temp_dir(), 'raw_file_fetcher_');
     if (false === ($stream = $filesystem->readStream($file->getKey()))) {
         throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $file->getKey()));
     }
     if (false === file_put_contents($localPathname, $stream)) {
         throw new FileTransferException(sprintf('Unable to fetch the file "%s" from the filesystem.', $file->getKey()));
     }
     return new \SplFileInfo($localPathname);
 }
 function it_returns_null_when_there_is_no_diff($repository, FileInterface $file)
 {
     $file->getHash()->willReturn(sha1_file(__FILE__));
     $repository->findOneByIdentifier('key/of/my/original/file.txt')->willReturn($file);
     $this->compare(['data' => ['filePath' => __FILE__]], ['data' => ['filePath' => 'key/of/my/original/file.txt']])->shouldReturn(null);
 }
 function it_normalizes_a_file(FileInterface $file)
 {
     $file->getKey()->willReturn('key/of/file.txt');
     $file->getOriginalFilename()->willReturn('original file name.txt');
     $this->normalize($file, Argument::cetera())->shouldReturn(['filePath' => 'key/of/file.txt', 'originalFilename' => 'original file name.txt']);
 }