|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Behat\SoapExtension\Utils; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait SoapManager. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Behat\SoapExtension\Utils |
|
11
|
|
|
*/ |
|
12
|
|
|
trait SoapManager |
|
13
|
|
|
{ |
|
14
|
|
|
use ArrayManager; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* URL of WSDL service to consume. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string|null $wsdl |
|
20
|
|
|
*/ |
|
21
|
|
|
private $wsdl; |
|
22
|
|
|
/** |
|
23
|
|
|
* Set of options for SOAP request. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $options = []; |
|
28
|
|
|
/** |
|
29
|
|
|
* Response of SOAP method. |
|
30
|
|
|
* |
|
31
|
|
|
* @var mixed |
|
32
|
|
|
*/ |
|
33
|
|
|
private $response = []; |
|
34
|
|
|
/** |
|
35
|
|
|
* Last SOAP response. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $rawResponse = ''; |
|
40
|
|
|
/** |
|
41
|
|
|
* The URIs of the namespaces. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string[] |
|
44
|
|
|
*/ |
|
45
|
|
|
private $namespaces = []; |
|
46
|
|
|
/** |
|
47
|
|
|
* Latest exception thrown out during SOAP call. |
|
48
|
|
|
* |
|
49
|
|
|
* @var null|\SoapFault |
|
50
|
|
|
*/ |
|
51
|
|
|
private $exception; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set of headers for SOAP request. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private $headers = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Make SOAP call to a function with params. |
|
62
|
|
|
* |
|
63
|
|
|
* @link http://php.net/manual/en/soapclient.getlastrequest.php#example-5896 |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $function |
|
66
|
|
|
* SOAP function name to execute. Use MethodNameIsIgnored if function name is in the XML body. |
|
67
|
|
|
* @param array $arguments |
|
68
|
|
|
* Arguments array to pass to soap call function. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function sendRequest($function, array $arguments) |
|
71
|
|
|
{ |
|
72
|
|
|
// These values can be easily overridden inside of configuration file. |
|
73
|
|
|
$this->options += [ |
|
74
|
|
|
// Important for raw response steps. |
|
75
|
|
|
'trace' => 1, |
|
76
|
|
|
'exceptions' => true, |
|
77
|
|
|
'cache_wsdl' => WSDL_CACHE_NONE, |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$client = new \SoapClient($this->wsdl, $this->options); |
|
82
|
|
|
|
|
83
|
|
|
if ($this->headers) { |
|
84
|
|
|
$client->__setSoapHeaders($this->headers); |
|
85
|
|
|
} |
|
86
|
|
|
$this->response = $client->__soapCall($function, $arguments); |
|
87
|
|
|
$this->rawResponse = $client->__getLastResponse(); |
|
88
|
|
|
} catch (\SoapFault $e) { |
|
89
|
|
|
$this->exception = $e; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Extracts first value matching provided XPATH expression. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $query |
|
97
|
|
|
* XPATH expression used to extract value from $this->rawResponse |
|
98
|
|
|
* |
|
99
|
|
|
* @return \DOMNode|bool |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function extractResponseValueMatchingXPATH($query) |
|
102
|
|
|
{ |
|
103
|
|
|
// @todo: Allow users to ignore namespaces via config or steps. |
|
104
|
|
|
// @example: $this->rawResponse = str_replace('xmlns=', 'ns=', $this->rawResponse); |
|
105
|
|
|
$dom = new \DOMDocument(); |
|
106
|
|
|
$dom->loadXML($this->rawResponse); |
|
107
|
|
|
$xpath = new \DOMXpath($dom); |
|
108
|
|
|
|
|
109
|
|
|
foreach ($this->namespaces as $prefix => $uri) { |
|
110
|
|
|
$xpath->registerNamespace($prefix, $uri); |
|
111
|
4 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
$nodeList = $xpath->query($query); |
|
114
|
4 |
|
|
|
115
|
|
|
return $nodeList->length > 0 ? $nodeList->item(0)->nodeValue : false; |
|
116
|
4 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
4 |
|
* Helper to extract a property value from the response. |
|
120
|
4 |
|
* |
|
121
|
|
|
* @param string $property |
|
122
|
4 |
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function extractResponseProperty($property) |
|
126
|
|
|
{ |
|
127
|
|
|
return static::arrayValue(static::objectToArray($this->response), explode('][', $property)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return null|\SoapFault |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getException() |
|
134
|
|
|
{ |
|
135
|
|
|
// When this method was called, this means thrown exception was read and won't be available anymore. |
|
136
|
|
|
$exception = $this->exception; |
|
137
|
|
|
// Reset the exception. |
|
138
|
|
|
$this->exception = null; |
|
139
|
|
|
|
|
140
|
|
|
return $exception; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $wsdl |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function setWSDL($wsdl) |
|
147
|
|
|
{ |
|
148
|
|
|
// Allow "null" and valid URLs. |
|
149
|
|
|
$isWsdlValid = null === $wsdl || filter_var($wsdl, FILTER_VALIDATE_URL); |
|
150
|
|
|
// Set the URL if it is validated. |
|
151
|
|
|
$this->wsdl = $isWsdlValid ? $wsdl : null; |
|
152
|
|
|
|
|
153
|
|
|
// Throw deferred exception when WSDL was reset due to it invalidation. |
|
154
|
|
|
if (!$isWsdlValid) { |
|
155
|
|
|
throw new \InvalidArgumentException(sprintf('You must pass a correct WSDL or null to %s.', __METHOD__)); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param array $options |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function setOptions(array $options = null) |
|
163
|
|
|
{ |
|
164
|
|
|
if (null === $options) { |
|
165
|
|
|
$this->options = []; |
|
166
|
|
|
} else { |
|
167
|
|
|
foreach ($options as $option => $value) { |
|
168
|
|
|
$this->setOption($option, $value); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $headers |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function setHeaders(array $headers = null) |
|
176
|
|
|
{ |
|
177
|
|
|
if (null === $headers) { |
|
178
|
|
|
$this->headers = []; |
|
179
|
|
|
} else { |
|
180
|
|
|
$this->headers = $headers; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $option |
|
186
|
|
|
* @param mixed $value |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function setOption($option, $value) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->options[$option] = is_string($value) && defined($value) ? constant($value) : $value; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param array $namespaces |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function setNamespaces(array $namespaces = null) |
|
197
|
|
|
{ |
|
198
|
|
|
if (null === $namespaces) { |
|
199
|
|
|
$this->namespaces = []; |
|
200
|
|
|
} else { |
|
201
|
|
|
foreach ($namespaces as $prefix => $uri) { |
|
202
|
|
|
$this->setNamespace($prefix, $uri); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param string $prefix |
|
209
|
|
|
* @param string $uri |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function setNamespace($prefix, $uri) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->namespaces[$prefix] = $uri; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return mixed |
|
218
|
|
|
*/ |
|
219
|
|
|
public function getResponse() |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->response; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getRawResponse() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->rawResponse; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|