Esempio n. 1
0
 /**
  * @param \Magento\Sales\Model\Quote\Address $address
  * @return \Magento\Checkout\Service\V1\Data\Cart\Address
  */
 public function convertModelToDataObject(\Magento\Sales\Model\Quote\Address $address)
 {
     $data = [Address::KEY_COUNTRY_ID => $address->getCountryId(), Address::KEY_ID => $address->getId(), Address::KEY_CUSTOMER_ID => $address->getCustomerId(), Address::KEY_REGION => array(Region::KEY_REGION => $address->getRegion(), Region::KEY_REGION_ID => $address->getRegionId(), Region::KEY_REGION_CODE => $address->getRegionCode()), Address::KEY_STREET => $address->getStreet(), Address::KEY_COMPANY => $address->getCompany(), Address::KEY_TELEPHONE => $address->getTelephone(), Address::KEY_FAX => $address->getFax(), Address::KEY_POSTCODE => $address->getPostcode(), Address::KEY_CITY => $address->getCity(), Address::KEY_FIRSTNAME => $address->getFirstname(), Address::KEY_LASTNAME => $address->getLastname(), Address::KEY_MIDDLENAME => $address->getMiddlename(), Address::KEY_PREFIX => $address->getPrefix(), Address::KEY_SUFFIX => $address->getSuffix(), Address::KEY_EMAIL => $address->getEmail(), Address::KEY_VAT_ID => $address->getVatId()];
     foreach ($this->metadataService->getCustomAttributesMetadata() as $attributeMetadata) {
         $attributeCode = $attributeMetadata->getAttributeCode();
         $method = 'get' . SimpleDataObjectConverter::snakeCaseToCamelCase($attributeCode);
         $data[Address::CUSTOM_ATTRIBUTES_KEY][] = [AttributeValue::ATTRIBUTE_CODE => $attributeCode, AttributeValue::VALUE => $address->{$method}()];
     }
     return $this->addressBuilder->populateWithArray($data)->create();
 }
Esempio n. 2
0
 public function testCall()
 {
     $requestedServices = array('requestedServices');
     $this->_requestMock->expects($this->once())->method('getRequestedServices')->will($this->returnValue($requestedServices));
     $this->_dataObjectConverter->expects($this->once())->method('convertStdObjectToArray')->will($this->returnValue(['field' => 1]));
     $operationName = 'soapOperation';
     $className = 'Magento\\Framework\\Object';
     $methodName = 'testMethod';
     $isSecure = false;
     $aclResources = array('Magento_TestModule::resourceA');
     $this->_apiConfigMock->expects($this->once())->method('getServiceMethodInfo')->with($operationName, $requestedServices)->will($this->returnValue(array(SoapConfig::KEY_CLASS => $className, SoapConfig::KEY_METHOD => $methodName, SoapConfig::KEY_IS_SECURE => $isSecure, SoapConfig::KEY_ACL_RESOURCES => $aclResources)));
     $this->_authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
     $serviceMock = $this->getMockBuilder($className)->disableOriginalConstructor()->setMethods(array($methodName))->getMock();
     $serviceResponse = array('foo' => 'bar');
     $serviceMock->expects($this->once())->method($methodName)->will($this->returnValue($serviceResponse));
     $this->_objectManagerMock->expects($this->once())->method('get')->with($className)->will($this->returnValue($serviceMock));
     $this->_serializerMock->expects($this->once())->method('getInputData')->will($this->returnArgument(2));
     /** Execute SUT. */
     $this->assertEquals(array('result' => $serviceResponse), $this->_handler->__call($operationName, array((object) array('field' => 1))));
 }
Esempio n. 3
0
 /**
  * Convert service response into format acceptable by SoapServer.
  *
  * @param object|array|string|int|float|null $data
  * @return array
  * @throws \InvalidArgumentException
  */
 protected function _prepareResponseData($data)
 {
     if ($data instanceof AbstractSimpleObject) {
         $result = $this->_dataObjectConverter->convertKeysToCamelCase($data->__toArray());
     } elseif (is_array($data)) {
         foreach ($data as $key => $value) {
             $result[$key] = $value instanceof AbstractSimpleObject ? $this->_dataObjectConverter->convertKeysToCamelCase($value->__toArray()) : $value;
         }
     } elseif (is_scalar($data) || is_null($data)) {
         $result = $data;
     } else {
         throw new \InvalidArgumentException("Service returned result in invalid format.");
     }
     return array(self::RESULT_NODE_NAME => $result);
 }
