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
|
|
|
/** |
48
|
|
|
* @var \SoapFault|null |
49
|
|
|
*/ |
50
|
|
|
private $exception; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Make SOAP call to a function with params. |
54
|
|
|
* |
55
|
|
|
* @param string $function |
56
|
|
|
* SOAP function name to execute. Use MethodNameIsIgnored if function name is in the XML body. |
57
|
|
|
* @param array $arguments |
58
|
|
|
* Arguments array to pass to soap call function. |
59
|
|
|
*/ |
60
|
|
|
protected function sendRequest($function, array $arguments) |
61
|
|
|
{ |
62
|
|
|
// These values can be easily overridden inside of configuration file. |
63
|
|
|
$this->options += [ |
64
|
|
|
// Important for raw response steps. |
65
|
|
|
'trace' => 1, |
66
|
|
|
'exceptions' => true, |
67
|
|
|
'cache_wsdl' => WSDL_CACHE_NONE, |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$client = new \SoapClient($this->wsdl, $this->options); |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$this->response = $client->__soapCall($function, $arguments); |
74
|
|
|
$this->rawResponse = $client->__getLastResponse(); |
75
|
|
|
} |
76
|
|
|
catch (\SoapFault $e) { |
77
|
|
|
$this->exception = $e; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Extracts first value matching provided XPATH expression. |
83
|
|
|
* |
84
|
|
|
* @param string $query |
85
|
|
|
* XPATH expression used to extract value from $this->rawResponse |
86
|
|
|
* |
87
|
|
|
* @return \DOMNode|bool |
88
|
|
|
*/ |
89
|
|
|
protected function extractResponseValueMatchingXPATH($query) |
90
|
|
|
{ |
91
|
|
|
// @todo: Allow users to ignore namespaces via config or steps. |
92
|
|
|
// @example: $this->rawResponse = str_replace('xmlns=', 'ns=', $this->rawResponse); |
93
|
|
|
$dom = new \DOMDocument(); |
94
|
|
|
$dom->loadXML($this->rawResponse); |
95
|
|
|
$xpath = new \DOMXpath($dom); |
96
|
|
|
|
97
|
|
|
foreach ($this->namespaces as $prefix => $uri) { |
98
|
|
|
$xpath->registerNamespace($prefix, $uri); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$nodeList = $xpath->query($query); |
102
|
|
|
|
103
|
|
|
return $nodeList->length > 0 ? $nodeList->item(0)->nodeValue : false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Helper to extract a property value from the response. |
108
|
|
|
* |
109
|
|
|
* @param string $property |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
protected function extractResponseProperty($property) |
114
|
|
|
{ |
115
|
|
|
return static::arrayValue(static::objectToArray($this->response), explode('][', $property)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $wsdl |
120
|
|
|
*/ |
121
|
4 |
|
protected function setWSDL($wsdl) |
122
|
|
|
{ |
123
|
|
|
// Allow "null" and valid URLs. |
124
|
4 |
|
$isWsdlValid = null === $wsdl || filter_var($wsdl, FILTER_VALIDATE_URL); |
125
|
|
|
// Set the URL if it is validated. |
126
|
4 |
|
$this->wsdl = $isWsdlValid ? $wsdl : null; |
127
|
|
|
|
128
|
|
|
// Throw deferred exception when WSDL was reset due to it invalidation. |
129
|
4 |
|
if (!$isWsdlValid) { |
130
|
4 |
|
throw new \InvalidArgumentException(sprintf('You must pass a correct WSDL or null to %s.', __METHOD__)); |
131
|
|
|
} |
132
|
4 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $options |
136
|
|
|
*/ |
137
|
|
|
protected function setOptions(array $options = null) |
138
|
|
|
{ |
139
|
|
|
if (null === $options) { |
140
|
|
|
$this->options = []; |
141
|
|
|
} else { |
142
|
|
|
foreach ($options as $option => $value) { |
143
|
|
|
$this->setOption($option, $value); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $option |
150
|
|
|
* @param mixed $value |
151
|
|
|
*/ |
152
|
|
|
protected function setOption($option, $value) |
153
|
|
|
{ |
154
|
|
|
$this->options[$option] = defined($value) ? constant($value) : $value; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $namespaces |
159
|
|
|
*/ |
160
|
|
|
protected function setNamespaces(array $namespaces = null) |
161
|
|
|
{ |
162
|
|
|
if (null === $namespaces) { |
163
|
|
|
$this->namespaces = []; |
164
|
|
|
} else { |
165
|
|
|
foreach ($namespaces as $prefix => $uri) { |
166
|
|
|
$this->setNamespace($prefix, $uri); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $prefix |
173
|
|
|
* @param string $uri |
174
|
|
|
*/ |
175
|
|
|
protected function setNamespace($prefix, $uri) |
176
|
|
|
{ |
177
|
|
|
$this->namespaces[$prefix] = $uri; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return \SoapFault |
182
|
|
|
*/ |
183
|
|
|
public function getException() { |
184
|
|
|
return $this->exception; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param \SoapFault $exception |
189
|
|
|
*/ |
190
|
|
|
public function setException(\SoapFault $exception = null) { |
191
|
|
|
$this->exception = $exception; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|