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( $this->options = array(
'debug' => false, 'debug' => false,
'cache_type' => null, 'cache_type' => null,
'namespace' => null,
); );
// check option names and live merge, if errors are encountered Exception will be thrown // check option names and live merge, if errors are encountered Exception will be thrown
@ -114,10 +115,26 @@ class SoapClient
return $this->getNativeSoapClient()->__soapCall( return $this->getNativeSoapClient()->__soapCall(
$soapRequest->getFunction(), $soapRequest->getFunction(),
$soapRequest->getArguments(), $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 * @return \SoapClient
*/ */

View File

@ -20,12 +20,14 @@ class SoapRequest
protected $function; protected $function;
protected $arguments; protected $arguments;
protected $options; 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->function = $function;
$this->arguments = $arguments; $this->arguments = $arguments;
$this->options = $options; $this->options = $options;
$this->setHeaders($headers);
} }
/** /**
@ -143,6 +145,42 @@ class SoapRequest
return $this; 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 string The name of option
* @param mixed The value of option * @param mixed The value of option

View File

@ -23,6 +23,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase
$options = array( $options = array(
'cache_type' => Cache::TYPE_DISK_MEMORY, 'cache_type' => Cache::TYPE_DISK_MEMORY,
'debug' => true, 'debug' => true,
'namespace' => 'foo',
); );
$soapClient->setOptions($options); $soapClient->setOptions($options);
@ -61,6 +62,25 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase
$soapClient->getOption('bad_option'); $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() public function testGetSoapOptions()
{ {
Cache::setType(Cache::TYPE_MEMORY); Cache::setType(Cache::TYPE_MEMORY);

View File

@ -40,7 +40,8 @@ class SoapRequestTest extends \PHPUnit_Framework_TestCase
{ {
$soapRequest = new SoapRequest(); $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'); $soapRequest->addArgument('foo', 'bar');
@ -63,13 +64,30 @@ class SoapRequestTest extends \PHPUnit_Framework_TestCase
{ {
$soapRequest = new SoapRequest(); $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'); $soapRequest->addOption('soapaction', 'foo');
$this->assertEquals('foo', $soapRequest->getOption('soapaction')); $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() public function testConstruct()
{ {
$soapRequest = new SoapRequest(); $soapRequest = new SoapRequest();
@ -77,13 +95,19 @@ class SoapRequestTest extends \PHPUnit_Framework_TestCase
$this->assertNull($soapRequest->getFunction()); $this->assertNull($soapRequest->getFunction());
$this->assertEquals(array(), $soapRequest->getArguments()); $this->assertEquals(array(), $soapRequest->getArguments());
$this->assertEquals(array(), $soapRequest->getOptions()); $this->assertEquals(array(), $soapRequest->getOptions());
$this->assertEquals(array(), $soapRequest->getHeaders());
$arguments = array('bar' => 'foobar'); $arguments = array('bar' => 'foobar');
$options = array('soapaction' => '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('foo', $soapRequest->getFunction());
$this->assertEquals($arguments, $soapRequest->getArguments()); $this->assertEquals($arguments, $soapRequest->getArguments());
$this->assertEquals($options, $soapRequest->getOptions()); $this->assertEquals($options, $soapRequest->getOptions());
$this->assertSame($headers, $soapRequest->getHeaders());
} }
} }