forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexTypeBenchTrait.php
More file actions
254 lines (230 loc) · 10.2 KB
/
ComplexTypeBenchTrait.php
File metadata and controls
254 lines (230 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
declare(strict_types=1);
namespace Soap\Encoding\Benchmarks;
use Soap\Encoding\Driver;
use Soap\Encoding\EncoderRegistry;
use Soap\Engine\HttpBinding\SoapResponse;
use Soap\Wsdl\Loader\CallbackLoader;
use Soap\WsdlReader\Metadata\Wsdl1MetadataProvider;
use Soap\WsdlReader\Wsdl1Reader;
/**
* Shared setup for encode/decode benchmarks.
* Complex type: itemType (9 properties including nested addressType).
*/
trait ComplexTypeBenchTrait
{
protected Driver $literalDriver;
protected Driver $encodedDriver;
protected object $singleItem;
/** @var list<object> */
protected array $items;
protected SoapResponse $literalResponse;
protected SoapResponse $encodedResponse;
protected BenchSoapClient $extSoapLiteral;
protected BenchSoapClient $extSoapEncoded;
protected string $minimalResponse;
protected string $fullResponse;
public function setUp(): void
{
$this->singleItem = (object) [
'id' => 42,
'name' => 'Widget Pro',
'description' => 'A high-quality widget for professional use',
'price' => 29.99,
'quantity' => 100,
'active' => true,
'sku' => 'WDG-PRO-001',
'category' => 'Electronics',
'address' => (object) [
'street' => '123 Main St',
'city' => 'Springfield',
'zip' => '62701',
],
];
$this->items = [];
for ($i = 0; $i < 500; $i++) {
$this->items[] = (object) [
'id' => $i,
'name' => 'Widget ' . $i,
'description' => 'Description for widget ' . $i,
'price' => 9.99 + $i,
'quantity' => $i * 10,
'active' => $i % 2 === 0,
'sku' => 'WDG-' . str_pad((string) $i, 5, '0', STR_PAD_LEFT),
'category' => 'Category ' . ($i % 10),
'address' => (object) [
'street' => $i . ' Elm St',
'city' => 'City ' . ($i % 50),
'zip' => str_pad((string) ($i % 99999), 5, '0', STR_PAD_LEFT),
],
];
}
// php-soap/encoding
$this->literalDriver = $this->buildPhpSoapDriver('literal');
$this->encodedDriver = $this->buildPhpSoapDriver('encoded');
$this->literalResponse = new SoapResponse(
$this->literalDriver->encode('test', [$this->singleItem])->getRequest()
);
$this->encodedResponse = new SoapResponse(
$this->encodedDriver->encode('test', [$this->singleItem])->getRequest()
);
// ext-soap
$this->extSoapLiteral = $this->buildExtSoapClient('literal');
$this->extSoapEncoded = $this->buildExtSoapClient('encoded');
$this->minimalResponse = '<?xml version="1.0" encoding="UTF-8"?>'
. '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">'
. '<SOAP-ENV:Body><ns1:testResponse xmlns:ns1="http://test-uri/"/>'
. '</SOAP-ENV:Body></SOAP-ENV:Envelope>';
$this->fullResponse = '<?xml version="1.0" encoding="UTF-8"?>'
. '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">'
. '<SOAP-ENV:Body><ns1:testResponse xmlns:ns1="http://test-uri/">'
. '<testParam><id>42</id><name>Widget Pro</name>'
. '<description>A high-quality widget for professional use</description>'
. '<price>29.99</price><quantity>100</quantity><active>true</active>'
. '<sku>WDG-PRO-001</sku><category>Electronics</category>'
. '<address><street>123 Main St</street><city>Springfield</city><zip>62701</zip></address>'
. '</testParam></ns1:testResponse>'
. '</SOAP-ENV:Body></SOAP-ENV:Envelope>';
// Warmup ext-soap
$this->extSoapLiteral->mockResponse = $this->minimalResponse;
$this->extSoapLiteral->__soapCall('test', ['testParam' => $this->singleItem]);
$this->extSoapEncoded->mockResponse = $this->minimalResponse;
$this->extSoapEncoded->__soapCall('test', ['testParam' => $this->singleItem]);
}
private function buildPhpSoapDriver(string $use): Driver
{
$encodingStyle = $use === 'encoded'
? ' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'
: '';
$wsdl = <<<WSDL
<definitions name="BenchTest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://test-uri/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://test-uri/">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test-uri/">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
<complexType name="addressType">
<sequence>
<element name="street" type="xsd:string"/>
<element name="city" type="xsd:string"/>
<element name="zip" type="xsd:string"/>
</sequence>
</complexType>
<complexType name="itemType">
<sequence>
<element name="id" type="xsd:int"/>
<element name="name" type="xsd:string"/>
<element name="description" type="xsd:string"/>
<element name="price" type="xsd:float"/>
<element name="quantity" type="xsd:int"/>
<element name="active" type="xsd:boolean"/>
<element name="sku" type="xsd:string"/>
<element name="category" type="xsd:string"/>
<element name="address" type="tns:addressType"/>
</sequence>
</complexType>
</schema>
</types>
<message name="testMessage">
<part name="testParam" type="tns:itemType"/>
</message>
<portType name="testPortType">
<operation name="test">
<input message="testMessage"/>
<output message="testMessage"/>
</operation>
</portType>
<binding name="testBinding" type="testPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="test">
<soap:operation soapAction="#test" style="rpc"/>
<input><soap:body use="{$use}" namespace="http://test-uri/"{$encodingStyle}/></input>
<output><soap:body use="{$use}" namespace="http://test-uri/"{$encodingStyle}/></output>
</operation>
</binding>
<service name="testService">
<port name="testPort" binding="tns:testBinding">
<soap:address location="test://"/>
</port>
</service>
</definitions>
WSDL;
$wsdlObject = (new Wsdl1Reader(
new CallbackLoader(static fn (): string => $wsdl)
))('file.wsdl');
$registry = EncoderRegistry::default();
$metadata = (new Wsdl1MetadataProvider($wsdlObject))->getMetadata();
return Driver::createFromMetadata($metadata, $wsdlObject->namespaces, $registry);
}
private function buildExtSoapClient(string $use): BenchSoapClient
{
$encodingStyle = $use === 'encoded'
? ' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'
: '';
$wsdl = <<<WSDL
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="BenchTest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://test-uri/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://test-uri/">
<types>
<xsd:schema targetNamespace="http://test-uri/">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:complexType name="addressType">
<xsd:sequence>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="zip" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="itemType">
<xsd:sequence>
<xsd:element name="id" type="xsd:int"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="quantity" type="xsd:int"/>
<xsd:element name="active" type="xsd:boolean"/>
<xsd:element name="sku" type="xsd:string"/>
<xsd:element name="category" type="xsd:string"/>
<xsd:element name="address" type="tns:addressType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
<message name="testMessage"><part name="testParam" type="tns:itemType"/></message>
<portType name="testPortType">
<operation name="test"><input message="tns:testMessage"/><output message="tns:testMessage"/></operation>
</portType>
<binding name="testBinding" type="tns:testPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="test">
<soap:operation soapAction="#test" style="rpc"/>
<input><soap:body use="{$use}" namespace="http://test-uri/"{$encodingStyle}/></input>
<output><soap:body use="{$use}" namespace="http://test-uri/"{$encodingStyle}/></output>
</operation>
</binding>
<service name="testService">
<port name="testPort" binding="tns:testBinding"><soap:address location="test://"/></port>
</service>
</definitions>
WSDL;
$wsdlFile = tempnam(sys_get_temp_dir(), 'wsdl_') . '.wsdl';
file_put_contents($wsdlFile, $wsdl);
$client = new BenchSoapClient($wsdlFile, [
'trace' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
]);
unlink($wsdlFile);
return $client;
}
}