|
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 $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
|
|
|
protected function setWSDL($wsdl) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->wsdl = $wsdl; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param array $options |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function setOptions(array $options = null) |
|
130
|
|
|
{ |
|
131
|
|
|
if (null === $options) { |
|
132
|
|
|
$this->options = []; |
|
133
|
|
|
} else { |
|
134
|
|
|
foreach ($options as $option => $value) { |
|
135
|
|
|
$this->setOption($option, $value); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $option |
|
142
|
|
|
* @param mixed $value |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function setOption($option, $value) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->options[$option] = defined($value) ? constant($value) : $value; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param array $namespaces |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function setNamespaces(array $namespaces = null) |
|
153
|
|
|
{ |
|
154
|
|
|
if (null === $namespaces) { |
|
155
|
|
|
$this->namespaces = []; |
|
156
|
|
|
} else { |
|
157
|
|
|
foreach ($namespaces as $prefix => $uri) { |
|
158
|
|
|
$this->setNamespace($prefix, $uri); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string $prefix |
|
165
|
|
|
* @param string $uri |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function setNamespace($prefix, $uri) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->namespaces[$prefix] = $uri; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return \SoapFault |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getException() { |
|
176
|
|
|
return $this->exception; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param \SoapFault $exception |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setException(\SoapFault $exception = null) { |
|
183
|
|
|
$this->exception = $exception; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.