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']]]);
 }
 function it_returns_flat_data_with_media($channelManager, Filesystem $filesystem, ChannelInterface $channel, LocaleInterface $locale, ProductInterface $product, FileInfoInterface $media1, FileInfoInterface $media2, ProductValueInterface $value1, ProductValueInterface $value2, AttributeInterface $attribute, ProductValueInterface $identifierValue, AttributeInterface $identifierAttribute, $serializer, $productBuilder)
 {
     $localeCodes = ['en_US'];
     $channel->getLocales()->willReturn(new ArrayCollection([$locale]));
     $channel->getLocaleCodes()->willReturn($localeCodes);
     $productBuilder->addMissingProductValues($product, [$channel], [$locale])->shouldBeCalled();
     $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' => $localeCodes, 'decimal_separator' => '.', 'date_format' => 'yyyy-MM-dd'])->willReturn(['normalized_product']);
     $channelManager->getChannelByCode('foobar')->willReturn($channel);
     $this->setChannel('foobar');
     $this->process($product)->shouldReturn(['media' => [['normalized_media1'], ['normalized_media2']], 'product' => ['normalized_product']]);
 }
 /**
  * {@inheritdoc}
  */
 public function isComplete(ProductValueInterface $productValue, ChannelInterface $channel = null, LocaleInterface $locale = null)
 {
     $media = $productValue->getMedia();
     if (!$media || '' === $media->__toString()) {
         return false;
     }
     return true;
 }
 function it_generates_the_path_when_the_value_is_localisable_and_scopable(ProductValueInterface $value, FileInfoInterface $fileInfo, AttributeInterface $attribute)
 {
     $value->getMedia()->willReturn($fileInfo);
     $value->getLocale()->willReturn('fr_FR');
     $value->getScope()->willReturn('ecommerce');
     $value->getAttribute()->willReturn($attribute);
     $fileInfo->getOriginalFilename()->willReturn('file.jpg');
     $attribute->getCode()->willReturn('picture');
     $attribute->isLocalizable()->willReturn(true);
     $attribute->isScopable()->willReturn(true);
     $this->generate($value, ['identifier' => 'sku001'])->shouldReturn('files/sku001/picture/fr_FR/ecommerce/file.jpg');
 }
 function it_updates_normalized_product_template_values_if_media_values_have_been_handled($normalizer, ProductTemplateInterface $imageTemplate, ProductTemplateInterface $textTemplate, ProductValueInterface $imageValue, ProductValueInterface $textValue, FileInfoInterface $imageMedia)
 {
     $normalizer->normalize(Argument::cetera())->willReturn([]);
     $imageTemplate->getValues()->willReturn([$imageValue]);
     $imageValue->getMedia()->willReturn($imageMedia);
     $imageMedia->isRemoved()->willReturn(true);
     $imageValue->setMedia(null)->shouldBeCalled();
     $imageTemplate->setValuesData([])->shouldBeCalled();
     $textTemplate->getValues()->willReturn([$textValue]);
     $textTemplate->setValuesData([])->shouldNotBeCalled();
     $this->handleProductTemplateMedia($imageTemplate);
     $this->handleProductTemplateMedia($textTemplate);
 }
 public function it_succesfully_checks_complete_media(ProductValueInterface $value, ChannelInterface $channel, LocaleInterface $locale, ProductMediaInterface $media)
 {
     $value->getMedia()->willReturn(null);
     $this->isComplete($value, $channel, $locale)->shouldReturn(false);
     $value->getMedia()->willReturn([]);
     $this->isComplete($value, $channel, $locale)->shouldReturn(false);
     $media->__toString()->willReturn('');
     $value->getMedia()->willReturn($media);
     $this->isComplete($value, $channel, $locale)->shouldReturn(false);
     $media->__toString()->willReturn('other');
     $value->getMedia()->willReturn($media);
     $this->isComplete($value, $channel, $locale)->shouldReturn(true);
 }
 /**
  * Check if the media is not empty
  *
  * @param ProductValueInterface $value
  * @param Constraint            $constraint
  */
 protected function validateMedia(ProductValueInterface $value, Constraint $constraint)
 {
     $media = $value->getMedia();
     if (!$media || $media->__toString() === '' || $media->__toString() === null) {
         $this->context->buildViolation($constraint->messageComplete)->addViolation();
     }
 }
 function it_copies_when_a_product_value_has_a_media_but_not_the_target_value($builder, $attrValidatorHelper, FileInfoInterface $fromMedia, FileInfoInterface $toMedia, \SplFileInfo $rawFile, FileInfoInterface $fileInfo, FileInfoInterface $fromMedia, FileInfoInterface $toMedia, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, ProductInterface $product, ProductValueInterface $fromProductValue, FileStorerInterface $fileStorer, ProductValueInterface $toProductValue, FileFetcherInterface $fileFetcher, MountManager $mountManager, FilesystemInterface $fileSystem)
 {
     $fromLocale = 'fr_FR';
     $toLocale = 'fr_FR';
     $toScope = 'mobile';
     $fromScope = 'mobile';
     $fromAttribute->getCode()->willReturn('fromAttributeCode');
     $toAttribute->getCode()->willReturn('toAttributeCode');
     $attrValidatorHelper->validateLocale(Argument::cetera())->shouldBeCalled();
     $attrValidatorHelper->validateScope(Argument::cetera())->shouldBeCalled();
     $fromProductValue->getMedia()->willReturn($fromMedia);
     $fromMedia->getOriginalFilename()->willReturn('akeneo.jpg');
     $fromMedia->getKey()->willReturn('key');
     $mountManager->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS)->willReturn($fileSystem);
     $fileFetcher->fetch($fileSystem, 'key')->willReturn($rawFile);
     $fileStorer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS, false)->willReturn($fileInfo);
     $product->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue);
     $product->getValue('toAttributeCode', $toLocale, $toScope)->willReturn(null);
     $builder->addProductValue($product, $toAttribute, $toLocale, $toScope)->willReturn($toProductValue);
     $toProductValue->getMedia()->willReturn($toMedia);
     $toProductValue->setMedia($fileInfo)->shouldBeCalled();
     $this->copyAttributeData($product, $product, $fromAttribute, $toAttribute, ['from_locale' => $fromLocale, 'to_locale' => $toLocale, 'from_scope' => $fromScope, 'to_scope' => $toScope]);
 }
 function it_does_not_validate_a_product_value_with_backendtype_as_media($context, ProductValueComplete $constraint, ProductValueInterface $productValue, AttributeInterface $attribute, ConstraintViolationBuilderInterface $violation)
 {
     $constraint->getChannel()->willReturn($this->getChannel());
     $productMedia = new ProductMedia();
     $productValue->getMedia()->willReturn($productMedia);
     $productValue->getData()->willReturn('data');
     $attribute->getBackendType()->willReturn('media');
     $productValue->getAttribute()->willReturn($attribute);
     $context->buildViolation($constraint->messageComplete)->shouldBeCalled()->willReturn($violation);
     $this->validate($productValue, $constraint);
 }