/** * Converts a specified rate model to a shipping method data object. * * @param \Magento\Quote\Model\Quote\Item $item * @return array * @throws \Exception */ public function modelToDataObject($item) { $this->eventManager->dispatch('items_additional_data', ['item' => $item]); $items = $item->toArray(); $items['options'] = $this->getFormattedOptionValue($item); $itemsData = $this->totalsItemFactory->create(); $this->dataObjectHelper->populateWithArray($itemsData, $items, '\\Magento\\Quote\\Api\\Data\\TotalsItemInterface'); return $itemsData; }
/** * {@inheritdoc} */ public function getChildren($sku) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productRepository->get($sku); if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) { return []; } /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productTypeInstance */ $productTypeInstance = $product->getTypeInstance(); $productTypeInstance->setStoreFilter($product->getStoreId(), $product); $childrenList = []; /** @var \Magento\Catalog\Model\Product $child */ foreach ($productTypeInstance->getUsedProducts($product) as $child) { $attributes = []; foreach ($child->getAttributes() as $attribute) { $attrCode = $attribute->getAttributeCode(); $value = $child->getDataUsingMethod($attrCode) ?: $child->getData($attrCode); if (null !== $value && $attrCode != 'entity_id') { $attributes[$attrCode] = $value; } } $attributes['store_id'] = $child->getStoreId(); /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */ $productDataObject = $this->productFactory->create(); $this->dataObjectHelper->populateWithArray($productDataObject, $attributes, '\\Magento\\Catalog\\Api\\Data\\ProductInterface'); $childrenList[] = $productDataObject; } return $childrenList; }
public function setUp() { if (!function_exists('libxml_set_external_entity_loader')) { $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033'); } $this->customer = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\CustomerInterface', [], '', false, true, true); $this->customer->expects($this->once())->method('getWebsiteId')->willReturn(2); $this->customerDataFactory = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterfaceFactory', ['create'], [], '', false); $this->customerDataFactory->expects($this->once())->method('create')->willReturn($this->customer); $this->form = $this->getMock('Magento\\Customer\\Model\\Metadata\\Form', [], [], '', false); $this->request = $this->getMockForAbstractClass('Magento\\Framework\\App\\RequestInterface', [], '', false, true, true, ['getPost']); $this->response = $this->getMockForAbstractClass('Magento\\Framework\\App\\ResponseInterface', [], '', false); $this->formFactory = $this->getMock('Magento\\Customer\\Model\\Metadata\\FormFactory', ['create'], [], '', false); $this->formFactory->expects($this->atLeastOnce())->method('create')->willReturn($this->form); $this->extensibleDataObjectConverter = $this->getMock('Magento\\Framework\\Api\\ExtensibleDataObjectConverter', [], [], '', false); $this->dataObjectHelper = $this->getMock('Magento\\Framework\\Api\\DataObjectHelper', [], [], '', false); $this->dataObjectHelper->expects($this->once())->method('populateWithArray'); $this->customerAccountManagement = $this->getMockForAbstractClass('Magento\\Customer\\Api\\AccountManagementInterface', [], '', false, true, true); $this->resultJson = $this->getMock('Magento\\Framework\\Controller\\Result\\Json', [], [], '', false); $this->resultJson->expects($this->once())->method('setData'); $this->resultJsonFactory = $this->getMock('Magento\\Framework\\Controller\\Result\\JsonFactory', ['create'], [], '', false); $this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson); $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->controller = $objectHelper->getObject('Magento\\Customer\\Controller\\Adminhtml\\Index\\Validate', ['request' => $this->request, 'response' => $this->response, 'customerDataFactory' => $this->customerDataFactory, 'formFactory' => $this->formFactory, 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverter, 'customerAccountManagement' => $this->customerAccountManagement, 'resultJsonFactory' => $this->resultJsonFactory, 'dataObjectHelper' => $this->dataObjectHelper]); }
/** * @return \Magento\Customer\Api\Data\AddressInterface|string */ public function execute() { $customerAddressData = $this->getRequest()->getParam('address'); $customerAddressDataWithRegion = []; $customerAddressDataWithRegion['region']['region'] = $customerAddressData['region']; if (isset($customerAddressData['region_code'])) { $customerAddressDataWithRegion['region']['region_code'] = $customerAddressData['region_code']; } if ($customerAddressData['region_id']) { $customerAddressDataWithRegion['region']['region_id'] = $customerAddressData['region_id']; } $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion); /** * @var \Magento\Customer\Api\Data\AddressInterface $addressDataObject */ $addressDataObject = $this->customerAddressFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $customerAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addressValidationResponse = $this->validAddressManagement->saveValidAddress($addressDataObject, \Magento\Store\Model\Store::DEFAULT_STORE_ID); $resultJson = $this->resultJsonFactory->create(); if (!is_string($addressValidationResponse)) { $resultJson->setData([AddressInterface::FIRSTNAME => $addressValidationResponse->getFirstname(), AddressInterface::LASTNAME => $addressValidationResponse->getLastname(), AddressInterface::STREET => $addressValidationResponse->getStreet(), AddressInterface::COUNTRY_ID => $addressValidationResponse->getCountryId(), AddressInterface::CITY => $addressValidationResponse->getCity(), AddressInterface::REGION_ID => $addressValidationResponse->getRegionId(), AddressInterface::REGION => $addressValidationResponse->getRegion(), AddressInterface::POSTCODE => $addressValidationResponse->getPostcode()]); } else { $resultJson->setData($addressValidationResponse); } return $resultJson; }
/** * {@inheritdoc} */ public function save(CustomAttributesDataInterface $dataObjectWithCustomAttributes, $entityType, CustomAttributesDataInterface $previousCustomerData = null) { //Get all Image related custom attributes $imageDataObjects = $this->dataObjectHelper->getCustomAttributeValueByType($dataObjectWithCustomAttributes->getCustomAttributes(), '\\Magento\\Framework\\Api\\Data\\ImageContentInterface'); // Return if no images to process if (empty($imageDataObjects)) { return $dataObjectWithCustomAttributes; } // For every image, save it and replace it with corresponding Eav data object /** @var $imageDataObject \Magento\Framework\Api\AttributeValue */ foreach ($imageDataObjects as $imageDataObject) { /** @var $imageContent \Magento\Framework\Api\Data\ImageContentInterface */ $imageContent = $imageDataObject->getValue(); $filename = $this->processImageContent($entityType, $imageContent); //Set filename from static media location into data object $dataObjectWithCustomAttributes->setCustomAttribute($imageDataObject->getAttributeCode(), $filename); //Delete previously saved image if it exists if ($previousCustomerData) { $previousImageAttribute = $previousCustomerData->getCustomAttribute($imageDataObject->getAttributeCode()); if ($previousImageAttribute) { $previousImagePath = $previousImageAttribute->getValue(); if (!empty($previousImagePath)) { @unlink($this->mediaDirectory->getAbsolutePath() . $entityType . $previousImagePath); } } } } return $dataObjectWithCustomAttributes; }
/** * Create AttributeMetadata Data object from the Attribute Model * * @param \Magento\Customer\Model\Attribute $attribute * @return \Magento\Customer\Api\Data\AttributeMetadataInterface */ public function createMetadataAttribute($attribute) { $options = []; if ($attribute->usesSource()) { foreach ($attribute->getSource()->getAllOptions() as $option) { $optionDataObject = $this->optionFactory->create(); if (!is_array($option['value'])) { $optionDataObject->setValue($option['value']); } else { $optionArray = []; foreach ($option['value'] as $optionArrayValues) { $optionObject = $this->optionFactory->create(); $this->dataObjectHelper->populateWithArray($optionObject, $optionArrayValues, '\\Magento\\Customer\\Api\\Data\\OptionInterface'); $optionArray[] = $optionObject; } $optionDataObject->setOptions($optionArray); } $optionDataObject->setLabel($option['label']); $options[] = $optionDataObject; } } $validationRules = []; foreach ($attribute->getValidateRules() as $name => $value) { $validationRule = $this->validationRuleFactory->create()->setName($name)->setValue($value); $validationRules[] = $validationRule; } return $this->attributeMetadataFactory->create()->setAttributeCode($attribute->getAttributeCode())->setFrontendInput($attribute->getFrontendInput())->setInputFilter((string) $attribute->getInputFilter())->setStoreLabel($attribute->getStoreLabel())->setValidationRules($validationRules)->setIsVisible((bool) $attribute->getIsVisible())->setIsRequired((bool) $attribute->getIsRequired())->setMultilineCount((int) $attribute->getMultilineCount())->setDataModel((string) $attribute->getDataModel())->setOptions($options)->setFrontendClass($attribute->getFrontend()->getClass())->setFrontendLabel($attribute->getFrontendLabel())->setNote((string) $attribute->getNote())->setIsSystem((bool) $attribute->getIsSystem())->setIsUserDefined((bool) $attribute->getIsUserDefined())->setBackendType($attribute->getBackendType())->setSortOrder((int) $attribute->getSortOrder())->setIsUsedInGrid($attribute->getIsUsedInGrid())->setIsVisibleInGrid($attribute->getIsVisibleInGrid())->setIsFilterableInGrid($attribute->getIsFilterableInGrid())->setIsSearchableInGrid($attribute->getIsSearchableInGrid()); }
/** * @param \Magento\Quote\Model\Quote $quote * @return \Magento\Quote\Model\Quote */ public function prepareQuoteForNewCustomer(\Magento\Quote\Model\Quote $quote) { $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $billing->setDefaultBilling(true); if ($shipping && !$shipping->getSameAsBilling()) { $shipping->setDefaultShipping(true); $address = $shipping->exportCustomerAddress(); $shipping->setCustomerAddressData($address); } elseif ($shipping) { $billing->setDefaultShipping(true); } $address = $shipping->exportCustomerAddress(); $billing->setCustomerAddressData($address); foreach (['customer_dob', 'customer_taxvat', 'customer_gender'] as $attribute) { if ($quote->getData($attribute) && !$billing->getData($attribute)) { $billing->setData($attribute, $quote->getData($attribute)); } } $customer = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $this->copyObject->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setEmail($quote->getCustomerEmail()); $customer->setPrefix($quote->getCustomerPrefix()); $customer->setFirstname($quote->getCustomerFirstname()); $customer->setMiddlename($quote->getCustomerMiddlename()); $customer->setLastname($quote->getCustomerLastname()); $customer->setSuffix($quote->getCustomerSuffix()); $quote->setCustomer($customer); $quote->addCustomerAddress($billing->exportCustomerAddress()); if ($shipping->hasCustomerAddress()) { $quote->addCustomerAddress($shipping->exportCustomerAddress()); } return $quote; }
public function testGetChildren() { $productId = 'test'; $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productTypeInstance = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable')->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getTypeId')->willReturn(Configurable::TYPE_CODE); $product->expects($this->any())->method('getStoreId')->willReturn(1); $product->expects($this->any())->method('getTypeInstance')->willReturn($productTypeInstance); $productTypeInstance->expects($this->once())->method('setStoreFilter')->with(1, $product); $childProduct = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productTypeInstance->expects($this->any())->method('getUsedProducts')->with($product)->willReturn([$childProduct]); $this->productRepository->expects($this->any())->method('get')->with($productId)->willReturn($product); $attribute = $this->getMock('\\Magento\\Eav\\Api\\Data\\AttributeInterface'); $attribute->expects($this->once())->method('getAttributeCode')->willReturn('code'); $childProduct->expects($this->once())->method('getDataUsingMethod')->with('code')->willReturn(false); $childProduct->expects($this->once())->method('getData')->with('code')->willReturn(10); $childProduct->expects($this->once())->method('getStoreId')->willReturn(1); $childProduct->expects($this->once())->method('getAttributes')->willReturn([$attribute]); $productMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\ProductInterface'); $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray')->with($productMock, ['store_id' => 1, 'code' => 10], '\\Magento\\Catalog\\Api\\Data\\ProductInterface')->willReturnSelf(); $this->productFactory->expects($this->once())->method('create')->willReturn($productMock); $products = $this->object->getChildren($productId); $this->assertCount(1, $products); $this->assertEquals($productMock, $products[0]); }
/** * @param Address $object * @param array $data * @return OrderInterface */ public function convert(Address $object, $data = []) { $orderData = $this->objectCopyService->getDataFromFieldset( 'quote_convert_address', 'to_order', $object ); /** * @var $order \Magento\Sales\Model\Order */ $order = $this->orderFactory->create(); $this->dataObjectHelper->populateWithArray( $order, array_merge($orderData, $data), '\Magento\Sales\Api\Data\OrderInterface' ); $order->setStoreId($object->getQuote()->getStoreId()) ->setQuoteId($object->getQuote()->getId()) ->setIncrementId($object->getQuote()->getReservedOrderId()); $this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $object->getQuote(), $order); $this->eventManager->dispatch( 'sales_convert_quote_to_order', ['order' => $order, 'quote' => $object->getQuote()] ); return $order; }
/** * @param Address $object * @param array $data * @return OrderAddressInterface */ public function convert(Address $object, $data = []) { $orderAddress = $this->orderAddressRepository->create(); $orderAddressData = $this->objectCopyService->getDataFromFieldset('quote_convert_address', 'to_order_address', $object); $this->dataObjectHelper->populateWithArray($orderAddress, array_merge($orderAddressData, $data), '\\Magento\\Sales\\Api\\Data\\OrderAddressInterface'); return $orderAddress; }
/** * {@inheritDoc} * * @param int $cartId The cart ID. * @return Totals Quote totals data. */ public function get($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual()) { $addressTotalsData = $quote->getBillingAddress()->getData(); $addressTotals = $quote->getBillingAddress()->getTotals(); } else { $addressTotalsData = $quote->getShippingAddress()->getData(); $addressTotals = $quote->getShippingAddress()->getTotals(); } /** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */ $quoteTotals = $this->totalsFactory->create(); $this->dataObjectHelper->populateWithArray($quoteTotals, $addressTotalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface'); $items = []; foreach ($quote->getAllVisibleItems() as $index => $item) { $items[$index] = $this->itemConverter->modelToDataObject($item); } $calculatedTotals = $this->totalsConverter->process($addressTotals); $quoteTotals->setTotalSegments($calculatedTotals); $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount(); $amount = $amount > 0 ? $amount : 0; $quoteTotals->setCouponCode($this->couponService->get($cartId)); $quoteTotals->setGrandTotal($amount); $quoteTotals->setItems($items); $quoteTotals->setItemsQty($quote->getItemsQty()); $quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode()); $quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode()); return $quoteTotals; }
/** * {@inheritdoc} */ public function hydrate($entity, array $data) { $entityType = $this->typeResolver->resolve($entity); $mapper = $this->mapperPool->getMapper($entityType); $data = $mapper->databaseToEntity($entityType, array_merge($this->extract($entity), $data)); $this->dataObjectHelper->populateWithArray($entity, $data, $entityType); return $entity; }
public function testFindOneByDataIfFound() { $data = [['field1' => 'value1']]; $row = ['row1']; $urlRewrite = ['urlRewrite1']; $this->storage->expects($this->once())->method('doFindOneByData')->with($data)->will($this->returnValue($row)); $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with($urlRewrite, $row, '\\Magento\\UrlRewrite\\Service\\V1\\Data\\UrlRewrite')->will($this->returnSelf()); $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($urlRewrite)); $this->assertEquals($urlRewrite, $this->storage->findOneByData($data)); }
public function testGetItems() { $inputTypeMock = $this->getMock('Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Inputtype', [], [], '', false); $this->inputTypeFactoryMock->expects($this->once())->method('create')->willReturn($inputTypeMock); $inputTypeMock->expects($this->once())->method('toOptionArray')->willReturn(['option' => ['value']]); $attributeTypeMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\ProductAttributeTypeInterface'); $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray')->with($attributeTypeMock, ['value'], '\\Magento\\Catalog\\Api\\Data\\ProductAttributeTypeInterface')->willReturnSelf(); $this->attributeTypeFactoryMock->expects($this->once())->method('create')->willReturn($attributeTypeMock); $this->assertEquals([$attributeTypeMock], $this->model->getItems()); }
/** * {@inheritdoc} */ public function convertToProductOption(DataObject $request) { /** @var DownloadableOption $downloadableOption */ $downloadableOption = $this->downloadableOptionFactory->create(); $links = $request->getLinks(); if (!empty($links) && is_array($links)) { $this->dataObjectHelper->populateWithArray($downloadableOption, ['downloadable_links' => $links], 'Magento\\Downloadable\\Api\\Data\\DownloadableOptionInterface'); return ['downloadable_option' => $downloadableOption]; } return []; }
/** * @param Payment $object * @param array $data * @return OrderPaymentInterface */ public function convert(Payment $object, $data = []) { $paymentData = $this->objectCopyService->getDataFromFieldset('quote_convert_payment', 'to_order_payment', $object); $orderPayment = $this->orderPaymentFactory->create(); $this->dataObjectHelper->populateWithArray($orderPayment, array_merge($paymentData, $data), '\\Magento\\Sales\\Api\\Data\\OrderPaymentInterface'); $orderPayment->setAdditionalInformation(serialize(array_merge($object->getAdditionalInformation(), [Substitution::INFO_KEY_TITLE => $object->getMethodInstance()->getTitle()]))); // set directly on the model $orderPayment->setCcNumber($object->getCcNumber()); $orderPayment->setCcCid($object->getCcCid()); return $orderPayment; }
/** * {@inheritdoc} */ public function getItems() { $types = []; $inputType = $this->inputTypeFactory->create(); foreach ($inputType->toOptionArray() as $option) { $type = $this->attributeTypeFactory->create(); $this->dataObjectHelper->populateWithArray($type, $option, '\\Magento\\Catalog\\Api\\Data\\ProductAttributeTypeInterface'); $types[] = $type; } return $types; }
public function testFindOneByData() { $data = ['col1' => 'val1', 'col2' => 'val2']; $this->select->expects($this->at(1))->method('where')->with('col1 IN (?)', 'val1'); $this->select->expects($this->at(2))->method('where')->with('col2 IN (?)', 'val2'); $this->adapter->expects($this->any())->method('quoteIdentifier')->will($this->returnArgument(0)); $this->adapter->expects($this->once())->method('fetchRow')->with($this->select)->will($this->returnValue(['row1'])); $this->dataObjectHelper->expects($this->at(0))->method('populateWithArray')->with(['urlRewrite1'], ['row1'], '\\Magento\\UrlRewrite\\Service\\V1\\Data\\UrlRewrite')->will($this->returnSelf()); $this->urlRewriteFactory->expects($this->at(0))->method('create')->will($this->returnValue(['urlRewrite1'])); $this->assertEquals(['urlRewrite1'], $this->storage->findOneByData($data)); }
public function testConvert() { $orderData = ['test' => 'test1']; $data = ['test' => 'beer']; /** * @var \Magento\Quote\Model\Quote\Address $object */ $object = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false); $this->objectCopyMock->expects($this->once())->method('getDataFromFieldset')->with('sales_convert_quote_address', 'to_order_address', $object)->willReturn($orderData); $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with($this->orderInterfaceMock, ['test' => 'beer'], '\\Magento\\Sales\\Api\\Data\\OrderAddressInterface')->willReturnSelf(); $this->orderAddressRepositoryMock->expects($this->once())->method('create')->willReturn($this->orderInterfaceMock); $this->assertSame($this->orderInterfaceMock, $this->converter->convert($object, $data)); }
/** * @param Product $product * @param array $rowData * @return ProductAttributeMediaGalleryEntryInterface $entry */ public function convertTo(Product $product, array $rowData) { $image = $rowData; $productImages = $product->getMediaAttributeValues(); if (!isset($image['types'])) { $image['types'] = array_keys($productImages, $image['file']); } $entry = $this->mediaGalleryEntryFactory->create(); $this->dataObjectHelper->populateWithArray($entry, $image, '\\Magento\\Catalog\\Api\\Data\\ProductAttributeMediaGalleryEntryInterface'); if (isset($image['value_id'])) { $entry->setId($image['value_id']); } return $entry; }
/** * Helper to create tax rules. * * @param array $rulesData Keys match populateWithArray * @return array code => rule id */ public function createTaxRules($rulesData) { /** @var \Magento\Tax\Api\Data\TaxRuleInterfaceFactory $taxRuleFactory */ $taxRuleFactory = $this->objectManager->create('Magento\\Tax\\Api\\Data\\TaxRuleInterfaceFactory'); /** @var \Magento\Tax\Api\TaxRuleRepositoryInterface $taxRuleService */ $taxRuleService = $this->objectManager->create('Magento\\Tax\\Api\\TaxRuleRepositoryInterface'); $rules = []; foreach ($rulesData as $ruleData) { $taxRule = $taxRuleFactory->create(); $this->dataObjectHelper->populateWithArray($taxRule, $ruleData, '\\Magento\\Tax\\Api\\Data\\TaxRuleInterface'); $rules[$ruleData['code']] = $taxRuleService->save($taxRule)->getId(); } return $rules; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return \Magento\Bundle\Api\Data\OptionInterface[] */ public function getItems(\Magento\Catalog\Api\Data\ProductInterface $product) { $optionCollection = $this->type->getOptionsCollection($product); $optionList = []; /** @var \Magento\Bundle\Model\Option $option */ foreach ($optionCollection as $option) { $productLinks = $this->linkList->getItems($product, $option->getOptionId()); /** @var \Magento\Bundle\Api\Data\OptionInterface $optionDataObject */ $optionDataObject = $this->optionFactory->create(); $this->dataObjectHelper->populateWithArray($optionDataObject, $option->getData(), '\\Magento\\Bundle\\Api\\Data\\OptionInterface'); $optionDataObject->setOptionId($option->getOptionId())->setTitle($option->getTitle() === null ? $option->getDefaultTitle() : $option->getTitle())->setSku($product->getSku())->setProductLinks($productLinks); $optionList[] = $optionDataObject; } return $optionList; }
/** * {@inheritDoc} * * @param int $cartId The cart ID. * @return Totals Quote totals data. */ public function get($cartId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $shippingAddress = $quote->getShippingAddress(); $totalsData = array_merge($shippingAddress->getData(), $quote->getData()); $totals = $this->totalsFactory->create(); $this->dataObjectHelper->populateWithArray($totals, $totalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface'); $totals->setItems($quote->getAllItems()); return $totals; }
public function testPrepareQuoteForNewCustomer() { $customerAddressMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $this->addressMock->expects($this->any())->method('exportCustomerAddress')->willReturn($customerAddressMock); $this->addressMock->expects($this->any())->method('getData')->willReturn([]); $this->addressFactoryMock->expects($this->any())->method('create')->willReturn($this->addressMock); $this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(false); $this->quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($this->addressMock); $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->addressMock); $customerDataMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerDataMock); $this->copyObjectMock->expects($this->any())->method('getDataFromFieldset')->willReturn([]); $this->dataObjectHelper->expects($this->any())->method('populateWithArray')->willReturnSelf(); $this->assertInstanceOf('Magento\\Quote\\Model\\Quote', $this->quote->prepareQuoteForNewCustomer($this->quoteMock)); }
public function testGetTotals() { $cartId = 12; $this->quoteRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->addressMock); $this->addressMock->expects($this->once())->method('getData')->willReturn(['addressData']); $this->quoteMock->expects($this->once())->method('getData')->willReturn(['quoteData']); $item = $this->getMock('Magento\\Quote\\Model\\Quote\\Item', [], [], '', false); $this->quoteMock->expects($this->once())->method('getAllItems')->will($this->returnValue([$item])); $totals = $this->getMock('Magento\\Quote\\Model\\Cart\\Totals', ['setItems'], [], '', false); $this->totalsFactoryMock->expects($this->once())->method('create')->willReturn($totals); $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray'); $totals->expects($this->once())->method('setItems'); $this->model->get($cartId); }
public function testUpdateCustomerException() { $customerData = $this->_createCustomer(); $existingCustomerDataObject = $this->_getCustomerData($customerData[Customer::ID]); $lastName = $existingCustomerDataObject->getLastname(); //Set non-existent id = -1 $customerData[Customer::LASTNAME] = $lastName . 'Updated'; $customerData[Customer::ID] = -1; $newCustomerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($newCustomerDataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH . "/-1", 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => self::SERVICE_NAME . 'Save']]; $newCustomerDataObject = $this->dataObjectProcessor->buildOutputDataArray($newCustomerDataObject, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); $requestData = ['customer' => $newCustomerDataObject]; $expectedMessage = 'No such entity with %fieldName = %fieldValue'; try { $this->_webApiCall($serviceInfo, $requestData); $this->fail("Expected exception."); } catch (\SoapFault $e) { $this->assertContains($expectedMessage, $e->getMessage(), "SoapFault does not contain expected message."); } catch (\Exception $e) { $errorObj = $this->processRestExceptionResult($e); $this->assertEquals($expectedMessage, $errorObj['message']); $this->assertEquals(['fieldName' => 'customerId', 'fieldValue' => -1], $errorObj['parameters']); $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode()); } }
public function testCalculateTax() { $storeId = 3; $algorithm = 'algorithm'; $customerId = 100; $taxClassId = 200; $taxDetailsData = [\Magento\Tax\Model\TaxDetails\TaxDetails::KEY_SUBTOTAL => 0.0, \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_TAX_AMOUNT => 0.0, \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_DISCOUNT_TAX_COMPENSATION_AMOUNT => 0.0, \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_APPLIED_TAXES => [], \Magento\Tax\Model\TaxDetails\TaxDetails::KEY_ITEMS => []]; $quoteDetailsMock = $this->getMock('\\Magento\\Tax\\Api\\Data\\QuoteDetailsInterface'); $storeMock = $this->getMock('\\Magento\\Store\\Model\\Store', ['getStoreId'], [], '', false); $this->storeManager->expects($this->once())->method('getStore')->willReturn($storeMock); $storeMock->expects($this->once())->method('getStoreId')->willReturn($storeId); $billAddressMock = $this->getMock('Magento\\Customer\\Api\\Data\\AddressInterface', [], [], '', false); $shipAddressMock = $this->getMock('Magento\\Customer\\Api\\Data\\AddressInterface', [], [], '', false); $taxClassKeyMock = $this->getMock('\\Magento\\Tax\\Api\\Data\\TaxClassKeyInterface'); $quoteDetailsItemMock = $this->getMock('\\Magento\\Tax\\Api\\Data\\QuoteDetailsItemInterface'); $quoteDetailsMock->expects($this->once())->method('getItems')->willReturn([$quoteDetailsItemMock]); $quoteDetailsMock->expects($this->once())->method('getBillingAddress')->willReturn($billAddressMock); $quoteDetailsMock->expects($this->once())->method('getShippingAddress')->willReturn($shipAddressMock); $quoteDetailsMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $quoteDetailsMock->expects($this->once())->method('getCustomerTaxClassKey')->willReturn($taxClassKeyMock); $this->configMock->expects($this->once())->method('getAlgorithm')->with($storeId)->willReturn($algorithm); $this->taxClassManagementMock->expects($this->once())->method('getTaxClassId')->with($taxClassKeyMock, 'customer')->willReturn($taxClassId); $calculatorMock = $this->getMock('Magento\\Tax\\Model\\Calculation\\TotalBaseCalculator', [], [], '', false); $this->calculatorFactory->expects($this->once())->method('create')->with($algorithm, $storeId, $billAddressMock, $shipAddressMock, $taxClassId, $customerId)->willReturn($calculatorMock); $taxDetailsMock = $this->getMock('\\Magento\\Tax\\Api\\Data\\TaxDetailsItemInterface'); $calculatorMock->expects($this->once())->method('calculate')->willReturn($taxDetailsMock); $taxDetailsMock = $this->getMock('\\Magento\\Tax\\Api\\Data\\TaxDetailsInterface'); $this->taxDetailsDataObjectFactory->expects($this->once())->method('create')->willReturn($taxDetailsMock); $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray')->with($taxDetailsMock, $taxDetailsData)->willReturnSelf(); $this->assertEquals($taxDetailsMock, $this->taxCalculationService->calculateTax($quoteDetailsMock)); }
/** * Add address to customer during create account * * @return AddressInterface|null */ protected function extractAddress() { if (!$this->getRequest()->getPost('create_address')) { return null; } $addressForm = $this->formFactory->create('customer_address', 'customer_register_address'); $allowedAttributes = $addressForm->getAllowedAttributes(); $addressData = []; $regionDataObject = $this->regionDataFactory->create(); foreach ($allowedAttributes as $attribute) { $attributeCode = $attribute->getAttributeCode(); $value = $this->getRequest()->getParam($attributeCode); if ($value === null) { continue; } switch ($attributeCode) { case 'region_id': $regionDataObject->setRegionId($value); break; case 'region': $regionDataObject->setRegion($value); break; default: $addressData[$attributeCode] = $value; } } $addressDataObject = $this->addressDataFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addressDataObject->setRegion($regionDataObject); $addressDataObject->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false)); return $addressDataObject; }
public function testSetAccountData() { $taxClassId = 1; $attributes = [['email', '*****@*****.**'], ['group_id', 1]]; $attributeMocks = []; foreach ($attributes as $attribute) { $attributeMock = $this->getMock('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface', [], [], '', false); $attributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attribute[0])); $attributeMocks[] = $attributeMock; } $customerGroupMock = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\GroupInterface', [], '', false, true, true, ['getTaxClassId']); $customerGroupMock->expects($this->once())->method('getTaxClassId')->will($this->returnValue($taxClassId)); $customerFormMock = $this->getMock('Magento\\Customer\\Model\\Metadata\\Form', [], [], '', false); $customerFormMock->expects($this->any())->method('getAttributes')->will($this->returnValue($attributeMocks)); $customerFormMock->expects($this->any())->method('extractData')->will($this->returnValue([])); $customerFormMock->expects($this->any())->method('restoreData')->will($this->returnValue([])); $customerFormMock->expects($this->any())->method('prepareRequest')->will($this->returnValue($this->getMock('Magento\\Framework\\App\\RequestInterface'))); $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $this->customerMapper->expects($this->atLeastOnce())->method('toFlatArray')->willReturn(['email' => '*****@*****.**', 'group_id' => 1, 'gender' => 1]); $quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', [], [], '', false); $quoteMock->expects($this->any())->method('getCustomer')->will($this->returnValue($customerMock)); $quoteMock->expects($this->once())->method('addData')->with(['customer_email' => $attributes[0][1], 'customer_group_id' => $attributes[1][1], 'customer_tax_class_id' => $taxClassId]); $this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with($customerMock, ['email' => '*****@*****.**', 'group_id' => 1, 'gender' => 1], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerFormMock)); $this->sessionQuoteMock->expects($this->any())->method('getQuote')->will($this->returnValue($quoteMock)); $this->customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerMock)); $this->groupRepositoryMock->expects($this->once())->method('getById')->will($this->returnValue($customerGroupMock)); $this->adminOrderCreate->setAccountData([]); }
/** * Copies a quote address to a customer address * * Ths function serves to create a customer address from a quote address because when a user is logged in and they * go to checkout, if they select an existing customer address the checkout module will retrieve that customer * address to set as the quote address. Rather that update the quote address every time at checkout, this module * will update the customer address (if the user selects the valid address) so that customer address only needs * to be validated once. To do this, the quote address must be converted to a customer address so the validated * address can be saved to the quote address. * * @param QuoteAddressInterface $quoteAddress * @param CustomerAddressInterface $customerAddress * @return null|CustomerAddressInterface */ public function copyQuoteAddressToCustomerAddress(QuoteAddressInterface $quoteAddress, CustomerAddressInterface $customerAddress) { $customerAddress->setRegionId($quoteAddress->getRegionId()); $customerAddress->setCountryId($quoteAddress->getCountryId()); $customerAddress->setStreet($quoteAddress->getStreet()); $customerAddress->setPostcode($quoteAddress->getPostcode()); $customerAddress->setCity($quoteAddress->getCity()); $customerAddressData = $this->getCustomerAddressData($customerAddress); $customerAddressDataWithRegion = []; $customerAddressDataWithRegion['region']['region'] = $quoteAddress->getRegion(); $customerAddressDataWithRegion['region']['region_code'] = $quoteAddress->getRegionCode(); if ($customerAddressData['region_id']) { $customerAddressDataWithRegion['region']['region_id'] = $quoteAddress->getRegionId(); } $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion); // Per Github issue #4, the \Magento\Framework\Api\DataObjectHelper::_setDataValues method chokes on the // custom_attributes array, so remove it and set it after the data has been mapped $customAttributes = false; if (isset($customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]) && is_array($customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) { $customAttributes = $customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]; unset($customerAddressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]); } $addressDataObject = $this->customerAddressFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $customerAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); if ($customAttributes) { $addressDataObject->setCustomAttributes($customAttributes); } return $addressDataObject; }