Esempio n. 4
0
 /**
  * Load customer group collection data from service
  *
  * @param bool $printQuery
  * @param bool $logQuery
  * @return $this
  */
 public function loadData($printQuery = false, $logQuery = false)
 {
     if (!$this->isLoaded()) {
         $searchCriteria = $this->getSearchCriteria();
         $searchResults = $this->groupService->searchGroups($searchCriteria);
         $this->_totalRecords = $searchResults->getTotalCount();
         /** @var CustomerGroup[] $groups */
         $groups = $searchResults->getItems();
         foreach ($groups as $group) {
             $groupItem = new \Magento\Framework\Object();
             $groupItem->addData(\Magento\Framework\Service\SimpleDataObjectConverter::toFlatArray($group));
             $this->_addItem($groupItem);
         }
         $this->_setIsLoaded();
     }
     return $this;
 }
Esempio n. 5
0
 public function testConvertSoapStdObjectToArray()
 {
     $stdObject = json_decode(json_encode($this->getCustomerDetails()->__toArray()), false);
     $addresses = $stdObject->addresses;
     unset($stdObject->addresses);
     $stdObject->addresses = new \stdClass();
     $stdObject->addresses->item = $addresses;
     $response = $this->dataObjectConverter->convertStdObjectToArray($stdObject);
     //Check array conversion
     $this->assertTrue(is_array($response['customer']));
     $this->assertTrue(is_array($response['addresses']));
     $this->assertEquals(2, count($response['addresses']['item']));
     //Check if data is correct
     $this->assertEquals(self::FIRSTNAME, $response['customer']['firstname']);
     $this->assertEquals(self::GROUP_ID, $response['customer']['group_id']);
     foreach ($response['addresses']['item'] as $key => $address) {
         $region = $address['region'];
         $this->assertEquals(self::REGION, $region['region']);
         $this->assertEquals(self::REGION_CODE, $region['region_code']);
         $this->assertEquals(self::REGION_ID, $region['region_id']);
         $this->assertEquals($this->expectedStreet[$key], $address['street']);
     }
 }
Esempio n. 6
0
 /**
  * Handle REST request
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return \Magento\Framework\App\ResponseInterface
  */
 public function dispatch(\Magento\Framework\App\RequestInterface $request)
 {
     $path = $this->_pathProcessor->process($request->getPathInfo());
     $this->_request->setPathInfo($path);
     $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE);
     try {
         $this->checkPermissions();
         $route = $this->getCurrentRoute();
         if ($route->isSecure() && !$this->_request->isSecure()) {
             throw new \Magento\Webapi\Exception(__('Operation allowed only in HTTPS'));
         }
         /** @var array $inputData */
         $inputData = $this->_request->getRequestData();
         $serviceMethodName = $route->getServiceMethod();
         $serviceClassName = $route->getServiceClass();
         $inputData = $this->overrideParams($inputData, $route->getParameters());
         $inputParams = $this->_serializer->getInputData($serviceClassName, $serviceMethodName, $inputData);
         $service = $this->_objectManager->get($serviceClassName);
         /** @var \Magento\Framework\Service\Data\AbstractExtensibleObject $outputData */
         $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams);
         $outputData = $this->dataObjectConverter->processServiceOutput($outputData);
         if ($this->_request->getParam(PartialResponseProcessor::FILTER_PARAMETER) && is_array($outputData)) {
             $outputData = $this->partialResponseProcessor->filter($outputData);
         }
         $this->_response->prepareResponse($outputData);
     } catch (\Exception $e) {
         $maskedException = $this->_errorProcessor->maskException($e);
         $this->_response->setException($maskedException);
     }
     return $this->_response;
 }
