function it_throws_an_error_if_an_option_code_is_unknown_on_attribute_data_set($attrOptionRepository, ProductInterface $product, AttributeInterface $attribute) { $attribute->getCode()->willReturn('attributeCode'); $data = ['unknown code']; $attrOptionRepository->findOneBy(['code' => 'unknown code', 'attribute' => $attribute])->shouldBeCalledTimes(1)->willReturn(null); $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('attributeCode', 'code', 'The option does not exist', 'adder', 'multi select', 'unknown code'))->during('addAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]); }
function it_throws_an_error_if_data_value_does_not_contain_valid_currency($currencyManager, AttributeInterface $attribute) { $attribute->getCode()->willReturn('attributeCode'); $currencyManager->getActiveCodes()->willReturn(['EUR', 'USD']); $data = [['data' => 123, 'currency' => 'invalid currency']]; $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('attributeCode', 'currency', 'The currency does not exist', 'setter', 'prices collection', 'invalid currency'))->during('setValue', [[], $attribute, $data, 'fr_FR', 'mobile']); }
function it_throws_an_error_if_an_option_code_is_unknown($attrOptionRepository, AttributeInterface $attribute) { $attribute->getCode()->willReturn('attributeCode'); $data = ['unknown code']; $attrOptionRepository->findOneBy(['code' => 'unknown code', 'attribute' => $attribute])->shouldBeCalledTimes(1)->willReturn(null); $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('attributeCode', 'code', 'The option does not exist', 'setter', 'multi select', 'unknown code'))->during('setValue', [[], $attribute, $data, 'fr_FR', 'mobile']); }
function it_throws_an_error_if_attribute_data_unit_does_not_exist(AttributeInterface $attribute, ProductInterface $product, $measureManager) { $attribute->getCode()->willReturn('attributeCode'); $attribute->getMetricFamily()->willReturn('Weight'); $data = ['data' => 42, 'unit' => 'incorrect unit']; $measureManager->getUnitSymbolsForFamily('Weight')->shouldBeCalled()->willReturn(['KILOGRAM' => 'kg', 'GRAM' => 'g']); $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('attributeCode', 'unit', 'The unit does not exist', 'setter', 'metric', 'incorrect unit'))->during('setAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]); }
/** * {@inheritdoc} * * Expected data input format: ["option_code", "other_option_code"] */ public function setAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = []) { $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'multi select'); $this->checkData($attribute, $data); $attributeOptions = []; foreach ($data as $optionCode) { $option = $this->getOption($attribute, $optionCode); if (null === $option) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'code', 'The option does not exist', 'setter', 'multi select', $optionCode); } $attributeOptions[] = $option; } $this->setOptions($product, $attribute, $attributeOptions, $options['locale'], $options['scope']); }
/** * {@inheritdoc} */ public function setAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = []) { $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'reference data collection'); $this->checkData($attribute, $data); $referenceDataCollection = []; $repository = $this->repositoryResolver->resolve($attribute->getReferenceDataName()); foreach ($data as $referenceDataCode) { $referenceData = $repository->findOneBy(['code' => $referenceDataCode]); if (null === $referenceData) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'code', sprintf('No reference data "%s" with code "%s" has been found', $attribute->getReferenceDataName(), $referenceDataCode), 'setter', 'reference data collection', $referenceDataCode); } $referenceDataCollection[] = $referenceData; } $this->setReferenceDataCollection($attribute, $product, $referenceDataCollection, $options['locale'], $options['scope']); }
/** * {@inheritdoc} * * Expected data input format: ["option_code", "other_option_code"] */ public function addAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = []) { $options = $this->resolver->resolve($options); $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'multi select'); $this->checkData($attribute, $data); $attributeOptions = []; foreach ($data as $optionCode) { $option = $this->attrOptionRepository->findOneBy(['code' => $optionCode, 'attribute' => $attribute]); if (null === $option) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'code', 'The option does not exist', 'adder', 'multi select', $optionCode); } $attributeOptions[] = $option; } $this->addOptions($product, $attribute, $attributeOptions, $options['locale'], $options['scope']); }
/** * {@inheritdoc} */ public function setValue(array $products, AttributeInterface $attribute, $data, $locale = null, $scope = null) { $this->checkLocaleAndScope($attribute, $locale, $scope, 'multi select'); $this->checkData($attribute, $data); $attributeOptions = []; foreach ($data as $optionCode) { $option = $this->attrOptionRepository->findOneBy(['code' => $optionCode, 'attribute' => $attribute]); if (null === $option) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'code', 'The option does not exist', 'setter', 'multi select', $optionCode); } $attributeOptions[] = $option; } foreach ($products as $product) { $this->setOptions($attribute, $product, $attributeOptions, $locale, $scope); } }
/** * Check if data is valid * * @param AttributeInterface $attribute * @param mixed $data */ protected function checkData(AttributeInterface $attribute, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'setter', 'metric', gettype($data)); } if (!array_key_exists('data', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'setter', 'metric', print_r($data, true)); } if (!array_key_exists('unit', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'unit', 'setter', 'metric', print_r($data, true)); } if (!is_string($data['unit'])) { throw InvalidArgumentException::arrayStringValueExpected($attribute->getCode(), 'unit', 'setter', 'metric', $data['unit']); } if (!array_key_exists($data['unit'], $this->measureManager->getUnitSymbolsForFamily($attribute->getMetricFamily()))) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'unit', 'The unit does not exist', 'setter', 'metric', $data['unit']); } }
/** * Check if data are valid * * @param AttributeInterface $attribute * @param mixed $data * * @return mixed */ protected function checkData(AttributeInterface $attribute, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'setter', 'prices collection', gettype($data)); } foreach ($data as $price) { if (!is_array($price)) { throw InvalidArgumentException::arrayOfArraysExpected($attribute->getCode(), 'setter', 'prices collection', gettype($data)); } if (!array_key_exists('data', $price)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'setter', 'prices collection', print_r($data, true)); } if (!array_key_exists('currency', $price)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'currency', 'setter', 'prices collection', print_r($data, true)); } if (!in_array($price['currency'], $this->currencyManager->getActiveCodes())) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'currency', 'The currency does not exist', 'setter', 'prices collection', $price['currency']); } } }
/** * Check if value is valid * * @param AttributeInterface $attribute * @param mixed $data */ protected function checkValue(AttributeInterface $attribute, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'filter', 'metric', gettype($data)); } if (!array_key_exists('data', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'filter', 'metric', print_r($data, true)); } if (!array_key_exists('unit', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'unit', 'filter', 'metric', print_r($data, true)); } if (!is_numeric($data['data']) && null !== $data['data']) { throw InvalidArgumentException::arrayNumericKeyExpected($attribute->getCode(), 'data', 'filter', 'metric', gettype($data['data'])); } if (!is_string($data['unit'])) { throw InvalidArgumentException::arrayStringKeyExpected($attribute->getCode(), 'unit', 'filter', 'metric', gettype($data['unit'])); } if (!array_key_exists($data['unit'], $this->measureManager->getUnitSymbolsForFamily($attribute->getMetricFamily()))) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'unit', sprintf('The unit does not exist in the attribute\'s family "%s"', $attribute->getMetricFamily()), 'filter', 'metric', $data['unit']); } }
/** * @param AttributeInterface $attribute * @param mixed $data */ protected function checkValue(AttributeInterface $attribute, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'filter', 'price', gettype($data)); } if (!array_key_exists('data', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'filter', 'price', print_r($data, true)); } if (!array_key_exists('currency', $data)) { throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'currency', 'filter', 'price', print_r($data, true)); } if (!is_numeric($data['data']) && null !== $data['data']) { throw InvalidArgumentException::arrayNumericKeyExpected($attribute->getCode(), 'data', 'filter', 'price', gettype($data['data'])); } if (!is_string($data['currency'])) { throw InvalidArgumentException::arrayStringKeyExpected($attribute->getCode(), 'currency', 'filter', 'price', gettype($data['currency'])); } if (!in_array($data['currency'], $this->currencyManager->getActiveCodes())) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'currency', 'The currency does not exist', 'filter', 'price', $data['currency']); } }
function it_throws_an_error_if_the_option_doesnt_exist(AttributeInterface $attribute) { $attribute->getCode()->willReturn('attributeCode'); $data = 'unknown code'; $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('attributeCode', 'code', 'The option does not exist', 'setter', 'simple select', $data))->duringSetValue([], $attribute, $data, 'fr_FR', 'mobile'); }
function it_throws_an_exception_if_value_had_not_a_valid_currency($currencyManager, AttributeInterface $attribute) { $currencyManager->getActiveCodes()->willReturn(['EUR', 'USD']); $attribute->getCode()->willReturn('price_code'); $value = ['data' => 132, 'currency' => 'FOO']; $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('price_code', 'currency', 'The currency does not exist', 'filter', 'price', 'FOO'))->during('addAttributeFilter', [$attribute, '=', $value]); }
function it_throws_an_exception_if_value_had_not_a_valid_unit($measureManager, AttributeInterface $attribute) { $attribute->getMetricFamily()->willReturn('length'); $measureManager->getUnitSymbolsForFamily('length')->willReturn(['CENTIMETER' => 'cm', 'METER' => 'm', 'KILOMETER' => 'km']); $attribute->getCode()->willReturn('metric_code'); $value = ['data' => 132, 'unit' => 'foo']; $this->shouldThrow(InvalidArgumentException::arrayInvalidKey('metric_code', 'unit', 'The unit does not exist in the attribute\'s family "length"', 'filter', 'metric', 'foo'))->during('addAttributeFilter', [$attribute, '=', $value]); }