/** * Verify the message and params are not used to determine the call count * * @return void */ public function testAddErrorWithSameMessage() { $rawMessage = 'Foo "%var"'; $params = ['var' => 'Bar']; $expectedProcessedMessage = 'Foo "Bar"'; $inputException = new InputException(new Phrase($rawMessage, $params)); $this->assertEquals($rawMessage, $inputException->getRawMessage()); $this->assertEquals($expectedProcessedMessage, $inputException->getMessage()); $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage()); $this->assertFalse($inputException->wasErrorAdded()); $this->assertCount(0, $inputException->getErrors()); $inputException->addError(new Phrase($rawMessage, $params)); $this->assertEquals($expectedProcessedMessage, $inputException->getMessage()); $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage()); $this->assertTrue($inputException->wasErrorAdded()); $this->assertCount(0, $inputException->getErrors()); $inputException->addError(new Phrase($rawMessage, $params)); $this->assertEquals($expectedProcessedMessage, $inputException->getMessage()); $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage()); $this->assertTrue($inputException->wasErrorAdded()); $errors = $inputException->getErrors(); $this->assertCount(2, $errors); $this->assertEquals($expectedProcessedMessage, $errors[0]->getMessage()); $this->assertEquals($expectedProcessedMessage, $errors[0]->getLogMessage()); $this->assertEquals($expectedProcessedMessage, $errors[1]->getMessage()); $this->assertEquals($expectedProcessedMessage, $errors[1]->getLogMessage()); }
/** * Validate user credentials * * @param string $username * @param string $password * @throws InputException * @return void */ public function validateCredentials($username, $password) { $exception = new InputException(); if (!is_string($username) || strlen($username) == 0) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'username']); } if (!is_string($username) || strlen($password) == 0) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'password']); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate user credentials * * @param string $username * @param string $password * @throws InputException * @return void */ public function validate($username, $password) { $exception = new InputException(); if (!is_string($username) || strlen($username) == 0) { $exception->addError(__('%fieldName is a required field.', ['fieldName' => 'username'])); } if (!is_string($password) || strlen($password) == 0) { $exception->addError(__('%fieldName is a required field.', ['fieldName' => 'password'])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Process an input error * * @param array $inputError * @return void * @throws InputException */ protected function processInputError($inputError) { if (!empty($inputError)) { $exception = new InputException(); foreach ($inputError as $errorParamField) { $exception->addError(new Phrase(InputException::REQUIRED_FIELD, ['fieldName' => $errorParamField])); } if ($exception->wasErrorAdded()) { throw $exception; } } }
/** * Ensure that all necessary data is available for a new option creation. * * @param OptionInterface $option * @return void * @throws InputException * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function validateNewOptionData(OptionInterface $option) { $inputException = new InputException(); if (!$option->getAttributeId()) { $inputException->addError(__('Option attribute ID is not specified.')); } if (!$option->getLabel()) { $inputException->addError(__('Option label is not specified.')); } if (!$option->getValues()) { $inputException->addError(__('Option values are not specified.')); } else { foreach ($option->getValues() as $optionValue) { if (!$optionValue->getValueIndex()) { $inputException->addError(__('Value index is not specified for an option.')); } if (null === $optionValue->getPricingValue()) { $inputException->addError(__('Price is not specified for an option.')); } if (null === $optionValue->getIsPercent()) { $inputException->addError(__('Percent/absolute is not specified for an option.')); } } } if ($inputException->wasErrorAdded()) { throw $inputException; } }
public function testCreateCustomerWithErrors() { $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH, 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => self::SERVICE_NAME . 'CreateAccount']]; $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray($this->customerHelper->createSampleCustomerDataObject(), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $invalidEmail = 'invalid'; $customerDataArray['email'] = $invalidEmail; $requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD]; try { $this->_webApiCall($serviceInfo, $requestData); $this->fail('Expected exception did not occur.'); } catch (\Exception $e) { if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { $expectedException = new InputException(); $expectedException->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'email', 'value' => $invalidEmail])); $this->assertInstanceOf('SoapFault', $e); $this->checkSoapFault($e, $expectedException->getRawMessage(), 'env:Sender', $expectedException->getParameters()); } else { $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode()); $exceptionData = $this->processRestExceptionResult($e); $expectedExceptionData = ['message' => InputException::INVALID_FIELD_VALUE, 'parameters' => ['fieldName' => 'email', 'value' => $invalidEmail]]; $this->assertEquals($expectedExceptionData, $exceptionData); } } }
/** * Test creating a customer with absent required address fields */ public function testCreateCustomerWithoutAddressRequiresException() { $customerDataArray = $this->dataObjectProcessor->buildOutputDataArray($this->customerHelper->createSampleCustomerDataObject(), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); foreach ($customerDataArray[Customer::KEY_ADDRESSES] as &$address) { $address[Address::FIRSTNAME] = null; } $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH, 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => self::SERVICE_NAME . 'Save']]; $requestData = ['customer' => $customerDataArray]; try { $this->_webApiCall($serviceInfo, $requestData); $this->fail('Expected exception did not occur.'); } catch (\Exception $e) { if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { $expectedException = new InputException(); $expectedException->addError(__('%fieldName is a required field.', ['fieldName' => Address::FIRSTNAME])); $this->assertInstanceOf('SoapFault', $e); $this->checkSoapFault($e, $expectedException->getRawMessage(), 'env:Sender', $expectedException->getParameters()); } else { $this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode()); $exceptionData = $this->processRestExceptionResult($e); $expectedExceptionData = ['message' => '%fieldName is a required field.', 'parameters' => ['fieldName' => Address::FIRSTNAME]]; $this->assertEquals($expectedExceptionData, $exceptionData); } } try { $this->customerRegistry->retrieveByEmail($customerDataArray[Customer::EMAIL], $customerDataArray[Customer::WEBSITE_ID]); $this->fail('An expected NoSuchEntityException was not thrown.'); } catch (NoSuchEntityException $e) { $exception = NoSuchEntityException::doubleField('email', $customerDataArray[Customer::EMAIL], 'websiteId', $customerDataArray[Customer::WEBSITE_ID]); $this->assertEquals($exception->getMessage(), $e->getMessage(), 'Exception message does not match expected message.'); } }
/** * Validate tax rate * * @param \Magento\Tax\Api\Data\TaxRateInterface $taxRate * @throws InputException * @return void * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(\Magento\Tax\Api\Data\TaxRateInterface $taxRate) { $exception = new InputException(); $countryCode = $taxRate->getTaxCountryId(); if (!\Zend_Validate::is($countryCode, 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'country_id'])); } elseif (!\Zend_Validate::is($this->countryFactory->create()->loadByCode($countryCode)->getId(), 'NotEmpty')) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'country_id', 'value' => $countryCode])); } $regionCode = $taxRate->getTaxRegionId(); // if regionCode eq 0 (all regions *), do not validate with existing region list if (\Zend_Validate::is($regionCode, 'NotEmpty') && $regionCode != "0" && !\Zend_Validate::is($this->regionFactory->create()->load($regionCode)->getId(), 'NotEmpty')) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'region_id', 'value' => $regionCode])); } if (!\Zend_Validate::is($taxRate->getRate(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'percentage_rate'])); } if (!\Zend_Validate::is(trim($taxRate->getCode()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'code'])); } if ($taxRate->getZipIsRange()) { $zipRangeFromTo = ['zip_from' => $taxRate->getZipFrom(), 'zip_to' => $taxRate->getZipTo()]; foreach ($zipRangeFromTo as $key => $value) { if (!is_numeric($value) || $value < 0) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => $key, 'value' => $value])); } } if ($zipRangeFromTo['zip_from'] > $zipRangeFromTo['zip_to']) { $exception->addError(__('Range To should be equal or greater than Range From.')); } } else { if (!\Zend_Validate::is(trim($taxRate->getTaxPostcode()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode'])); } } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate Slider Item values. * * @param \Stepzerosolutions\Tbslider\Api\Data\SlideritemsInterface $slider * @throws InputException * @return void * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function _validate($slider) { $exception = new InputException(); if (!\Zend_Validate::is($slider->getSlideritemTitle(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'slideritem_title'])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate TaxClass Data * * @param \Magento\Tax\Api\Data\TaxClassInterface $taxClass * @return void * @throws InputException */ protected function validateTaxClassData(\Magento\Tax\Api\Data\TaxClassInterface $taxClass) { $exception = new InputException(); if (!\Zend_Validate::is(trim($taxClass->getClassName()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => TaxClassInterface::KEY_NAME])); } $classType = $taxClass->getClassType(); if (!\Zend_Validate::is(trim($classType), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => TaxClassInterface::KEY_TYPE])); } elseif ($classType !== TaxClassManagementInterface::TYPE_CUSTOMER && $classType !== TaxClassManagementInterface::TYPE_PRODUCT) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => TaxClassInterface::KEY_TYPE, 'value' => $classType])); } if ($exception->wasErrorAdded()) { throw $exception; } }
public function testValidateAddressesBoth() { // Setup address mock, no first name $mockBadAddress = $this->_createAddress(1, ''); // Setup address mock, with first name $mockAddress = $this->_createAddress(1, 'John'); $addressBad = $this->_addressBuilder->create(); $addressGood = $this->_addressBuilder->create(); $this->_addressConverterMock->expects($this->any())->method('createAddressModel')->will($this->returnValueMap(array(array($addressBad, $mockBadAddress), array($addressGood, $mockAddress)))); $customerService = $this->_createService(); try { $customerService->validateAddresses(array($addressBad, $addressGood)); $this->fail("InputException was expected but not thrown"); } catch (InputException $actualException) { $expectedException = new InputException(); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname', 'index' => 0]); $this->assertEquals($expectedException, $actualException); } }
/** * Validate thesaurus values * * @param \Smile\ElasticsuiteThesaurus\Api\Data\ThesaurusInterface $thesaurus the thesaurus to validate * * @return void * @throws \Magento\Framework\Exception\InputException */ protected function validate(\Smile\ElasticsuiteThesaurus\Api\Data\ThesaurusInterface $thesaurus) { $exception = new \Magento\Framework\Exception\InputException(); $validator = new \Zend_Validate(); if (!$validator->is(trim($thesaurus->getName()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'name'])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * {@inheritdoc} */ public function inputException($wrappedErrorParameters) { $exception = new InputException(); if ($wrappedErrorParameters) { foreach ($wrappedErrorParameters as $error) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => $error->getFieldName(), 'value' => $error->getValue()])); } } throw $exception; }
/** * Validate group values. * * @param \Magento\Customer\Api\Data\GroupInterface $group * @throws InputException * @return void * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function _validate($group) { $exception = new InputException(); if (!\Zend_Validate::is($group->getCode(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'code'])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate tax rate * * @param TaxRateDataObject $taxRate * @throws InputException * @return void * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(TaxRateDataObject $taxRate) { $exception = new InputException(); $countryCode = $taxRate->getCountryId(); if (!\Zend_Validate::is($countryCode, 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'country_id']); } else { if (!\Zend_Validate::is($this->countryFactory->create()->loadByCode($countryCode)->getId(), 'NotEmpty')) { $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'country_id', 'value' => $countryCode]); } } $regionCode = $taxRate->getRegionId(); if (\Zend_Validate::is($regionCode, 'NotEmpty') && !\Zend_Validate::is($this->regionFactory->create()->load($regionCode)->getId(), 'NotEmpty')) { $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'region_id', 'value' => $regionCode]); } if (!\Zend_Validate::is($taxRate->getPercentageRate(), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'percentage_rate']); } if (!\Zend_Validate::is(trim($taxRate->getCode()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'code']); } if ($taxRate->getZipRange()) { $zipRangeFromTo = ['zip_from' => $taxRate->getZipRange()->getFrom(), 'zip_to' => $taxRate->getZipRange()->getTo()]; foreach ($zipRangeFromTo as $key => $value) { if (!is_numeric($value) || $value < 0) { $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => $key, 'value' => $value]); } } if ($zipRangeFromTo['zip_from'] > $zipRangeFromTo['zip_to']) { $exception->addError('Range To should be equal or greater than Range From.'); } } else { if (!\Zend_Validate::is(trim($taxRate->getPostcode()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode']); } } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate tax rule * * @param TaxRule $rule * @return void * @throws InputException * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(TaxRule $rule) { $exception = new InputException(); // SortOrder is required and must be 0 or greater if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::SORT_ORDER]); } if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::SORT_ORDER, 'value' => $rule->getSortOrder(), 'minValue' => 0]); } // Priority is required and must be 0 or greater if (!\Zend_Validate::is(trim($rule->getPriority()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRIORITY]); } if (!\Zend_Validate::is(trim($rule->getPriority()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::PRIORITY, 'value' => $rule->getPriority(), 'minValue' => 0]); } // Code is required if (!\Zend_Validate::is(trim($rule->getCode()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CODE]); } // customer tax class ids is required if ($rule->getCustomerTaxClassIds() === null || !$rule->getCustomerTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS]); } else { // see if the customer tax class ids exist $customerTaxClassIds = $rule->getCustomerTaxClassIds(); foreach ($customerTaxClassIds as $customerTaxClassId) { try { $taxClass = $this->taxClassService->getTaxClass($customerTaxClassId); if (is_null($taxClass) || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_CUSTOMER)) { $exception->addError(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS, 'value' => $customerTaxClassId]); } } catch (NoSuchEntityException $e) { $exception->addError($e->getRawMessage(), $e->getParameters()); } } } // product tax class ids is required if ($rule->getProductTaxClassIds() === null || !$rule->getProductTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS]); } else { // see if the product tax class ids exist $productTaxClassIds = $rule->getProductTaxClassIds(); foreach ($productTaxClassIds as $productTaxClassId) { try { $taxClass = $this->taxClassService->getTaxClass($productTaxClassId); if (is_null($taxClass) || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_PRODUCT)) { $exception->addError(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS, 'value' => $productTaxClassId]); } } catch (NoSuchEntityException $e) { $exception->addError($e->getRawMessage(), $e->getParameters()); } } } // tax rate ids is required if ($rule->getTaxRateIds() === null || !$rule->getTaxRateIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::TAX_RATE_IDS]); } // throw exception if errors were found if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate group values. * * @param \Magento\Customer\Api\Data\GroupInterface $group * @throws InputException * @return void * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function _validate($group) { $exception = new InputException(); if (!\Zend_Validate::is($group->getCode(), 'NotEmpty')) { $exception->addError(__('%fieldName is a required field.', ['fieldName' => 'code'])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate tax rule * * @param TaxRule $rule * @return void * @throws InputException * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(TaxRule $rule) { $exception = new InputException(); if (!\Zend_Validate::is(trim($rule->getSortOrder()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::SORT_ORDER, 'value' => $rule->getSortOrder(), 'minValue' => 0]); } if (!\Zend_Validate::is(trim($rule->getPriority()), 'GreaterThan', [-1])) { $exception->addError(InputException::INVALID_FIELD_MIN_VALUE, ['fieldName' => TaxRule::PRIORITY, 'value' => $rule->getPriority(), 'minValue' => 0]); } // Code is required if (!\Zend_Validate::is(trim($rule->getCode()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CODE]); } // customer tax class ids is required if ($rule->getCustomerTaxClassIds() === null || !$rule->getCustomerTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::CUSTOMER_TAX_CLASS_IDS]); } // product tax class ids is required if ($rule->getProductTaxClassIds() === null || !$rule->getProductTaxClassIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::PRODUCT_TAX_CLASS_IDS]); } // tax rate ids is required if ($rule->getTaxRateIds() === null || !$rule->getTaxRateIds()) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => TaxRule::TAX_RATE_IDS]); } // throw exception if errors were found if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Validate customer attribute values. * * @param CustomerModel $customerModel * @throws InputException * @return void * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(CustomerModel $customerModel) { $exception = new InputException(); if (!\Zend_Validate::is(trim($customerModel->getFirstname()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname']); } if (!\Zend_Validate::is(trim($customerModel->getLastname()), 'NotEmpty')) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'lastname']); } if (!\Zend_Validate::is($customerModel->getEmail(), 'EmailAddress')) { $exception->addError(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'email', 'value' => $customerModel->getEmail()]); } $dob = $this->getAttributeMetadata('dob'); if (!is_null($dob) && $dob->isRequired() && '' == trim($customerModel->getDob())) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'dob']); } $taxvat = $this->getAttributeMetadata('taxvat'); if (!is_null($taxvat) && $taxvat->isRequired() && '' == trim($customerModel->getTaxvat())) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'taxvat']); } $gender = $this->getAttributeMetadata('gender'); if (!is_null($gender) && $gender->isRequired() && '' == trim($customerModel->getGender())) { $exception->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'gender']); } if ($exception->wasErrorAdded()) { throw $exception; } }
public function testValidateAddressesOneInvalidNoKeys() { $invalidAddress = $this->_addressBuilder->setFirstname('Freddy')->setLastname('Mercury')->create(); try { $this->_service->validateAddresses(array_merge($this->_expectedAddresses, array($invalidAddress))); $this->fail("InputException was expected but not thrown"); } catch (InputException $actualException) { $expectedException = new InputException(); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'street', 'index' => 2]); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'city', 'index' => 2]); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'telephone', 'index' => 2]); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode', 'index' => 2]); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'countryId', 'index' => 2]); $this->assertEquals($expectedException, $actualException); } }
/** * Validate Customer Addresses attribute values. * * @param CustomerAddressModel $customerAddressModel the model to validate * @return InputException * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function _validate(CustomerAddressModel $customerAddressModel) { $exception = new InputException(); if ($customerAddressModel->getShouldIgnoreValidation()) { return $exception; } if (!\Zend_Validate::is($customerAddressModel->getFirstname(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname'])); } if (!\Zend_Validate::is($customerAddressModel->getLastname(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'lastname'])); } if (!\Zend_Validate::is($customerAddressModel->getStreetLine(1), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'street'])); } if (!\Zend_Validate::is($customerAddressModel->getCity(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'city'])); } if (!\Zend_Validate::is($customerAddressModel->getTelephone(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'telephone'])); } $havingOptionalZip = $this->directoryData->getCountriesWithOptionalZip(); if (!in_array($customerAddressModel->getCountryId(), $havingOptionalZip) && !\Zend_Validate::is($customerAddressModel->getPostcode(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'postcode'])); } if (!\Zend_Validate::is($customerAddressModel->getCountryId(), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'countryId'])); } if ($customerAddressModel->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is($customerAddressModel->getRegionId(), 'NotEmpty') && $this->directoryData->isRegionRequired($customerAddressModel->getCountryId())) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'regionId'])); } return $exception; }
private function validate(PostInterface $post) { $exception = new InputException(); if (!\Zend_Validate::is(trim($post->getTitle()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'title'])); } if (!\Zend_Validate::is(trim($post->getUrlKey()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'url_key'])); } $id = $this->_resourceModel->checkUrlKey($post->getUrlKey()); if ($id && $id != $post->getId()) { $exception->addError(__(InputException::DEFAULT_MESSAGE)); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Convert the input array from key-value format to a list of parameters suitable for the specified class / method. * * The input array should have the field name as the key, and the value will either be a primitive or another * key-value array. The top level of this array needs keys that match the names of the parameters on the * service method. * * Mismatched types are caught by the PHP runtime, not explicitly checked for by this code. * * @param string $serviceClassName name of the service class that we are trying to call * @param string $serviceMethodName name of the method that we are trying to call * @param array $inputArray data to send to method in key-value format * @return array list of parameters that can be used to call the service method * @throws InputException if no value is provided for required parameters */ public function process($serviceClassName, $serviceMethodName, array $inputArray) { $inputData = []; $inputError = []; foreach ($this->getMethodParams($serviceClassName, $serviceMethodName) as $param) { $paramName = $param['name']; $snakeCaseParamName = strtolower(preg_replace("/(?<=\\w)(?=[A-Z])/", "_$1", $paramName)); if (isset($inputArray[$paramName]) || isset($inputArray[$snakeCaseParamName])) { $paramValue = isset($inputArray[$paramName]) ? $inputArray[$paramName] : $inputArray[$snakeCaseParamName]; $inputData[] = $this->_convertValue($paramValue, $param['type']); } else { if ($param['isDefaultValueAvailable']) { $inputData[] = $param['defaultValue']; } else { $inputError[] = $paramName; } } } if (!empty($inputError)) { $exception = new InputException(); foreach ($inputError as $errorParamField) { $exception->addError(new Phrase(InputException::REQUIRED_FIELD, ['fieldName' => $errorParamField])); } if ($exception->wasErrorAdded()) { throw $exception; } } return $inputData; }
/** * Validate customer attribute values. * * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @throws InputException * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ private function validate(\Magento\Customer\Api\Data\CustomerInterface $customer) { $exception = new InputException(); if (!\Zend_Validate::is(trim($customer->getFirstname()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname'])); } if (!\Zend_Validate::is(trim($customer->getLastname()), 'NotEmpty')) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'lastname'])); } $isEmailAddress = \Zend_Validate::is($customer->getEmail(), 'EmailAddress'); if (!$isEmailAddress) { $exception->addError(__(InputException::INVALID_FIELD_VALUE, ['fieldName' => 'email', 'value' => $customer->getEmail()])); } $dob = $this->getAttributeMetadata('dob'); if ($dob !== null && $dob->isRequired() && '' == trim($customer->getDob())) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'dob'])); } $taxvat = $this->getAttributeMetadata('taxvat'); if ($taxvat !== null && $taxvat->isRequired() && '' == trim($customer->getTaxvat())) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'taxvat'])); } $gender = $this->getAttributeMetadata('gender'); if ($gender !== null && $gender->isRequired() && '' == trim($customer->getGender())) { $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'gender'])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * {@inheritdoc} */ public function inputException($wrappedErrorParameters) { $exception = new InputException(); if ($wrappedErrorParameters) { foreach ($wrappedErrorParameters as $error) { $exception->addError(__('Invalid value of "%value" provided for the %fieldName field.', ['fieldName' => $error->getFieldName(), 'value' => $error->getValue()])); } } throw $exception; }
/** * Validate TaxClass Data * * @param \Magento\Tax\Api\Data\TaxClassInterface $taxClass * @return void * @throws InputException */ protected function validateTaxClassData(\Magento\Tax\Api\Data\TaxClassInterface $taxClass) { $exception = new InputException(); if (!\Zend_Validate::is(trim($taxClass->getClassName()), 'NotEmpty')) { $exception->addError(__('%fieldName is a required field.', ['fieldName' => ClassModel::KEY_NAME])); } $classType = $taxClass->getClassType(); if (!\Zend_Validate::is(trim($classType), 'NotEmpty')) { $exception->addError(__('%fieldName is a required field.', ['fieldName' => ClassModel::KEY_TYPE])); } elseif ($classType !== TaxClassManagementInterface::TYPE_CUSTOMER && $classType !== TaxClassManagementInterface::TYPE_PRODUCT) { $exception->addError(__('Invalid value of "%value" provided for the %fieldName field.', ['fieldName' => ClassModel::KEY_TYPE, 'value' => $classType])); } if ($exception->wasErrorAdded()) { throw $exception; } }
/** * Process an input error * * @param array $inputError * @return void * @throws InputException */ protected function processInputError($inputError) { if (!empty($inputError)) { $exception = new InputException(); foreach ($inputError as $errorParamField) { $exception->addError(new Phrase('%fieldName is a required field.', ['fieldName' => $errorParamField])); } if ($exception->wasErrorAdded()) { throw $exception; } } }