Esempio n. 7
0
 /**
  * @param string $field
  * @param StockItem $dataObject
  * @return mixed
  * @throws \BadMethodCallException
  */
 public function getDoFieldData($field, StockItem $dataObject)
 {
     $possibleMethods = array('get' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToCamelCase($field), 'is' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToCamelCase($field));
     foreach ($possibleMethods as $method) {
         if (method_exists($dataObject, $method)) {
             return $dataObject->{$method}();
         }
     }
     throw new \BadMethodCallException(__('Field "%1" was not found in DO "%2".', $field, get_class($dataObject)));
 }
Esempio n. 8
0
 /**
  * Add rendering EAV attributes to Form element
  *
  * @param \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata[] $attributes
  * @param \Magento\Framework\Data\Form\AbstractForm $form
  * @return $this
  */
 protected function _addAttributesToForm($attributes, \Magento\Framework\Data\Form\AbstractForm $form)
 {
     // add additional form types
     $types = $this->_getAdditionalFormElementTypes();
     foreach ($types as $type => $className) {
         $form->addType($type, $className);
     }
     $renderers = $this->_getAdditionalFormElementRenderers();
     foreach ($attributes as $attribute) {
         $inputType = $attribute->getFrontendInput();
         if ($inputType) {
             $element = $form->addField($attribute->getAttributeCode(), $inputType, array('name' => $attribute->getAttributeCode(), 'label' => __($attribute->getStoreLabel()), 'class' => $attribute->getFrontendClass(), 'required' => $attribute->isRequired()));
             if ($inputType == 'multiline') {
                 $element->setLineCount($attribute->getMultilineCount());
             }
             $element->setEntityAttribute($attribute);
             $this->_addAdditionalFormElementData($element);
             if (!empty($renderers[$attribute->getAttributeCode()])) {
                 $element->setRenderer($renderers[$attribute->getAttributeCode()]);
             }
             if ($inputType == 'select' || $inputType == 'multiselect') {
                 $options = array();
                 foreach ($attribute->getOptions() as $optionData) {
                     $options[] = \Magento\Framework\Service\SimpleDataObjectConverter::toFlatArray($optionData);
                 }
                 $element->setValues($options);
             } elseif ($inputType == 'date') {
                 $format = $this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT);
                 $element->setImage($this->getViewFileUrl('images/grid-cal.gif'));
                 $element->setDateFormat($format);
             }
         }
     }
     return $this;
 }
Esempio n. 9
0
 /**
  * Initialize customer form
  *
  * @return \Magento\Customer\Model\Metadata\Form $customerForm
  */
 protected function _getCustomerForm()
 {
     if (is_null($this->_customerForm)) {
         $this->_customerForm = $this->_customerFormFactory->create('customer', 'adminhtml_customer', SimpleDataObjectConverter::toFlatArray($this->_getCustomerDataObject()));
     }
     return $this->_customerForm;
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_OBJECT_CLASS_NAME)
 {
     $customAttributes = [];
     if (!$this->customerDataObjectMethods) {
         $this->customerDataObjectMethods = array_flip(get_class_methods($dataObjectClassName));
     }
     foreach ($this->getAllAttributesMetadata() as $attributeMetadata) {
         $attributeCode = $attributeMetadata->getAttributeCode();
         $camelCaseKey = \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToCamelCase($attributeCode);
         $isDataObjectMethod = isset($this->customerDataObjectMethods['get' . $camelCaseKey]) || isset($this->customerDataObjectMethods['is' . $camelCaseKey]);
         /** Even though disable_auto_group_change is system attribute, it should be available to the clients */
         if (!$isDataObjectMethod && (!$attributeMetadata->isSystem() || $attributeCode == 'disable_auto_group_change')) {
             $customAttributes[] = $attributeMetadata;
         }
     }
     return array_merge($customAttributes, $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName));
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_OBJECT_CLASS_NAME)
 {
     $customAttributes = [];
     if (!$this->addressDataObjectMethods) {
         $this->addressDataObjectMethods = array_flip(get_class_methods($dataObjectClassName));
     }
     foreach ($this->getAllAttributesMetadata() as $attributeMetadata) {
         $attributeCode = $attributeMetadata->getAttributeCode();
         $camelCaseKey = \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToCamelCase($attributeCode);
         $isDataObjectMethod = isset($this->addressDataObjectMethods['get' . $camelCaseKey]) || isset($this->addressDataObjectMethods['is' . $camelCaseKey]);
         if (!$isDataObjectMethod && !$attributeMetadata->isSystem()) {
             $customAttributes[] = $attributeMetadata;
         }
     }
     return array_merge($customAttributes, $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName));
 }
