Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Soap Class Reference
Inheritance diagram for Soap:
AdapterInterface

Public Member Functions

 call ($serviceInfo, $arguments=[], $storeCode=null, $integration=null)
 
 instantiateSoapClient ($wsdlUrl, $token=null)
 
 generateWsdlUrl ($services, $storeCode=null)
 

Data Fields

const WSDL_BASE_PATH = '/soap'
 

Protected Member Functions

 _getSoapClient ($serviceInfo, $storeCode=null)
 
 _getSoapOperation ($serviceInfo)
 
 _getSoapServiceVersion ($serviceInfo)
 
 _getSoapServiceName ($serviceInfo)
 
 toSnakeCase (array $objectData)
 

Protected Attributes

 $_soapClients = []
 
 $_soapConfig
 
 $_converter
 

Detailed Description

Test client for SOAP API testing.

Definition at line 15 of file Soap.php.

Member Function Documentation

◆ _getSoapClient()

_getSoapClient (   $serviceInfo,
  $storeCode = null 
)
protected

Get proper SOAP client instance that is initialized with WSDL corresponding to requested service interface.

Parameters
string$serviceInfoPHP service interface name, should include version if present
string | null$storeCode
Returns
\Zend\Soap\Client

Check if there is SOAP client initialized with requested WSDL available

Definition at line 72 of file Soap.php.

73  {
74  $wsdlUrl = $this->generateWsdlUrl(
75  [$this->_getSoapServiceName($serviceInfo) . $this->_getSoapServiceVersion($serviceInfo)],
77  );
79  if (!isset($this->_soapClients[$wsdlUrl])) {
80  $token = isset($serviceInfo['soap']['token']) ? $serviceInfo['soap']['token'] : null;
81  $this->_soapClients[$wsdlUrl] = $this->instantiateSoapClient($wsdlUrl, $token);
82  }
83  return $this->_soapClients[$wsdlUrl];
84  }
instantiateSoapClient($wsdlUrl, $token=null)
Definition: Soap.php:93
$storeCode
Definition: indexer.php:15
generateWsdlUrl($services, $storeCode=null)
Definition: Soap.php:120

◆ _getSoapOperation()

_getSoapOperation (   $serviceInfo)
protected

Retrieve SOAP operation name from available service info.

Parameters
array$serviceInfo
Returns
string
Exceptions

Definition at line 148 of file Soap.php.

