public function testConstructor() { $exception = new NoSuchEntityException(); $this->assertEquals('No such entity.', $exception->getRawMessage()); $this->assertEquals('No such entity.', $exception->getMessage()); $this->assertEquals('No such entity.', $exception->getLogMessage()); $exception = new NoSuchEntityException(new Phrase(NoSuchEntityException::MESSAGE_SINGLE_FIELD, ['fieldName' => 'field', 'fieldValue' => 'value'])); $this->assertEquals('No such entity with field = value', $exception->getMessage()); $this->assertEquals(NoSuchEntityException::MESSAGE_SINGLE_FIELD, $exception->getRawMessage()); $this->assertEquals('No such entity with field = value', $exception->getLogMessage()); $exception = new NoSuchEntityException(new Phrase(NoSuchEntityException::MESSAGE_DOUBLE_FIELDS, ['fieldName' => 'field1', 'fieldValue' => 'value1', 'field2Name' => 'field2', 'field2Value' => 'value2'])); $this->assertEquals(NoSuchEntityException::MESSAGE_DOUBLE_FIELDS, $exception->getRawMessage()); $this->assertEquals('No such entity with field1 = value1, field2 = value2', $exception->getMessage()); $this->assertEquals('No such entity with field1 = value1, field2 = value2', $exception->getLogMessage()); }
public function testConstructor() { $exception = new NoSuchEntityException(); $this->assertEquals('No such entity.', $exception->getRawMessage()); $this->assertEquals('No such entity.', $exception->getMessage()); $this->assertEquals('No such entity.', $exception->getLogMessage()); $exception = new NoSuchEntityException(new Phrase('No such entity with %fieldName = %fieldValue', ['fieldName' => 'field', 'fieldValue' => 'value'])); $this->assertEquals('No such entity with field = value', $exception->getMessage()); $this->assertEquals('No such entity with %fieldName = %fieldValue', $exception->getRawMessage()); $this->assertEquals('No such entity with field = value', $exception->getLogMessage()); $exception = new NoSuchEntityException(new Phrase('No such entity with %fieldName = %fieldValue, %field2Name = %field2Value', ['fieldName' => 'field1', 'fieldValue' => 'value1', 'field2Name' => 'field2', 'field2Value' => 'value2'])); $this->assertEquals('No such entity with %fieldName = %fieldValue, %field2Name = %field2Value', $exception->getRawMessage()); $this->assertEquals('No such entity with field1 = value1, field2 = value2', $exception->getMessage()); $this->assertEquals('No such entity with field1 = value1, field2 = value2', $exception->getLogMessage()); }
/** * Check if quote is allowed * * @param \Magento\Quote\Api\CartRepositoryInterface $subject * @param \Magento\Quote\Model\Quote $quote * @return \Magento\Quote\Model\Quote * @throws \Magento\Framework\Exception\NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetActiveForCustomer(\Magento\Quote\Api\CartRepositoryInterface $subject, \Magento\Quote\Model\Quote $quote) { if (!$this->isAllowed($quote)) { throw NoSuchEntityException::singleField('cartId', $quote->getId()); } return $quote; }
/** * Save coupon. * * @param \Magento\SalesRule\Api\Data\CouponInterface $coupon * @return \Magento\SalesRule\Api\Data\CouponInterface * @throws \Magento\Framework\Exception\InputException If there is a problem with the input * @throws \Magento\Framework\Exception\NoSuchEntityException If a coupon ID is sent but the coupon does not exist * @throws \Magento\Framework\Exception\LocalizedException */ public function save(\Magento\SalesRule\Api\Data\CouponInterface $coupon) { //if coupon id is provided, use the existing coupon and blend in the new data supplied $couponId = $coupon->getCouponId(); if ($couponId) { $existingCoupon = $this->getById($couponId); $mergedData = array_merge($existingCoupon->getData(), $coupon->getData()); $coupon->setData($mergedData); } //blend in specific fields from the rule try { $rule = $this->ruleFactory->create()->load($coupon->getRuleId()); if (!$rule->getRuleId()) { throw \Magento\Framework\Exception\NoSuchEntityException::singleField('rule_id', $coupon->getRuleId()); } if ($rule->getCouponType() == $rule::COUPON_TYPE_NO_COUPON) { throw new \Magento\Framework\Exception\LocalizedException(__('Specified rule does not allow coupons')); } elseif ($rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_MANUAL) { throw new \Magento\Framework\Exception\LocalizedException(__('Specified rule only allows auto generated coupons')); } elseif (!$rule->getUseAutoGeneration() && $coupon->getType() == $coupon::TYPE_GENERATED) { throw new \Magento\Framework\Exception\LocalizedException(__('Specified rule does not allow auto generated coupons')); } $coupon->setExpirationDate($rule->getToDate()); $coupon->setUsageLimit($rule->getUsesPerCoupon()); $coupon->setUsagePerCustomer($rule->getUsesPerCustomer()); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('Error occurred when saving coupon: %1', $e->getMessage())); } $this->resourceModel->save($coupon); return $coupon; }
/** * Check if quote is allowed * * @param \Magento\Sales\Model\QuoteRepository $subject * @param \Magento\Sales\Model\Quote $quote * @return \Magento\Sales\Model\Quote * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function afterGetForCustomer(\Magento\Sales\Model\QuoteRepository $subject, \Magento\Sales\Model\Quote $quote) { if (!$this->isAllowed($quote)) { throw NoSuchEntityException::singleField('cartId', $quote->getId()); } return $quote; }
/** * Checks if order is allowed * * @param \Magento\Sales\Model\ResourceModel\Order $subject * @param callable $proceed * @param \Magento\Framework\Model\AbstractModel $order * @param mixed $value * @param null|string $field * @return \Magento\Sales\Model\Order * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundLoad(\Magento\Sales\Model\ResourceModel\Order $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $order, $value, $field = null) { $result = $proceed($order, $value, $field); if (!$this->isAllowed($order)) { throw NoSuchEntityException::singleField('orderId', $order->getId()); } return $result; }
/** * Get active quote by id * * @param int $cartId * @param int[] $sharedStoreIds * @throws NoSuchEntityException * @return CartInterface */ public function get($cartId, array $sharedStoreIds = []) { $quote = parent::get($cartId, $sharedStoreIds); if (!$quote->getIsActive()) { throw NoSuchEntityException::singleField('cartId', $cartId); } return $quote; }
/** * Load quote with different methods * * @param string $loadMethod * @param string $loadField * @param int $identifier * @return Quote * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function loadQuote($loadMethod, $loadField, $identifier) { $quote = $this->quoteFactory->create(); $quote->setStoreId($this->storeManager->getStore()->getId())->{$loadMethod}($identifier); if (!$quote->getId() || !$quote->getIsActive()) { throw NoSuchEntityException::singleField($loadField, $identifier); } return $quote; }
/** * @return void */ public function testDoubleField() { $website = 'website'; $websiteValue = 15; $email = 'email'; $emailValue = '*****@*****.**'; NoSuchEntityException::doubleField($website, $websiteValue, $email, $emailValue); $this->assertSame("No such entity with {$website} = {$websiteValue}, {$email} = {$emailValue}", NoSuchEntityException::doubleField($website, $websiteValue, $email, $emailValue)->getMessage()); }
/** * Get cart by id * * @param int $cartId * @return Quote * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function get($cartId) { $quote = $this->quoteFactory->create(); $quote->setStoreId($this->storeManager->getStore()->getId())->load($cartId); if (!$quote->getId() || !$quote->getIsActive()) { throw NoSuchEntityException::singleField('cartId', $cartId); } return $quote; }
/** * @param int $id * @return CategoryModel * @throws \Magento\Framework\Exception\NoSuchEntityException */ private function getCategory($id) { /** @var CategoryModel $category */ $category = $this->categoryFactory->create(); $category->load($id); if (!$category->getId()) { throw NoSuchEntityException::singleField(Category::ID, $id); } return $category; }
/** * {@inheritdoc} */ public function getAttributes($formCode) { $attributes = []; $attributesFormCollection = $this->attributeMetadataDataProvider->loadAttributesCollection(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $formCode); foreach ($attributesFormCollection as $attribute) { $attributes[$attribute->getAttributeCode()] = $this->attributeMetadataConverter->createMetadataAttribute($attribute); } if (empty($attributes)) { throw NoSuchEntityException::singleField('formCode', $formCode); } return $attributes; }
/** * Get instance of the Group Model identified by an id * * @param int $groupId * @return Group * @throws NoSuchEntityException */ public function retrieve($groupId) { if (isset($this->registry[$groupId])) { return $this->registry[$groupId]; } $group = $this->groupFactory->create(); $group->load($groupId); if (is_null($group->getId())) { throw NoSuchEntityException::singleField('groupId', $groupId); } $this->registry[$groupId] = $group; return $group; }
/** * Get instance of the Address Model identified by id * * @param int $addressId * @return Address * @throws NoSuchEntityException */ public function retrieve($addressId) { if (isset($this->registry[$addressId])) { return $this->registry[$addressId]; } $address = $this->addressFactory->create(); $address->load($addressId); if (!$address->getId()) { throw NoSuchEntityException::singleField('addressId', $addressId); } $this->registry[$addressId] = $address; return $address; }
/** * Retrieve TaxRule Model from registry given an id * * @param int $taxRuleId * @return TaxRuleModel * @throws NoSuchEntityException */ public function retrieveTaxRule($taxRuleId) { if (isset($this->registry[$taxRuleId])) { return $this->registry[$taxRuleId]; } $taxRuleModel = $this->taxRuleModelFactory->create()->load($taxRuleId); if (!$taxRuleModel->getId()) { // tax rule does not exist throw NoSuchEntityException::singleField('taxRuleId', $taxRuleId); } $this->registry[$taxRuleModel->getId()] = $taxRuleModel; return $taxRuleModel; }
/** * Get instance of the Demo Model identified by an id * * @param int $demoId * @return Demo * @throws NoSuchEntityException */ public function retrieve($demoId) { if (isset($this->registry[$demoId])) { return $this->registry[$demoId]; } $demo = $this->demoFactory->create(); $demo->load($demoId); if ($demo->getId() === null || $demo->getId() != $demoId) { throw NoSuchEntityException::singleField(DemoInterface::ID, $demoId); } $this->registry[$demoId] = $demo; return $demo; }
/** * Get instance of the Group Model identified by an id * * @param int $groupId * @return Group * @throws NoSuchEntityException */ public function retrieve($groupId) { if (isset($this->registry[$groupId])) { return $this->registry[$groupId]; } $group = $this->groupFactory->create(); $group->load($groupId); if ($group->getId() === null || $group->getId() != $groupId) { throw NoSuchEntityException::singleField(GroupInterface::ID, $groupId); } $this->registry[$groupId] = $group; return $group; }
/** * {@inheritdoc} */ public function getAttributes($formCode) { $attributes = []; $attributesFormCollection = $this->attributeMetadataDataProvider->loadAttributesCollection(self::ENTITY_TYPE_CUSTOMER, $formCode); foreach ($attributesFormCollection as $attribute) { /** @var $attribute \Magento\Customer\Model\Attribute */ $attributes[$attribute->getAttributeCode()] = $this->attributeMetadataConverter->createMetadataAttribute($attribute); } if (empty($attributes)) { throw NoSuchEntityException::singleField('formCode', $formCode); } return $attributes; }
/** * Get instance of the Slider Model identified by an id * * @param int $sliderId * @return Slider * @throws NoSuchEntityException */ public function retrieve($sliderId) { if (isset($this->registry[$sliderId])) { return $this->registry[$sliderId]; } $slider = $this->sliderFactory->create(); $slider->load($sliderId); if ($slider->getId() === null || $slider->getId() != $sliderId) { throw NoSuchEntityException::singleField(SliderInterface::ID, $sliderId); } $this->registry[$sliderId] = $slider; return $slider; }
/** * Retrieve tax class model from the registry * * @param int $taxClassId * @return TaxClassModel * @throws NoSuchEntityException */ public function retrieve($taxClassId) { if (isset($this->taxClassRegistryById[$taxClassId])) { return $this->taxClassRegistryById[$taxClassId]; } /** @var TaxClassModel $taxClassModel */ $taxClassModel = $this->taxClassModelFactory->create()->load($taxClassId); if (!$taxClassModel->getId()) { // tax class does not exist throw NoSuchEntityException::singleField(TaxClass::KEY_ID, $taxClassId); } $this->taxClassRegistryById[$taxClassModel->getId()] = $taxClassModel; return $taxClassModel; }
public function retrieve($featuretoggleId) { if (isset($this->featuretoggleRegistryById[$featuretoggleId])) { return $this->featuretoggleRegistryById[$featuretoggleId]; } /** @var Warehouse $warehouse */ $featureToggle = $this->featuretoggleFactory->create()->load($featuretoggleId); if (!$featureToggle->getFeaturetoggleId()) { // featureToggle does not exist throw NoSuchEntityException::singleField('featuretoggleId', $featuretoggleId); } else { $this->featuretoggleRegistryById[$featuretoggleId] = $featureToggle; return $featureToggle; } }
/** * {@inheritdoc} */ public function getList($attributeSetId) { if (!$this->attributeSetFactory->create()->load($attributeSetId)->getId()) { throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId); } $collection = $this->groupListFactory->create(); $collection->setAttributeSetFilter($attributeSetId); $collection->setSortOrder(); $groups = array(); /** @var $group \Magento\Eav\Model\Entity\Attribute\Group */ foreach ($collection->getItems() as $group) { $this->groupBuilder->setId($group->getId())->setName($group->getAttributeGroupName()); $groups[] = $this->groupBuilder->create(); } return $groups; }
/** * Retrieve Customer Model from registry given an id * * @param string $customerId * @return Customer * @throws NoSuchEntityException */ public function retrieve($customerId) { if (isset($this->customerRegistryById[$customerId])) { return $this->customerRegistryById[$customerId]; } /** @var Customer $customer */ $customer = $this->customerFactory->create()->load($customerId); if (!$customer->getId()) { // customer does not exist throw NoSuchEntityException::singleField('customerId', $customerId); } else { $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId()); $this->customerRegistryById[$customerId] = $customer; $this->customerRegistryByEmail[$emailKey] = $customer; return $customer; } }
public function retrieveByUrl($url) { if (isset($this->postRegistryByUrl[$url])) { return $this->postRegistryByUrl[$url]; } /** @var $post Post $post */ $post = $this->postFactory->create(); $id = $post->checkUrlKey($url); if (!$id) { throw NoSuchEntityException::singleField('url_key', $url); } else { $post->load($id); $this->postRegistryById[$id] = $post; $this->postRegistryByUrl[$url] = $post; return $post; } }
/** * Read configuration by code * * @param null|string $code * @return array * @throws NoSuchEntityException */ public function read($code = null) { if (empty($code)) { $store = $this->_storeManager->getStore(); } else { $store = $this->_storeFactory->create(); $store->load($code); } if (!($store && $store->getCode())) { throw NoSuchEntityException::singleField('storeCode', $code); } $websiteConfig = $this->_scopePool->getScope(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, $store->getWebsite()->getCode())->getSource(); $config = array_replace_recursive($websiteConfig, $this->_initialConfig->getData("stores|{$code}")); $collection = $this->_collectionFactory->create(['scope' => \Magento\Store\Model\ScopeInterface::SCOPE_STORES, 'scopeId' => $store->getId()]); $dbStoreConfig = []; foreach ($collection as $item) { $dbStoreConfig[$item->getPath()] = $item->getValue(); } $config = $this->_converter->convert($dbStoreConfig, $config); return $config; }
/** * {@inheritdoc} */ public function removeOption($id, $optionId) { $model = $this->eavConfig->getAttribute(ProductMetadataServiceInterface::ENTITY_TYPE, $id); if (!$model || !$model->getId()) { throw NoSuchEntityException::singleField(AttributeMetadata::ATTRIBUTE_ID, $id); } if (!$model->usesSource()) { throw new StateException('Attribute doesn\'t have any option'); } if (!$model->getSource()->getOptionText($optionId)) { throw new NoSuchEntityException(sprintf('Attribute %s does not contain option with Id %s', $id, $optionId)); } $modelData = array('option' => array('value' => array($optionId => []), 'delete' => array($optionId => '1'))); $model->addData($modelData); try { $model->save(); } catch (\Exception $e) { throw new StateException('Unable to remove option'); } return true; }
/** * Generate coupon for a rule * * @param \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec * @return string[] * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\LocalizedException */ public function generate(\Magento\SalesRule\Api\Data\CouponGenerationSpecInterface $couponSpec) { $data = $this->convertCouponSpec($couponSpec); if (!$this->couponGenerator->validateData($data)) { throw new \Magento\Framework\Exception\InputException(); } try { $rule = $this->ruleFactory->create()->load($couponSpec->getRuleId()); if (!$rule->getRuleId()) { throw \Magento\Framework\Exception\NoSuchEntityException::singleField(\Magento\SalesRule\Model\Coupon::KEY_RULE_ID, $couponSpec->getRuleId()); } if (!$rule->getUseAutoGeneration()) { throw new \Magento\Framework\Exception\LocalizedException(__('Specified rule does not allow automatic coupon generation')); } $this->couponGenerator->setData($data); $this->couponGenerator->setData('to_date', $rule->getToDate()); $this->couponGenerator->setData('uses_per_coupon', $rule->getUsesPerCoupon()); $this->couponGenerator->setData('usage_per_customer', $rule->getUsesPerCustomer()); $this->couponGenerator->generatePool(); return $this->couponGenerator->getGeneratedCodes(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('Error occurred when generating coupons: %1', $e->getMessage())); } }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute) { if ($attribute->getAttributeId()) { $existingModel = $this->get($attribute->getAttributeCode()); if (!$existingModel->getAttributeId()) { throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode()); } $attribute->setAttributeId($existingModel->getAttributeId()); $attribute->setIsUserDefined($existingModel->getIsUserDefined()); $attribute->setFrontendInput($existingModel->getFrontendInput()); if (is_array($attribute->getFrontendLabels())) { $frontendLabel[0] = $existingModel->getDefaultFrontendLabel(); foreach ($attribute->getFrontendLabels() as $item) { $frontendLabel[$item->getStoreId()] = $item->getLabel(); } $attribute->setDefaultFrontendLabel($frontendLabel); } if (!$attribute->getIsUserDefined()) { // Unset attribute field for system attributes $attribute->setApplyTo(null); } } else { $attribute->setAttributeId(null); if (!$attribute->getFrontendLabels() && !$attribute->getDefaultFrontendLabel()) { throw InputException::requiredField('frontend_label'); } $frontendLabels = []; if ($attribute->getDefaultFrontendLabel()) { $frontendLabels[0] = $attribute->getDefaultFrontendLabel(); } if ($attribute->getFrontendLabels() && is_array($attribute->getFrontendLabels())) { foreach ($attribute->getFrontendLabels() as $label) { $frontendLabels[$label->getStoreId()] = $label->getLabel(); } if (!isset($frontendLabels[0]) || !$frontendLabels[0]) { throw InputException::invalidFieldValue('frontend_label', null); } $attribute->setDefaultFrontendLabel($frontendLabels); } $attribute->setAttributeCode($attribute->getAttributeCode() ?: $this->generateCode($frontendLabels[0])); $this->validateCode($attribute->getAttributeCode()); $this->validateFrontendInput($attribute->getFrontendInput()); $attribute->setBackendType($attribute->getBackendTypeByInput($attribute->getFrontendInput())); $attribute->setSourceModel($this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())); $attribute->setBackendModel($this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())); $attribute->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)->getId()); $attribute->setIsUserDefined(1); } $this->attributeResource->save($attribute); return $attribute; }
/** * Verify that deleting an existing group works. */ public function testDeleteGroupExists() { $group = $this->customerGroupFactory->create(); $group->setId(null); $group->setCode('Delete Group'); $group->setTaxClassId(3); $groupId = $this->createGroup($group); $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH . "/{$groupId}", 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => 'customerGroupRepositoryV1DeleteById']]; $requestData = [CustomerGroup::ID => $groupId]; $response = $this->_webApiCall($serviceInfo, $requestData); $this->assertTrue($response, 'Expected response should be true.'); try { $this->groupRepository->getById($groupId); $this->fail('An expected NoSuchEntityException was not thrown.'); } catch (NoSuchEntityException $e) { $exception = NoSuchEntityException::singleField(CustomerGroup::ID, $groupId); $this->assertEquals($exception->getMessage(), $e->getMessage(), 'Exception message does not match expected message.'); } }
/** * Load quote with different methods * * @param string $loadMethod * @param string $loadField * @param int $identifier * @param int[] $sharedStoreIds * @throws NoSuchEntityException * @return Quote */ protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = []) { /** @var Quote $quote */ $quote = $this->quoteFactory->create(); if ($sharedStoreIds) { $quote->setSharedStoreIds($sharedStoreIds); } $quote->setStoreId($this->storeManager->getStore()->getId())->{$loadMethod}($identifier); if (!$quote->getId()) { throw NoSuchEntityException::singleField($loadField, $identifier); } return $quote; }