diff --git a/src/BeSimple/SoapClient/SoapClient.php b/src/BeSimple/SoapClient/SoapClient.php index 10de074..b40ad00 100644 --- a/src/BeSimple/SoapClient/SoapClient.php +++ b/src/BeSimple/SoapClient/SoapClient.php @@ -12,190 +12,9 @@ namespace BeSimple\SoapClient; -use BeSimple\SoapCommon\Cache; -use BeSimple\SoapCommon\Classmap; -use BeSimple\SoapCommon\Converter\TypeConverterCollection; - /** * @author Francis Besset */ -class SoapClient +class SoapClient extends \SoapClient { - protected $wsdl; - protected $classmap; - protected $converters; - protected $soapClient; - - /** - * @param string $wsdl - * @param array $options - */ - public function __construct($wsdl, array $options = array(), Classmap $classmap = null, TypeConverterCollection $converters = null) - { - $this->wsdl = $wsdl; - $this->classmap = $classmap; - $this->converters = $converters; - - $this->setOptions($options); - } - - public function setOptions(array $options) - { - $this->options = array( - 'debug' => false, - 'cache_type' => null, - 'namespace' => null, - 'exceptions' => true, - 'user_agent' => 'BeSimpleSoap', - ); - - // check option names and live merge, if errors are encountered Exception will be thrown - $invalid = array(); - $isInvalid = false; - foreach ($options as $key => $value) { - if (array_key_exists($key, $this->options)) { - $this->options[$key] = $value; - } else { - $isInvalid = true; - $invalid[] = $key; - } - } - - if ($isInvalid) { - throw new \InvalidArgumentException(sprintf( - 'The "%s" class does not support the following options: "%s".', - get_class($this), - implode('\', \'', $invalid) - )); - } - } - - /** - * @param string $name The name - * @param mixed $value The value - * - * @throws \InvalidArgumentException - */ - public function setOption($name, $value) - { - if (!array_key_exists($name, $this->options)) { - throw new \InvalidArgumentException(sprintf( - 'The "%s" class does not support the "%s" option.', - get_class($this), - $name - )); - } - - $this->options[$name] = $value; - } - - public function getOptions() - { - return $this->options; - } - - /** - * @param string $key The key - * - * @return mixed The value - * - * @throws \InvalidArgumentException - */ - public function getOption($key) - { - if (!array_key_exists($key, $this->options)) { - throw new \InvalidArgumentException(sprintf( - 'The "%s" class does not support the "%s" option.', - get_class($this), - $key - )); - } - - return $this->options[$key]; - } - - /** - * @param SoapRequest $soapRequest - * - * @return mixed - */ - public function send(SoapRequest $soapRequest) - { - return $this->getNativeSoapClient()->__soapCall( - $soapRequest->getFunction(), - $soapRequest->getArguments(), - $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 - */ - public function getNativeSoapClient() - { - if (!$this->soapClient) { - $this->soapClient = new \SoapClient($this->wsdl, $this->getSoapOptions()); - } - - return $this->soapClient; - } - - /** - * @return array The \SoapClient options - */ - public function getSoapOptions() - { - if (null === $this->options['cache_type']) { - $this->options['cache_type'] = Cache::getType(); - } - - return array( - 'cache_wsdl' => $this->options['cache_type'], - 'trace' => $this->options['debug'], - 'classmap' => $this->getClassmap(), - 'exceptions' => $this->options['exceptions'], - 'typemap' => $this->getTypemap(), - 'user_agent' => $this->options['user_agent'], - ); - } - - /** - * @return array - */ - protected function getClassmap() - { - if (!$this->classmap) { - return array(); - } - - return $this->classmap->all(); - } - - /** - * @return array - */ - protected function getTypemap() - { - if (!$this->converters) { - return array(); - } - - return $this->converters->getTypemap(); - } } \ No newline at end of file diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index 5ec9678..63c9934 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -29,6 +29,19 @@ class SoapClientBuilder extends AbstractSoapBuilder ; } + /** + * @return SoapClient + */ + public function build() + { + $this->validateOptions(); + + return new SoapClient($this->optionWsdl, $this->options); + } + + /** + * @return SoapClientBuilder + */ public function withTrace($trace = true) { $this->soapOptions['trace'] = $trace; @@ -36,6 +49,9 @@ class SoapClientBuilder extends AbstractSoapBuilder return $this; } + /** + * @return SoapClientBuilder + */ public function withExceptions($exceptions = true) { $this->soapOptions['exceptions'] = $exceptions; @@ -43,10 +59,18 @@ class SoapClientBuilder extends AbstractSoapBuilder return $this; } + /** + * @return SoapClientBuilder + */ public function withUserAgent($userAgent) { $this->soapOptions['user_agent'] = $userAgent; return $this; } + + protected function validateOptions() + { + $this->validateWsdl(); + } } \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php deleted file mode 100644 index d947f74..0000000 --- a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php +++ /dev/null @@ -1,147 +0,0 @@ - - * (c) Francis Besset - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace BeSimple\Tests\SoapClient; - -use BeSimple\SoapCommon\Cache; -use BeSimple\SoapCommon\Classmap; -use BeSimple\SoapCommon\Converter\DateTimeTypeConverter; -use BeSimple\SoapCommon\Converter\DateTypeConverter; -use BeSimple\SoapCommon\Converter\TypeConverterCollection; -use BeSimple\SoapClient\SoapClient; - -class SoapClientTest extends \PHPUnit_Framework_TestCase -{ - public function testSetOptions() - { - $soapClient = new SoapClient('foo.wsdl'); - $options = array( - 'cache_type' => Cache::TYPE_DISK_MEMORY, - 'debug' => true, - 'namespace' => 'foo', - ); - $soapClient->setOptions($options); - - $this->assertEquals(array_merge($options, array('exceptions' => true, 'user_agent' => 'BeSimpleSoap')), $soapClient->getOptions()); - } - - public function testSetOptionsThrowsAnExceptionIfOptionsDoesNotExists() - { - $soapClient = new SoapClient('foo.wsdl'); - - $this->setExpectedException('InvalidArgumentException'); - $soapClient->setOptions(array('bad_option' => true)); - } - - public function testSetOption() - { - $soapClient = new SoapClient('foo.wsdl'); - $soapClient->setOption('debug', true); - - $this->assertEquals(true, $soapClient->getOption('debug')); - } - - public function testSetOptionThrowsAnExceptionIfOptionDoesNotExists() - { - $soapClient = new SoapClient('foo.wsdl'); - - $this->setExpectedException('InvalidArgumentException'); - $soapClient->setOption('bad_option', 'bar'); - } - - public function testGetOptionThrowsAnExceptionIfOptionDoesNotExists() - { - $soapClient = new SoapClient('foo.wsdl'); - - $this->setExpectedException('InvalidArgumentException'); - $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); - $soapClient = new SoapClient('foo.wsdl', array('debug' => true)); - $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'classmap' => array(), 'exceptions' => true, 'typemap' => array(), 'user_agent' => 'BeSimpleSoap'), $soapClient->getSoapOptions()); - - $soapClient = new SoapClient('foo.wsdl', array('debug' => false, 'cache_type' => Cache::TYPE_NONE)); - $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false, 'classmap' => array(), 'exceptions' => true, 'typemap' => array(), 'user_agent' => 'BeSimpleSoap'), $soapClient->getSoapOptions()); - } - - public function testGetSoapOptionsWithClassmap() - { - $classmap = new Classmap(); - - $soapClient = new SoapClient('foo.wsdl', array(), $classmap); - $soapOptions = $soapClient->getSoapOptions(); - - $this->assertSame(array(), $soapOptions['classmap']); - - $map = array( - 'foobar' => 'BeSimple\SoapClient\SoapClient', - 'barfoo' => 'BeSimple\SoapClient\Tests\SoapClientTest', - ); - $classmap->set($map); - $soapOptions = $soapClient->getSoapOptions(); - - $this->assertSame($map, $soapOptions['classmap']); - } - - public function testGetSoapOptionsWithTypemap() - { - $converters = new TypeConverterCollection(); - - $dateTimeTypeConverter = new DateTimeTypeConverter(); - $converters->add($dateTimeTypeConverter); - - $dateTypeConverter = new DateTypeConverter(); - $converters->add($dateTypeConverter); - - $soapClient = new SoapClient('foo.wsdl', array(), null, $converters); - $soapOptions = $soapClient->getSoapOptions(); - - $this->assertEquals('http://www.w3.org/2001/XMLSchema', $soapOptions['typemap'][0]['type_ns']); - $this->assertEquals('dateTime', $soapOptions['typemap'][0]['type_name']); - $this->assertInstanceOf('Closure', $soapOptions['typemap'][0]['from_xml']); - $this->assertInstanceOf('Closure', $soapOptions['typemap'][0]['to_xml']); - - $this->assertEquals('http://www.w3.org/2001/XMLSchema', $soapOptions['typemap'][1]['type_ns']); - $this->assertEquals('date', $soapOptions['typemap'][1]['type_name']); - $this->assertInstanceOf('Closure', $soapOptions['typemap'][1]['from_xml']); - $this->assertInstanceOf('Closure', $soapOptions['typemap'][1]['to_xml']); - } - - public function testGetNativeSoapClient() - { - $soapClient = new SoapClient(__DIR__.'/Fixtures/foobar.wsdl', array('debug' => true)); - - $this->assertInstanceOf('SoapClient', $soapClient->getNativeSoapClient()); - } -} \ No newline at end of file