/** * Check if data are valid * * @param string $field * @param mixed $data */ protected function checkData($field, $data) { if (!is_array($data)) { throw InvalidArgumentException::arrayExpected($field, 'remover', 'category', gettype($data)); } foreach ($data as $key => $value) { if (!is_string($value)) { throw InvalidArgumentException::arrayStringValueExpected($field, $key, 'remover', 'category', gettype($value)); } } }
/** * 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', 'multi select', gettype($data)); } foreach ($data as $key => $value) { if (!is_string($value)) { throw InvalidArgumentException::arrayStringValueExpected($attribute->getCode(), $key, 'setter', 'multi select', gettype($value)); } } }
/** * 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']); } }
function it_checks_valid_data_format(ProductInterface $product) { $this->shouldThrow(InvalidArgumentException::arrayExpected('categories', 'setter', 'category', 'string'))->during('setFieldData', [$product, 'categories', 'not an array']); $this->shouldThrow(InvalidArgumentException::arrayStringValueExpected('categories', 0, 'setter', 'category', 'array'))->during('setFieldData', [$product, 'categories', [['array of array']]]); }
function it_throws_an_error_if_unit_from_attribute_data_is_not_a_string(AttributeInterface $attribute, ProductInterface $product) { $attribute->getCode()->willReturn('attributeCode'); $data = ['data' => 42, 'unit' => 123]; $this->shouldThrow(InvalidArgumentException::arrayStringValueExpected('attributeCode', 'unit', 'setter', 'metric', 123))->during('setAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]); }
function it_throws_an_error_if_attribute_data_is_not_an_array_of_option_codes(AttributeInterface $attribute, ProductInterface $product) { $attribute->getCode()->willReturn('attributeCode'); $data = ['foo' => ['bar' => 'baz']]; $this->shouldThrow(InvalidArgumentException::arrayStringValueExpected('attributeCode', 'foo', 'adder', 'multi select', 'array'))->during('addAttributeData', [$product, $attribute, $data, ['locale' => 'fr_FR', 'scope' => 'mobile']]); }