Added SoapHeader in SoapRequest

This commit is contained in:
Francis Besset 2011-09-04 23:42:03 +02:00
parent caeb484e19
commit 5790a89571
4 changed files with 104 additions and 5 deletions

View File

@ -37,6 +37,7 @@ class SoapClient
$this->options = array(
'debug' => false,
'cache_type' => null,
'namespace' => null,
);
// check option names and live merge, if errors are encountered Exception will be thrown
@ -114,10 +115,26 @@ class SoapClient
return $this->getNativeSoapClient()->__soapCall(
$soapRequest->getFunction(),
$soapRequest->getArguments(),
$soapRequest->getOptions()
$soapRequest->getOptions(),
$soapRequest->getHeaders()
);
}
/**
* @param string The SoapHeader name
* @param mixed The SoapHeader value
*
* @return \SoapHeader
*/
public function createSoapHeader($name, $value)
{
if (null === $namespace = $this->getOption('namespace')) {
throw new \RuntimeException('You cannot create SoapHeader if you do not specify a namespace.');
}
return new \SoapHeader($namespace, $name, $value);
}
/**
* @return \SoapClient
*/

View File

@ -20,12 +20,14 @@ class SoapRequest
protected $function;
protected $arguments;
protected $options;
protected $headers;
public function __construct($function = null, array $arguments = array(), array $options = array())
public function __construct($function = null, array $arguments = array(), array $options = array(), array $headers = array())
{
$this->function = $function;
$this->arguments = $arguments;
$this->options = $options;
$this->setHeaders($headers);
}
/**
@ -143,6 +145,42 @@ class SoapRequest
return $this;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @param array $headers
*
* @return SoapRequest
*/
public function setHeaders(array $headers)
{
$this->headers = array();
foreach ($headers as $header) {
$this->addHeader($header);
}
return $this;
}
/**
* @param \SoapHeader $header
*
* @return SoapRequest
*/
public function addHeader(\SoapHeader $header)
{
$this->headers[] = $header;
return $this;
}
/**
* @param string The name of option
* @param mixed The value of option

View File

@ -23,6 +23,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase
$options = array(
'cache_type' => Cache::TYPE_DISK_MEMORY,
'debug' => true,
'namespace' => 'foo',
);
$soapClient->setOptions($options);
@ -61,6 +62,25 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase
$soapClient->getOption('bad_option');
}
public function testCreateSoapHeader()
{
$soapClient = new SoapClient('foo.wsdl', array('namespace' => 'http://foobar/soap/User/1.0/'));
$soapHeader = $soapClient->createSoapHeader('foo', 'bar');
$this->assertInstanceOf('SoapHeader', $soapHeader);
$this->assertEquals('http://foobar/soap/User/1.0/', $soapHeader->namespace);
$this->assertEquals('foo', $soapHeader->name);
$this->assertEquals('bar', $soapHeader->data);
}
public function testCreateSoapHeaderThrowsAnExceptionIfNamespaceIsNull()
{
$soapClient = new SoapClient('foo.wsdl');
$this->setExpectedException('RuntimeException');
$soapHeader = $soapClient->createSoapHeader('foo', 'bar');
}
public function testGetSoapOptions()
{
Cache::setType(Cache::TYPE_MEMORY);

View File

@ -40,7 +40,8 @@ class SoapRequestTest extends \PHPUnit_Framework_TestCase
{
$soapRequest = new SoapRequest();
$this->assertEquals(false, $soapRequest->getArgument('foo', false));
$this->assertSame(null, $soapRequest->getArgument('foo'));
$this->assertFalse($soapRequest->getArgument('foo', false));
$soapRequest->addArgument('foo', 'bar');
@ -63,13 +64,30 @@ class SoapRequestTest extends \PHPUnit_Framework_TestCase
{
$soapRequest = new SoapRequest();
$this->assertEquals(false, $soapRequest->getOption('soapaction'));
$this->assertSame(null, $soapRequest->getOption('soapaction'));
$this->assertFalse($soapRequest->getOption('soapaction', false));
$soapRequest->addOption('soapaction', 'foo');
$this->assertEquals('foo', $soapRequest->getOption('soapaction'));
}
public function testSetHeaders()
{
$soapRequest = new SoapRequest();
$this->assertEquals(array(), $soapRequest->getHeaders());
$header1 = new \SoapHeader('foobar', 'foo', 'bar');
$header2 = new \SoapHeader('barfoo', 'bar', 'foo');
$soapRequest
->addHeader($header1)
->addHeader($header2)
;
$this->assertSame(array($header1, $header2), $soapRequest->getHeaders());
}
public function testConstruct()
{
$soapRequest = new SoapRequest();
@ -77,13 +95,19 @@ class SoapRequestTest extends \PHPUnit_Framework_TestCase
$this->assertNull($soapRequest->getFunction());
$this->assertEquals(array(), $soapRequest->getArguments());
$this->assertEquals(array(), $soapRequest->getOptions());
$this->assertEquals(array(), $soapRequest->getHeaders());
$arguments = array('bar' => 'foobar');
$options = array('soapaction' => 'foobar');
$soapRequest = new SoapRequest('foo', $arguments, $options);
$headers = array(
new \SoapHeader('foobar', 'foo', 'bar'),
new \SoapHeader('barfoo', 'bar', 'foo'),
);
$soapRequest = new SoapRequest('foo', $arguments, $options, $headers);
$this->assertEquals('foo', $soapRequest->getFunction());
$this->assertEquals($arguments, $soapRequest->getArguments());
$this->assertEquals($options, $soapRequest->getOptions());
$this->assertSame($headers, $soapRequest->getHeaders());
}
}