/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @expectedExceptionMessage No such entity with class_id = -9999 */ public function testGetTaxClassWithNoSuchEntityException() { $this->taxClassService->getTaxClass(-9999); }
/** * 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; } }