149  {
150  if (isset($serviceInfo['soap']['operation'])) {
151  $soapOperation = $serviceInfo['soap']['operation'];
152  } elseif (isset($serviceInfo['serviceInterface']) && isset($serviceInfo['method'])) {
153  $soapOperation = $this->_soapConfig->getSoapOperation(
154  $serviceInfo['serviceInterface'],
155  $serviceInfo['method']
156  );
157  } else {
158  throw new \LogicException("SOAP operation cannot be identified.");
159  }
160  return $soapOperation;
161  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ _getSoapServiceName()

_getSoapServiceName (   $serviceInfo)
protected

Retrieve service name from available service info.

Parameters
array$serviceInfo
Returns
string
Exceptions

Definition at line 206 of file Soap.php.

207  {
208  if (isset($serviceInfo['soap']['service'])) {
209  $serviceName = $serviceInfo['soap']['service'];
210  } elseif (isset($serviceInfo['serviceInterface'])) {
211  $serviceName = $this->_soapConfig->getServiceName($serviceInfo['serviceInterface'], false);
212  } else {
213  throw new \LogicException("Service name cannot be identified.");
214  }
215  return $serviceName;
216  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ _getSoapServiceVersion()

_getSoapServiceVersion (   $serviceInfo)
protected

Retrieve service version from available service info.

Parameters
array$serviceInfo
Returns
string
Exceptions

Normalize version

Definition at line 170 of file Soap.php.

171  {
172  if (isset($serviceInfo['soap']['operation'])) {
173  /*
174  TODO: Need to rework this to remove version call for serviceInfo array with 'operation' key
175  since version will be part of the service name
176  */
177  return '';
178  } elseif (isset($serviceInfo['serviceInterface'])) {
179  preg_match(
180  \Magento\Webapi\Model\Config::SERVICE_CLASS_PATTERN,
181  $serviceInfo['serviceInterface'],
182  $matches
183  );
184  if (isset($matches[3])) {
185  $version = $matches[3];
186  } else {
187  //TODO: Need to add this temporary version until version is added back for new MSC based services
188  $version = 1;
189  //throw new \LogicException("Service interface name is invalid.");
190  }
191  } else {
192  throw new \LogicException("Service version cannot be identified.");
193  }
195  $version = 'V' . ltrim($version, 'vV');
196  return $version;
197  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ call()

call (   $serviceInfo,
  $arguments = [],
  $storeCode = null,
  $integration = null 
)

{Perform call to the specified service method.

Parameters
array$serviceInfo
array(
    'rest' => array(
        'resourcePath' => $resourcePath,                 // e.g. /products/:id
        'httpMethod' => $httpMethod,                     // e.g. GET
        'token' => '21hasbtlaqy8t3mj73kjh71cxxkqj4aq'    // optional : for token based Authentication. Will
                                                            override default Oauth based authentication provided
                                                            by test framework
    ),
    'soap' => array(
        'service' => $soapService,                       // soap service name with Version suffix e.g.
                                                            catalogProductV1, customerV2
        'operation' => $operation                        // soap operation name e.g. catalogProductCreate
        'token' => '21hasbtlaqy8t3mj73kjh71cxxkqj4aq'    // optional : for token based Authentication. Will
                                                            override default Oauth based authentication provided
                                                            by test framework
    ),
    OR
    'serviceInterface' => $phpServiceInterfaceName,      // e.g. \Magento\Catalog\Api\ProductInterface
    'method' => $serviceMethodName                       // e.g. create
    'entityId' => $entityId                              // is used in REST route placeholder (if applicable)
);
array$arguments
string | null$storeCodeif store code not provided, default store code will be used
\Magento\Integration\Model\Integration | null$integration
Returns
array|string|int|float|bool
}

Remove result wrappers

Implements AdapterInterface.

Definition at line 51 of file Soap.php.

52  {
53  $soapOperation = $this->_getSoapOperation($serviceInfo);
54  $arguments = $this->_converter->convertKeysToCamelCase($arguments);
55  $soapResponse = $this->_getSoapClient($serviceInfo, $storeCode)->$soapOperation($arguments);
56  //Convert to snake case for tests to use same assertion data for both SOAP and REST tests
57  $result = (is_array($soapResponse) || is_object($soapResponse))
58  ? $this->toSnakeCase($this->_converter->convertStdObjectToArray($soapResponse, true))
59  : $soapResponse;
61  $result = isset($result[SoapHandler::RESULT_NODE_NAME]) ? $result[SoapHandler::RESULT_NODE_NAME] : $result;
62  return $result;
63  }
$storeCode
Definition: indexer.php:15
$arguments
_getSoapClient($serviceInfo, $storeCode=null)
Definition: Soap.php:72

◆ generateWsdlUrl()

generateWsdlUrl (   $services,
  $storeCode = null 
)

Generate WSDL URL.

Parameters
array$servicese.g.
array(
    'catalogProductV1',
    'customerV2'
);
string | null$storeCode
Returns
string

Sort list of services to avoid having different WSDL URLs for the identical lists of services.

TESTS_BASE_URL is initialized in PHPUnit configuration

Definition at line 120 of file Soap.php.

121  {
123  //TODO: This may change since same resource of multiple versions may be allowed after namespace changes
124  ksort($services);
125  if ($storeCode == null) {
127  ->get(\Magento\Store\Model\StoreManagerInterface::class)
128  ->getStore()
129  ->getCode();
130  }
131 
133  $wsdlUrl = rtrim(TESTS_BASE_URL, '/') . self::WSDL_BASE_PATH . '/' . $storeCode . '?wsdl=1&services=';
134  $wsdlResourceArray = [];
135  foreach ($services as $serviceName) {
136  $wsdlResourceArray[] = $serviceName;
137  }
138  return $wsdlUrl . implode(",", $wsdlResourceArray);
139  }
$storeCode
Definition: indexer.php:15

◆ instantiateSoapClient()

instantiateSoapClient (   $wsdlUrl,
  $token = null 
)

Create SOAP client instance and initialize it with provided WSDL URL.

Parameters
string$wsdlUrl
string$tokenAuthentication token
Returns
\Zend\Soap\Client

Definition at line 93 of file Soap.php.

94  {
95  $accessCredentials = $token
96  ? $token
97  : \Magento\TestFramework\Authentication\OauthHelper::getApiAccessCredentials()['key'];
98  $opts = ['http' => ['header' => "Authorization: Bearer " . $accessCredentials]];
99  $context = stream_context_create($opts);
100  $soapClient = new \Zend\Soap\Client($wsdlUrl);
101  $soapClient->setSoapVersion(SOAP_1_2);
102  $soapClient->setStreamContext($context);
103  if (TESTS_XDEBUG_ENABLED) {
104  $soapClient->setCookie('XDEBUG_SESSION', 1);
105  }
106  return $soapClient;
107  }

◆ toSnakeCase()

toSnakeCase ( array  $objectData)
protected

Recursively transform array keys from camelCase to snake_case.

Utility method for converting SOAP responses. Webapi framework's SOAP processing outputs snake case Data Object properties(ex. item_id) as camel case(itemId) to adhere to the WSDL. This method allows tests to use the same data for asserting both SOAP and REST responses.

Parameters
array$objectDataAn array of data.
Returns
array The array with all camelCase keys converted to snake_case.

Definition at line 228 of file Soap.php.

229  {
230  $data = [];
231  foreach ($objectData as $key => $value) {
232  $key = strtolower(preg_replace("/(?<=\\w)(?=[A-Z])/", "_$1", $key));
233  if (is_array($value)) {
234  $data[$key] = $this->toSnakeCase($value);
235  } else {
236  $data[$key] = $value;
237  }
238  }
239  return $data;
240  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_converter

$_converter
protected

Definition at line 34 of file Soap.php.

◆ $_soapClients

$_soapClients = []
protected

Definition at line 24 of file Soap.php.

◆ $_soapConfig

$_soapConfig
protected

Definition at line 29 of file Soap.php.

◆ WSDL_BASE_PATH

const WSDL_BASE_PATH = '/soap'

Definition at line 17 of file Soap.php.


The documentation for this class was generated from the following file: