/** * Covers object with custom attributes * * @dataProvider customAttributesDataProvider * @param $customAttributeType * @param $inputData * @param $expectedObject */ public function testCustomAttributesProperties($customAttributeType, $inputData, $expectedObject) { $this->customAttributeTypeLocator->expects($this->any())->method('getType')->willReturn($customAttributeType); $result = $this->serviceInputProcessor->process('Magento\\Framework\\Webapi\\Test\\Unit\\ServiceInputProcessor\\TestService', 'ObjectWithCustomAttributesMethod', $inputData); $this->assertTrue($result[0] instanceof ObjectWithCustomAttributes); $this->assertEquals($expectedObject, $result[0]); }
/** * Convert SOAP operation arguments into format acceptable by service method. * * @param string $serviceClass * @param string $serviceMethod * @param array $arguments * @return array */ protected function _prepareRequestData($serviceClass, $serviceMethod, $arguments) { /** SoapServer wraps parameters into array. Thus this wrapping should be removed to get access to parameters. */ $arguments = reset($arguments); $arguments = $this->_dataObjectConverter->convertStdObjectToArray($arguments, true); return $this->serviceInputProcessor->process($serviceClass, $serviceMethod, $arguments); }
/** @inheritdoc */ public function get($ids = null) { /* prepare request parameters */ if (is_array($ids)) { $params = [self::ODOO_IDS => $ids]; } elseif (is_int($ids)) { $params = [self::ODOO_IDS => [$ids]]; } else { $params = [self::ODOO_IDS => []]; } /* perform request and extract result data */ $cover = $this->_rest->request($params, self::ROUTE); $data = $cover->getResultData(); $result = $this->_mageSrvInProc->convertValue($data, \Praxigento\Odoo\Data\Odoo\Inventory::class); return $result; }
/** * 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\Framework\Webapi\Exception(__('Operation allowed only in HTTPS')); } /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $this->_response->prepareResponse($outputData); } catch (\Exception $e) { $maskedException = $this->_errorProcessor->maskException($e); $this->_response->setException($maskedException); } return $this->_response; }
/** * Process and resolve input parameters * * @return array * @throws \Magento\Framework\Webapi\Exception */ public function resolve() { $this->requestValidator->validate(); $route = $this->getRoute(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); /* * Valid only for updates using PUT when passing id value both in URL and body */ if ($this->request->getHttpMethod() == RestRequest::HTTP_METHOD_PUT) { $inputData = $this->paramsOverrider->overrideRequestBodyIdWithPathParam($this->request->getParams(), $this->request->getBodyParams(), $serviceClassName, $serviceMethodName); $inputData = array_merge($inputData, $this->request->getParams()); } else { $inputData = $this->request->getRequestData(); } $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); return $inputParams; }
public function save($order) { /* prepare request parameters */ $underscored = $order->getData(null, true); /* perform request and extract result data */ $cover = $this->_rest->request($underscored, self::ROUTE); $data = $cover->getResultData(); if ($data) { $result = $this->_mageSrvInProc->convertValue($data, \Praxigento\Odoo\Data\Odoo\SaleOrder\Response::class); } else { $error = $cover->getError(); $result = $this->_mageSrvInProc->convertValue($error, \Praxigento\Odoo\Data\Odoo\Error::class); /** TODO : delete tmp code (cannot use getData as getter for property) */ $debug = $error['data']['debug']; $name = $error['data']['name']; $result->setDebug($debug); $result->setName($name); } return $result; }
public function testCall() { $requestedServices = ['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\\DataObject'; $methodName = 'testMethod'; $isSecure = false; $aclResources = [['Magento_TestModule::resourceA']]; $this->_apiConfigMock->expects($this->once())->method('getServiceMethodInfo')->with($operationName, $requestedServices)->will($this->returnValue([ServiceMetadata::KEY_CLASS => $className, ServiceMetadata::KEY_METHOD => $methodName, ServiceMetadata::KEY_IS_SECURE => $isSecure, ServiceMetadata::KEY_ACL_RESOURCES => $aclResources])); $this->_authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true)); $serviceMock = $this->getMockBuilder($className)->disableOriginalConstructor()->setMethods([$methodName])->getMock(); $serviceResponse = ['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->_serviceInputProcessorMock->expects($this->once())->method('process')->will($this->returnArgument(2)); /** Execute SUT. */ $this->assertEquals(['result' => $serviceResponse], $this->_handler->__call($operationName, [(object) ['field' => 1]])); }
protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) { /* load JSON data */ $fileData = file_get_contents(__DIR__ . '/data.json'); $jsonData = json_decode($fileData, true); $bundle = $this->_serviceInputProcessor->convertValue($jsonData['data'], \Praxigento\Odoo\Data\Odoo\Inventory::class); $def = $this->_manTrans->begin(); try { /* create products using replication */ /** @var ProductSaveRequest $req */ $req = $this->_manObj->create(ProductSaveRequest::class); $req->setProductBundle($bundle); $this->_callReplicate->productSave($req); /* enable categories after replication */ $this->_subCats->enableForAllStoreViews(); $this->_manTrans->commit($def); } finally { // transaction will be rolled back if commit is not done (otherwise - do nothing) $this->_manTrans->end($def); } }
/** * Test Secure Request and Secure route combinations * * @dataProvider dataProviderSecureRequestSecureRoute */ public function testSecureRouteAndRequest($isSecureRoute, $isSecureRequest) { $this->_serviceMock->expects($this->any())->method(self::SERVICE_METHOD)->will($this->returnValue([])); $this->_routeMock->expects($this->any())->method('isSecure')->will($this->returnValue($isSecureRoute)); $this->_routeMock->expects($this->once())->method('getParameters')->will($this->returnValue([])); $this->_routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1'])); $this->_requestMock->expects($this->any())->method('getRequestData')->will($this->returnValue([])); $this->_requestMock->expects($this->any())->method('isSecure')->will($this->returnValue($isSecureRequest)); $this->_authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true)); $this->serviceInputProcessorMock->expects($this->any())->method('process')->will($this->returnValue([])); $this->_restController->dispatch($this->_requestMock); $this->assertFalse($this->_responseMock->isException()); }
/** * Execute API request * * @return void * @throws AuthorizationException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Webapi\Exception */ protected function processApiRequest() { $this->validateRequest(); /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $route = $this->getCurrentRoute(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $this->_response->prepareResponse($outputData); }
/** * Cover invalid custom attribute data * * @dataProvider invalidCustomAttributesDataProvider * @expectedException \Magento\Framework\Webapi\Exception */ public function testCustomAttributesExceptions($inputData) { $this->serviceInputProcessor->process('Magento\\Framework\\Webapi\\Test\\Unit\\ServiceInputProcessor\\TestService', 'ObjectWithCustomAttributesMethod', $inputData); }