function it_fails_if_the_group_code_is_not_found($groupRepository, ProductInterface $product, GroupInterface $pack, GroupTypeInterface $nonVariantType)
 {
     $groupRepository->findOneByIdentifier('not valid code')->willReturn(null);
     $pack->getType()->willReturn($nonVariantType);
     $nonVariantType->isVariant()->willReturn(false);
     $this->shouldThrow(InvalidArgumentException::expected('variant_group', 'existing variant group code', 'setter', 'variant_group', 'not valid code'))->during('setFieldData', [$product, 'variant_group', 'not valid code']);
 }
 function it_fails_if_one_of_the_associated_group_does_not_exist($productBuilder, $groupRepository, ProductInterface $product, AssociationInterface $xsellAssociation)
 {
     $product->getAssociations()->willReturn([$xsellAssociation]);
     $productBuilder->addMissingAssociations($product)->shouldBeCalled();
     $product->getAssociationForTypeCode('xsell')->willReturn($xsellAssociation);
     $groupRepository->findOneByIdentifier('not existing group')->willReturn(null);
     $this->shouldThrow(InvalidArgumentException::expected('associations', 'existing group code', 'adder', 'association', 'not existing group'))->during('addFieldData', [$product, 'associations', ['xsell' => ['groups' => ['not existing group'], 'products' => []]]]);
 }
 /**
  * {@inheritdoc}
  */
 public function addFieldFilter($field, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     if (!is_numeric($value) && !is_array($value)) {
         throw InvalidArgumentException::expected($field, 'array or numeric value', 'filter', 'productId', $value);
     }
     $field = current($this->qb->getRootAliases()) . '.' . $field;
     $condition = $this->prepareCriteriaCondition($field, $operator, $value);
     $this->qb->andWhere($condition);
     return $this;
 }
 function it_fails_if_the_group_code_does_not_correspond_to_a_simple_group($groupRepository, ProductInterface $product, GroupInterface $pack, GroupInterface $variant, GroupTypeInterface $nonVariantType, GroupTypeInterface $variantType)
 {
     $groupRepository->findOneByIdentifier('pack')->willReturn($pack);
     $pack->getType()->willReturn($nonVariantType);
     $nonVariantType->isVariant()->willReturn(false);
     $groupRepository->findOneByIdentifier('variant')->willReturn($variant);
     $variant->getType()->willReturn($variantType);
     $variantType->isVariant()->willReturn(true);
     $this->shouldThrow(InvalidArgumentException::expected('groups', 'non variant group code', 'remover', 'groups', 'variant'))->during('removeFieldData', [$product, 'groups', ['pack', 'variant']]);
 }
 /**
  * {@inheritdoc}
  */
 public function addFieldFilter($field, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     if (!is_string($value) && !is_array($value)) {
         throw InvalidArgumentException::expected($field, 'array or string value', 'filter', 'productId', $value);
     }
     $field = '_id';
     $value = is_array($value) ? $value : [$value];
     $this->applyFilter($value, $field, $operator);
     return $this;
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : "family_code"
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     if (null !== $data && '' !== $data) {
         $family = $this->getFamily($data);
         if (null === $family) {
             throw InvalidArgumentException::expected($field, 'existing family code', 'setter', 'family', $data);
         }
         $product->setFamily($family);
     } else {
         $product->setFamily(null);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["category_code", "another_category_code"]
  */
 public function removeFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $categories = [];
     foreach ($data as $categoryCode) {
         $category = $this->categoryRepository->findOneByIdentifier($categoryCode);
         if (null === $category) {
             throw InvalidArgumentException::expected($field, 'existing category code', 'remover', 'category', $categoryCode);
         }
         $categories[] = $category;
     }
     foreach ($categories as $categoryToRemove) {
         $product->removeCategory($categoryToRemove);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["group_code"]
  */
 public function addFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $groups = [];
     foreach ($data as $groupCode) {
         $group = $this->groupRepository->findOneByIdentifier($groupCode);
         if (null === $group) {
             throw InvalidArgumentException::expected($field, 'existing group code', 'adder', 'groups', $groupCode);
         } elseif ($group->getType()->isVariant()) {
             throw InvalidArgumentException::expected($field, 'non variant group code', 'adder', 'groups', $groupCode);
         } else {
             $groups[] = $group;
         }
     }
     foreach ($groups as $group) {
         $product->addGroup($group);
     }
 }
 /**
  * Override to add new operator and to work on time
  * {@inheritdoc}
  */
 public function addFieldFilter($field, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     if (static::GREATER_THAN_OR_EQUALS_WITH_TIME === $operator) {
         if ($value instanceof \DateTime) {
             $dateTimeValue = $value;
         } else {
             try {
                 $dateTimeValue = new \DateTime($value);
             } catch (\Exception $e) {
                 throw InvalidArgumentException::expected($field, 'DateTime object or new DateTime() compatible string. Error:' . $e->getMessage(), 'filter', 'date_time', is_string($value) ? $value : gettype($value));
             }
         }
         $normalizedField = sprintf('%s.%s', ProductQueryUtility::NORMALIZED_FIELD, $field);
         $this->qb->field($normalizedField)->gte($dateTimeValue->getTimestamp());
     } else {
         parent::addFieldFilter($field, $operator, $value, $locale, $scope, $options);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["group_code"]
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $groups = [];
     foreach ($data as $groupCode) {
         $group = $this->groupRepository->findOneByIdentifier($groupCode);
         if (null === $group) {
             throw InvalidArgumentException::expected($field, 'existing group code', 'setter', 'groups', $groupCode);
         } else {
             $groups[] = $group;
         }
     }
     $oldGroups = $product->getGroups();
     foreach ($oldGroups as $group) {
         $product->removeGroup($group);
     }
     foreach ($groups as $group) {
         $product->addGroup($group);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : ["category_code"]
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     $categories = [];
     foreach ($data as $categoryCode) {
         $category = $this->getCategory($categoryCode);
         if (null === $category) {
             throw InvalidArgumentException::expected($field, 'existing category code', 'setter', 'category', $categoryCode);
         } else {
             $categories[] = $category;
         }
     }
     $oldCategories = $product->getCategories();
     foreach ($oldCategories as $category) {
         $product->removeCategory($category);
     }
     foreach ($categories as $category) {
         $product->addCategory($category);
     }
 }
 /**
  * Override to add new operator and to work on time
  * {@inheritdoc}
  */
 public function addFieldFilter($field, $operator, $value, $locale = null, $scope = null, $options = [])
 {
     if (static::GREATER_THAN_OR_EQUALS_WITH_TIME === $operator) {
         if ($value instanceof \DateTime) {
             $dateTimeValue = $value;
         } else {
             try {
                 $dateTimeValue = new \DateTime($value);
             } catch (\Exception $e) {
                 throw InvalidArgumentException::expected($field, 'DateTime object or new DateTime() compatible string. Error:' . $e->getMessage(), 'filter', 'date_time', is_string($value) ? $value : gettype($value));
             }
         }
         $field = current($this->qb->getRootAliases()) . '.' . $field;
         $utcDateTimeValue = new \DateTime();
         $utcDateTimeValue->setTimezone(new \DateTimeZone('Etc/UTC'));
         $utcDateTimeValue->setTimestamp($dateTimeValue->getTimestamp());
         $this->qb->andWhere($this->qb->expr()->gte($field, $this->qb->expr()->literal($utcDateTimeValue->format('Y-m-d H:i:s'))));
     } else {
         parent::addFieldFilter($field, $operator, $value, $locale, $scope, $options);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Expected data input format : "variant_group_code"
  */
 public function setFieldData(ProductInterface $product, $field, $data, array $options = [])
 {
     $this->checkData($field, $data);
     if (null !== $data) {
         $variantGroup = $this->groupRepository->findOneByIdentifier($data);
         if (null === $variantGroup) {
             throw InvalidArgumentException::expected($field, 'existing variant group code', 'setter', 'variant_group', $data);
         }
         if (!$variantGroup->getType()->isVariant()) {
             throw InvalidArgumentException::expected($field, 'variant group code', 'setter', 'variant_group', $data);
         }
     }
     $existingGroups = $product->getGroups();
     foreach ($existingGroups as $group) {
         if ($group->getType()->isVariant()) {
             $product->removeGroup($group);
         }
     }
     if (null !== $data) {
         $product->addGroup($variantGroup);
     }
 }
 function it_throws_an_error_if_attribute_data_is_not_a_valid_path(AttributeInterface $attribute, ProductInterface $product)
 {
     $attribute->getCode()->willReturn('attributeCode');
     $data = ['filePath' => 'path/to/unknown/file', 'originalFilename' => 'image'];
     $this->shouldThrow(InvalidArgumentException::expected('attributeCode', 'a valid pathname', 'setter', 'media', 'path/to/unknown/file'))->during('setAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]);
 }
 function it_throws_an_exception_if_value_is_not_a_numeric_or_an_array()
 {
     $this->shouldThrow(InvalidArgumentException::expected('id', 'array or string value', 'filter', 'productId', 1234))->during('addFieldFilter', ['id', '=', 1234]);
 }
 /**
  * Check if the date format is valid
  *
  * @param string $type
  * @param string $value
  */
 protected function validateDateFormat($type, $value)
 {
     $dateValues = explode('-', $value);
     if (count($dateValues) !== 3 || (!is_numeric($dateValues[0]) || !is_numeric($dateValues[1]) || !is_numeric($dateValues[2])) || !checkdate($dateValues[1], $dateValues[2], $dateValues[0])) {
         throw InvalidArgumentException::expected($type, 'a string with the format yyyy-mm-dd', 'filter', 'date', $value);
     }
 }
 function it_throws_an_exception_if_value_is_an_array_but_does_not_contain_two_values()
 {
     $this->shouldThrow(InvalidArgumentException::expected('release_date', 'array with 2 elements, string or \\Datetime', 'filter', 'date', print_r([123, 123, 'three'], true)))->during('addFieldFilter', ['release_date', '>', [123, 123, 'three']]);
 }
 /**
  * @param AttributeInterface $attribute
  * @param string             $data
  */
 protected function validateDateFormat(AttributeInterface $attribute, $data)
 {
     $dateValues = explode('-', $data);
     if (count($dateValues) !== 3 || (!is_numeric($dateValues[0]) || !is_numeric($dateValues[1]) || !is_numeric($dateValues[2])) || !checkdate($dateValues[1], $dateValues[2], $dateValues[0])) {
         throw InvalidArgumentException::expected($attribute->getCode(), 'a string with the format yyyy-mm-dd', 'setter', 'date', gettype($data), $data);
     }
 }
 function it_throws_an_error_if_attribute_data_is_not_a_string_or_datetime_or_null(AttributeInterface $attribute, ProductInterface $product)
 {
     $attribute->getCode()->willReturn('attributeCode');
     $data = 132654;
     $this->shouldThrow(InvalidArgumentException::expected('attributeCode', 'datetime or string', gettype($data), 'setter', 'date'))->during('setAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]);
 }
 /**
  * @param AttributeInterface $attribute
  * @param mixed              $data
  *
  * @throws \Pim\Bundle\CatalogBundle\Exception\InvalidArgumentException If an invalid filePath is provided
  *
  * @return File|null
  */
 protected function getFileData(AttributeInterface $attribute, $data)
 {
     if (null === $data || null === $data['filePath'] && null === $data['originalFilename']) {
         return null;
     }
     $data = $this->resolveFilePath($data);
     try {
         return new File($data['filePath']);
     } catch (FileNotFoundException $e) {
         throw InvalidArgumentException::expected($attribute->getCode(), 'a valid file path', 'setter', 'media', $data['filePath']);
     }
 }
 /**
  * TODO: inform the user that this could take some time
  *
  * @param AttributeInterface $attribute
  * @param mixed              $data
  *
  * @throws InvalidArgumentException If an invalid filePath is provided
  *
  * @return FileInfoInterface|null
  */
 protected function storeFile(AttributeInterface $attribute, $data)
 {
     if (null === $data || null === $data['filePath'] && null === $data['originalFilename']) {
         return null;
     }
     try {
         $rawFile = new UploadedFile($data['filePath'], $data['originalFilename']);
         $file = $this->storer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS);
     } catch (FileNotFoundException $e) {
         throw InvalidArgumentException::expected($attribute->getCode(), 'a valid pathname', 'setter', 'media', $data['filePath']);
     }
     return $file;
 }
 function it_throws_an_error_if_data_is_not_a_valid_path(AttributeInterface $attribute)
 {
     $attribute->getCode()->willReturn('attributeCode');
     $data = ['filePath' => 'path/to/unknown/file', 'originalFilename' => 'image'];
     $this->shouldThrow(InvalidArgumentException::expected('attributeCode', 'a valid file path', 'setter', 'media', '../../../../../../app/uploads/product/path/to/unknown/file'))->during('setValue', [[], $attribute, $data, 'fr_FR', 'mobile']);
 }
 /**
  * @param AssociationInterface $association
  * @param array                $groupsCodes
  */
 protected function addAssociatedGroups(AssociationInterface $association, $groupsCodes)
 {
     foreach ($groupsCodes as $groupCode) {
         $associatedGroup = $this->groupRepository->findOneByIdentifier($groupCode);
         if (null === $associatedGroup) {
             throw InvalidArgumentException::expected('associations', 'existing group code', 'adder', 'association', $groupCode);
         }
         $association->addGroup($associatedGroup);
     }
 }
 function it_fails_if_the_family_code_is_not_a_valid_family_code($familyRepository, ProductInterface $product)
 {
     $familyRepository->findOneByIdentifier('shirt')->willReturn(null);
     $this->shouldThrow(InvalidArgumentException::expected('family', 'existing family code', 'setter', 'family', 'shirt'))->during('setFieldData', [$product, 'family', 'shirt']);
 }
 function it_fails_if_one_of_the_category_code_does_not_exist($categoryRepository, ProductInterface $product, CategoryInterface $mug, CategoryInterface $shirt)
 {
     $categoryRepository->findOneByIdentifier('mug')->willReturn($mug);
     $categoryRepository->findOneByIdentifier('non valid category code')->willReturn(null);
     $this->shouldThrow(InvalidArgumentException::expected('categories', 'existing category code', 'setter', 'category', 'non valid category code'))->during('setFieldData', [$product, 'categories', ['mug', 'non valid category code']]);
 }
 function it_throws_an_error_if_data_is_not_a_string_or_datetime_or_null(AttributeInterface $attribute)
 {
     $attribute->getCode()->willReturn('attributeCode');
     $data = 132654;
     $this->shouldThrow(InvalidArgumentException::expected('attributeCode', 'datetime or string', gettype($data), 'setter', 'date'))->during('setValue', [[], $attribute, $data, 'fr_FR', 'mobile']);
 }