Skip to content

Commit dac2d40

Browse files
committed
Merge branch 'release/1.0.3'
2 parents d160b5e + 29bb047 commit dac2d40

File tree

4 files changed

+127
-37
lines changed

4 files changed

+127
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 1.0.3
4+
- Add utility methods **getStreamContext()** and **getStreamContextOptions()** to AbstractSoapClientbase class
5+
36
## 1.0.2
47
- Remove AbstractStructArrayBase::getAttributeName() method due to fatal error on PHP <= 5.3
58

@@ -24,4 +27,4 @@
2427
- use top level namespace for SoapHeader class
2528

2629
## 0.0.1
27-
- Initial version
30+
- Initial version

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ public function setSoapHeaderCSPCHD(\Api\StructType\ApiCSPCHD $cSPCHD, $nameSpac
266266
```
267267
- **setHttpHeader($headerName, $headerValue)**: an easy way to define your proper HTTP headers that must be sent
268268
- **setLocation($location)**: Sets the location of the Web service to use
269+
- **getStreamContext()**: Returns the created stream context used by the SoapClient class
270+
- **getStreamContextOptions()**: Returns the created stream context's options used by the SoapClient class
269271

270272
## Need improvements for these classes?
271273
Feel free to make some pull requests. We'll study them and let you know when it can be integrated.

src/AbstractSoapClientBase.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ public function setHttpHeader($headerName, $headerValue)
321321
{
322322
$state = false;
323323
if (self::getSoapClient() && !empty($headerName)) {
324-
$streamContext = (isset(self::getSoapClient()->_stream_context) && is_resource(self::getSoapClient()->_stream_context)) ? self::getSoapClient()->_stream_context : null;
325-
if (!is_resource($streamContext)) {
324+
$streamContext = $this->getStreamContext();
325+
if ($streamContext === null) {
326326
$options = array();
327327
$options['http'] = array();
328328
$options['http']['header'] = '';
@@ -357,7 +357,7 @@ public function setHttpHeader($headerName, $headerValue)
357357
/**
358358
* Create context if it does not exist
359359
*/
360-
if (!is_resource($streamContext)) {
360+
if ($streamContext === null) {
361361
$state = (self::getSoapClient()->_stream_context = stream_context_create($options)) ? true : false;
362362
} else {
363363
/**
@@ -369,6 +369,27 @@ public function setHttpHeader($headerName, $headerValue)
369369
}
370370
return $state;
371371
}
372+
/**
373+
* Returns current \SoapClient::_stream_context resource or null
374+
* @return resource|null
375+
*/
376+
public function getStreamContext()
377+
{
378+
return (self::getSoapClient() && isset(self::getSoapClient()->_stream_context) && is_resource(self::getSoapClient()->_stream_context)) ? self::getSoapClient()->_stream_context : null;
379+
}
380+
/**
381+
* Returns current \SoapClient::_stream_context resource options or empty array
382+
* @return array
383+
*/
384+
public function getStreamContextOptions()
385+
{
386+
$options = array();
387+
$context = $this->getStreamContext();
388+
if ($context !== null) {
389+
$options = stream_context_get_options($context);
390+
}
391+
return $options;
392+
}
372393
/**
373394
* Method returning last errors occured during the calls
374395
* @return array

tests/SoapClientTest.php

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
use WsdlToPhp\PackageBase\Utils;
7-
87
use WsdlToPhp\PackageBase\Tests\SoapClient;
98

109
class SoapClientTest extends TestCase
@@ -59,8 +58,8 @@ public function testSetLocation()
5958
public function testGetLastRequestAsString()
6059
{
6160
$soapClient = new SoapClient(array(
62-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
63-
SoapClient::WSDL_CLASSMAP => self::classMap(),
61+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
62+
SoapClient::WSDL_CLASSMAP => self::classMap(),
6463
));
6564

6665
$this->assertSame(Utils::getFormatedXml(file_get_contents(__DIR__ . '/resources/oneline.xml')), $soapClient->getLastRequest(false));
@@ -71,8 +70,8 @@ public function testGetLastRequestAsString()
7170
public function testGetLastRequestAsDomDocument()
7271
{
7372
$soapClient = new SoapClient(array(
74-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
75-
SoapClient::WSDL_CLASSMAP => self::classMap(),
73+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
74+
SoapClient::WSDL_CLASSMAP => self::classMap(),
7675
));
7776

7877
$this->assertInstanceOf('\\DOMDocument', $soapClient->getLastRequest(true));
@@ -83,8 +82,8 @@ public function testGetLastRequestAsDomDocument()
8382
public function testGetLastResponseAsString()
8483
{
8584
$soapClient = new SoapClient(array(
86-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
87-
SoapClient::WSDL_CLASSMAP => self::classMap(),
85+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
86+
SoapClient::WSDL_CLASSMAP => self::classMap(),
8887
));
8988

9089
$this->assertSame(Utils::getFormatedXml(file_get_contents(__DIR__ . '/resources/oneline.xml')), $soapClient->getLastResponse(false));
@@ -95,8 +94,8 @@ public function testGetLastResponseAsString()
9594
public function testGetLastResponseAsDomDocument()
9695
{
9796
$soapClient = new SoapClient(array(
98-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
99-
SoapClient::WSDL_CLASSMAP => self::classMap(),
97+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
98+
SoapClient::WSDL_CLASSMAP => self::classMap(),
10099
));
101100

102101
$this->assertInstanceOf('\\DOMDocument', $soapClient->getLastResponse(true));
@@ -107,8 +106,8 @@ public function testGetLastResponseAsDomDocument()
107106
public function testGetLastRequestHeadersAsString()
108107
{
109108
$soapClient = new SoapClient(array(
110-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
111-
SoapClient::WSDL_CLASSMAP => self::classMap(),
109+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
110+
SoapClient::WSDL_CLASSMAP => self::classMap(),
112111
));
113112

114113
$this->assertSame($soapClient->getSoapClient()->__getLastRequestHeaders(), $soapClient->getLastRequestHeaders(false));
@@ -119,8 +118,8 @@ public function testGetLastRequestHeadersAsString()
119118
public function testGetLastRequestHeadersAsArray()
120119
{
121120
$soapClient = new SoapClient(array(
122-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
123-
SoapClient::WSDL_CLASSMAP => self::classMap(),
121+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
122+
SoapClient::WSDL_CLASSMAP => self::classMap(),
124123
));
125124

126125
$this->assertSame($soapClient->getSoapClient()->getLastRequestHeadersAsArray(), $soapClient->getLastRequestHeaders(true));
@@ -131,8 +130,8 @@ public function testGetLastRequestHeadersAsArray()
131130
public function testGetLastResponseHeadersAsString()
132131
{
133132
$soapClient = new SoapClient(array(
134-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
135-
SoapClient::WSDL_CLASSMAP => self::classMap(),
133+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
134+
SoapClient::WSDL_CLASSMAP => self::classMap(),
136135
));
137136

138137
$this->assertSame($soapClient->getSoapClient()->__getLastResponseHeaders(), $soapClient->getLastResponseHeaders(false));
@@ -143,8 +142,8 @@ public function testGetLastResponseHeadersAsString()
143142
public function testGetLastResponseHeadersAsArray()
144143
{
145144
$soapClient = new SoapClient(array(
146-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
147-
SoapClient::WSDL_CLASSMAP => self::classMap(),
145+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
146+
SoapClient::WSDL_CLASSMAP => self::classMap(),
148147
));
149148

150149
$this->assertSame($soapClient->getSoapClient()->getLastResponseHeadersAsArray(), $soapClient->getLastResponseHeaders(true));
@@ -155,8 +154,8 @@ public function testGetLastResponseHeadersAsArray()
155154
public function testSetGetLastErrorForMethod()
156155
{
157156
$soapClient = new SoapClient(array(
158-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
159-
SoapClient::WSDL_CLASSMAP => self::classMap(),
157+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
158+
SoapClient::WSDL_CLASSMAP => self::classMap(),
160159
));
161160

162161
// this call should fail as no parameter is defined in the request
@@ -170,8 +169,8 @@ public function testSetGetLastErrorForMethod()
170169
public function testSetGetLastError()
171170
{
172171
$soapClient = new SoapClient(array(
173-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
174-
SoapClient::WSDL_CLASSMAP => self::classMap(),
172+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
173+
SoapClient::WSDL_CLASSMAP => self::classMap(),
175174
));
176175

177176
// this call should fail as no parameter is defined in the request
@@ -185,8 +184,8 @@ public function testSetGetLastError()
185184
public function testSetGetResult()
186185
{
187186
$soapClient = new SoapClient(array(
188-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
189-
SoapClient::WSDL_CLASSMAP => self::classMap(),
187+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
188+
SoapClient::WSDL_CLASSMAP => self::classMap(),
190189
));
191190

192191
// this call should fail as no parameter is defined in the request
@@ -200,8 +199,8 @@ public function testSetGetResult()
200199
public function testSetHeaders()
201200
{
202201
$soapClient = new SoapClient(array(
203-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
204-
SoapClient::WSDL_CLASSMAP => self::classMap(),
202+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
203+
SoapClient::WSDL_CLASSMAP => self::classMap(),
205204
));
206205

207206
$this->assertTrue($soapClient->setHttpHeader('X-Header-Name', 'X-Header-Value'));
@@ -220,8 +219,8 @@ public function testSetHeaders()
220219
public function testSetHeadersOnExistingHeaders()
221220
{
222221
$soapClient = new SoapClient(array(
223-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
224-
SoapClient::WSDL_CLASSMAP => self::classMap(),
222+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
223+
SoapClient::WSDL_CLASSMAP => self::classMap(),
225224
));
226225

227226
$this->assertTrue($soapClient->setHttpHeader('X-Header-Name', 'X-Header-Value'));
@@ -249,9 +248,9 @@ public function testSetHeadersOnExistingHttpsHeaders()
249248
),
250249
));
251250
$soapClient = new SoapClient(array(
252-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
253-
SoapClient::WSDL_CLASSMAP => self::classMap(),
254-
SoapClient::WSDL_STREAM_CONTEXT => $streamContext,
251+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
252+
SoapClient::WSDL_CLASSMAP => self::classMap(),
253+
SoapClient::WSDL_STREAM_CONTEXT => $streamContext,
255254
));
256255

257256
$this->assertTrue($soapClient->setHttpHeader('X-Header-Name', 'X-Header-Value'));
@@ -284,9 +283,9 @@ public function testSetHeadersOnExistingHttpHeaders()
284283
),
285284
));
286285
$soapClient = new SoapClient(array(
287-
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
288-
SoapClient::WSDL_CLASSMAP => self::classMap(),
289-
SoapClient::WSDL_STREAM_CONTEXT => $streamContext,
286+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
287+
SoapClient::WSDL_CLASSMAP => self::classMap(),
288+
SoapClient::WSDL_STREAM_CONTEXT => $streamContext,
290289
));
291290

292291
$this->assertTrue($soapClient->setHttpHeader('X-Header-Name', 'X-Header-Value'));
@@ -304,6 +303,71 @@ public function testSetHeadersOnExistingHttpHeaders()
304303
),
305304
), stream_context_get_options(SoapClient::getSoapClient()->_stream_context));
306305
}
306+
/**
307+
*
308+
*/
309+
public function testGetStreamContext()
310+
{
311+
$soapClient = new SoapClient(array(
312+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
313+
SoapClient::WSDL_CLASSMAP => self::classMap(),
314+
));
315+
316+
$this->assertTrue($soapClient->setHttpHeader('X-Header-Name', 'X-Header-Value'));
317+
318+
$this->assertTrue(is_resource($soapClient->getStreamContext()));
319+
320+
$this->assertSame(array(
321+
'http' => array(
322+
'header' => 'X-Header-Name: X-Header-Value',
323+
),
324+
), stream_context_get_options($soapClient->getStreamContext()));
325+
}
326+
/**
327+
*
328+
*/
329+
public function testGetStreamContextAsNill()
330+
{
331+
$soapClient = new SoapClient(array(
332+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
333+
SoapClient::WSDL_CLASSMAP => self::classMap(),
334+
));
335+
336+
$this->assertNull($soapClient->getStreamContext());
337+
}
338+
/**
339+
*
340+
*/
341+
public function testSetHeadersOnExistingHttpHeadersWithGetStreamContextOptions()
342+
{
343+
$streamContext = stream_context_create(array(
344+
'http' => array(
345+
'Auth' => array(
346+
'X-HEADER' => 'X-VALUE',
347+
),
348+
),
349+
));
350+
$soapClient = new SoapClient(array(
351+
SoapClient::WSDL_URL => __DIR__ . '/resources/bingsearch.wsdl',
352+
SoapClient::WSDL_CLASSMAP => self::classMap(),
353+
SoapClient::WSDL_STREAM_CONTEXT => $streamContext,
354+
));
355+
356+
$this->assertTrue($soapClient->setHttpHeader('X-Header-Name', 'X-Header-Value'));
357+
$this->assertTrue($soapClient->setHttpHeader('X-Header-ID', 'X-Header-ID-Value'));
358+
359+
$this->assertTrue(is_resource($soapClient->getStreamContext()));
360+
361+
$this->assertSame(array(
362+
'http' => array(
363+
'Auth' => array(
364+
'X-HEADER' => 'X-VALUE',
365+
),
366+
'header' => 'X-Header-Name: X-Header-Value' . "\r\n" .
367+
'X-Header-ID: X-Header-ID-Value',
368+
),
369+
), $soapClient->getStreamContextOptions());
370+
}
307371
/**
308372
*
309373
*/

0 commit comments

Comments
 (0)