Esempio n. 12
0
 /**
  * Initializes Data Object with the data from array
  *
  * @param array $data
  * @return $this
  */
 protected function _setDataValues(array $data)
 {
     $dataObjectMethods = get_class_methods($this->_getDataObjectType());
     foreach ($data as $key => $value) {
         /* First, verify is there any getter for the key on the Service Data Object */
         $camelCaseKey = \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToCamelCase($key);
         $possibleMethods = array('get' . $camelCaseKey, 'is' . $camelCaseKey);
         if ($key == AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY && is_array($data[$key]) && !empty($data[$key])) {
             foreach ($data[$key] as $customAttribute) {
                 $this->setCustomAttribute($customAttribute[AttributeValue::ATTRIBUTE_CODE], $customAttribute[AttributeValue::VALUE]);
             }
         } elseif (array_intersect($possibleMethods, $dataObjectMethods)) {
             $this->_data[$key] = $value;
         } else {
             $this->setCustomAttribute($key, $value);
         }
     }
     return $this;
 }
 /**
  * Convert custom attribute data array to array of AttributeValue Data Object
  *
  * @param array $customAttributesValueArray
  * @param string $returnType
  * @param string $dataObjectClassName
  * @return AttributeValue[]
  */
 protected function convertCustomAttributeValue($customAttributesValueArray, $returnType, $dataObjectClassName)
 {
     $result = [];
     $allAttributes = $this->serviceConfigReader->read();
     $dataObjectClassName = ltrim($dataObjectClassName, '\\');
     if (!isset($allAttributes[$dataObjectClassName])) {
         return $this->_convertValue($customAttributesValueArray, $returnType);
     }
     $dataObjectAttributes = $allAttributes[$dataObjectClassName];
     $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE));
     foreach ($customAttributesValueArray as $customAttribute) {
         if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) {
             $customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE];
         } else {
             if (isset($customAttribute[$camelCaseAttributeCodeKey])) {
                 $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
             } else {
                 $customAttributeCode = null;
             }
         }
         //Check if type is defined, else default to mixed
         $type = isset($dataObjectAttributes[$customAttributeCode]) ? $dataObjectAttributes[$customAttributeCode] : TypeProcessor::ANY_TYPE;
         $customAttributeValue = $customAttribute[AttributeValue::VALUE];
         if (is_array($customAttributeValue)) {
             //If type for AttributeValue's value as array is mixed, further processing is not possible
             if ($type === TypeProcessor::ANY_TYPE) {
                 continue;
             }
             //If custom attribute value is an array then its a data object type
             $attributeValue = $this->_createFromArray($type, $customAttributeValue);
         } else {
             $attributeValue = $this->_convertValue($customAttributeValue, $type);
         }
         //Populate the attribute value data object once the value for custom attribute is derived based on type
         $result[] = $this->attributeValueBuilder->setAttributeCode($customAttributeCode)->setValue($attributeValue)->create();
     }
     return $result;
 }
