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_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; }
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_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; }
/** * {@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); } }
/** * {@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); } }
/** * {@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 : "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_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']]); }
function it_throws_an_exception_if_value_is_an_array_but_does_not_contain_two_values(AttributeInterface $attribute) { $attribute->getCode()->willReturn('release_date'); $this->shouldThrow(InvalidArgumentException::expected('release_date', 'array with 2 elements, string or \\DateTime', 'filter', 'date', print_r([123, 123, 'three'], true)))->during('addAttributeFilter', [$attribute, '>', [123, 123, 'three']]); }
/** * 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_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', 'adder', 'category', 'non valid category code'))->during('addFieldData', [$product, 'categories', ['mug', 'non valid category code']]); }
/** * @param string $type * @param mixed $value * * @throws InvalidArgumentException * * @return string */ protected function formatSingleValue($type, $value) { if (null === $value) { return $value; } if ($value instanceof \DateTime) { return $value->format(static::DATETIME_FORMAT); } if (is_string($value)) { $dateTime = \DateTime::createFromFormat(static::DATETIME_FORMAT, $value); if (!$dateTime || 0 < $dateTime->getLastErrors()['warning_count']) { throw InvalidArgumentException::expected($type, 'a string with the format yyyy-mm-dd', 'filter', 'date', $value); } return $dateTime->format(static::DATETIME_FORMAT); } throw InvalidArgumentException::expected($type, 'array with 2 elements, string or \\DateTime', 'filter', 'date', print_r($value, true)); }
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]); }
/** * @param AssociationInterface $association * @param array $groupsCodes */ protected function setAssociatedGroups(AssociationInterface $association, $groupsCodes) { foreach ($groupsCodes as $groupCode) { $associatedGroup = $this->groupRepository->findOneByIdentifier($groupCode); if (!$associatedGroup) { throw InvalidArgumentException::expected('associations', 'existing group code', 'setter', 'association', $groupCode); } $association->addGroup($associatedGroup); } }
/** * 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); } }
/** * @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']]); }
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_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']]); }