Esempio n. 14
0
 /**
  * @magentoDataFixture Magento/Customer/_files/customer_group.php
  */
 public function testSaveActionExistingGroup()
 {
     $groupId = $this->findGroupIdWithCode(self::CUSTOMER_GROUP_CODE);
     $this->getRequest()->setParam('tax_class', self::TAX_CLASS_ID);
     $this->getRequest()->setParam('id', $groupId);
     $this->getRequest()->setParam('code', self::CUSTOMER_GROUP_CODE);
     $this->dispatch('backend/customer/group/save');
     $this->assertSessionMessages($this->isEmpty(), MessageInterface::TYPE_ERROR);
     $this->assertSessionMessages($this->logicalNot($this->isEmpty()), MessageInterface::TYPE_SUCCESS);
     $this->assertSessionMessages($this->equalTo(['The customer group has been saved.']), MessageInterface::TYPE_SUCCESS);
     /** @var \Magento\Customer\Service\V1\CustomerGroupServiceInterface $groupService */
     $groupService = Bootstrap::getObjectManager()->get('Magento\\Customer\\Service\\V1\\CustomerGroupServiceInterface');
     $customerGroupData = \Magento\Framework\Service\SimpleDataObjectConverter::toFlatArray($groupService->getGroup($groupId));
     ksort($customerGroupData);
     $this->assertEquals(['code' => self::CUSTOMER_GROUP_CODE, 'id' => $groupId, 'tax_class_id' => self::TAX_CLASS_ID], $customerGroupData);
 }
 /**
  * @magentoDbIsolation enabled
  */
 public function testCreateCustomerInServiceVsInModel()
 {
     $email = '*****@*****.**';
     $email2 = '*****@*****.**';
     $firstname = 'Tester';
     $lastname = 'McTest';
     $groupId = 1;
     $password = '******';
     /** @var \Magento\Customer\Model\Customer $customerModel */
     $customerModel = $this->_objectManager->create('Magento\\Customer\\Model\\CustomerFactory')->create();
     $customerModel->setEmail($email)->setFirstname($firstname)->setLastname($lastname)->setGroupId($groupId)->setPassword($password);
     $customerModel->save();
     /** @var \Magento\Customer\Model\Customer $customerModel */
     $savedModel = $this->_objectManager->create('Magento\\Customer\\Model\\CustomerFactory')->create()->load($customerModel->getId());
     $dataInModel = $savedModel->getData();
     $this->_customerBuilder->setEmail($email2)->setFirstname($firstname)->setLastname($lastname)->setGroupId($groupId);
     $newCustomerEntity = $this->_customerBuilder->create();
     $customerDetails = $this->_customerDetailsBuilder->setCustomer($newCustomerEntity)->create();
     $customerData = $this->_customerAccountService->createCustomer($customerDetails, $password);
     $this->assertNotNull($customerData->getId());
     $savedCustomer = $this->_customerAccountService->getCustomer($customerData->getId());
     $dataInService = \Magento\Framework\Service\SimpleDataObjectConverter::toFlatArray($savedCustomer);
     $expectedDifferences = ['created_at', 'updated_at', 'email', 'is_active', 'entity_id', 'entity_type_id', 'password_hash', 'attribute_set_id', 'disable_auto_group_change', 'confirmation', 'reward_update_notification', 'reward_warning_notification'];
     foreach ($dataInModel as $key => $value) {
         if (!in_array($key, $expectedDifferences)) {
             if (is_null($value)) {
                 $this->assertArrayNotHasKey($key, $dataInService);
             } else {
                 $this->assertEquals($value, $dataInService[$key], 'Failed asserting value for ' . $key);
             }
         }
     }
     $this->assertEquals($email2, $dataInService['email']);
     $this->assertArrayNotHasKey('is_active', $dataInService);
     $this->assertArrayNotHasKey('updated_at', $dataInService);
     $this->assertArrayNotHasKey('password_hash', $dataInService);
 }
 /**
  * Initializes Data Object with the data from array
  *
  * @param array $data
  * @return $this
  */
 protected function _setDataValues(array $data)
 {
     $dataObjectMethods = get_class_methods($this->_getDataObjectType());
     foreach ($data as $key => $value) {
         /* First, verify is there any getter for the key on the Service Data Object */
         $possibleMethods = array('get' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key), 'is' . \Magento\Framework\Service\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key));
         if (array_intersect($possibleMethods, $dataObjectMethods)) {
             $this->_data[$key] = $value;
         }
     }
     return $this;
 }
Esempio n. 17
0
 /**
  * Convert Service Data Object to array
  *
  * @param UrlRewrite $object
  * @return array
  */
 public function convertObjectToArray(UrlRewrite $object)
 {
     return SimpleDataObjectConverter::toFlatArray($object);
 }