From 7be719ffc57ae7b2a0cbb18f954c4f537d52e787 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 8 Oct 2011 12:22:17 +0200 Subject: [PATCH 01/12] [SoapClient] Moved TypeConverterCollection parameter in the constructor --- src/BeSimple/SoapClient/SoapClient.php | 15 +++++++-------- .../BeSimple/Tests/SoapClient/SoapClientTest.php | 10 +++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/BeSimple/SoapClient/SoapClient.php b/src/BeSimple/SoapClient/SoapClient.php index 6832ecb..9266a0c 100644 --- a/src/BeSimple/SoapClient/SoapClient.php +++ b/src/BeSimple/SoapClient/SoapClient.php @@ -28,10 +28,11 @@ class SoapClient * @param string $wsdl * @param array $options */ - public function __construct($wsdl, TypeConverterCollection $converters = null, array $options = array()) + public function __construct($wsdl, array $options = array(), TypeConverterCollection $converters = null) { $this->wsdl = $wsdl; $this->converters = $converters; + $this->setOptions($options); } @@ -155,17 +156,15 @@ class SoapClient */ public function getSoapOptions() { - $options = array(); - if (null === $this->options['cache_type']) { $this->options['cache_type'] = Cache::getType(); } - $options['cache_wsdl'] = $this->options['cache_type']; - $options['trace'] = $this->options['debug']; - $options['typemap'] = $this->getTypemap(); - - return $options; + return array( + 'cache_wsdl' => $this->options['cache_type'], + 'trace' => $this->options['debug'], + 'typemap' => $this->getTypemap(), + ); } /** diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php index 34e9561..580bcbe 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php @@ -67,7 +67,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase public function testCreateSoapHeader() { - $soapClient = new SoapClient('foo.wsdl', null, array('namespace' => 'http://foobar/soap/User/1.0/')); + $soapClient = new SoapClient('foo.wsdl', array('namespace' => 'http://foobar/soap/User/1.0/')); $soapHeader = $soapClient->createSoapHeader('foo', 'bar'); $this->assertInstanceOf('SoapHeader', $soapHeader); @@ -87,10 +87,10 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase public function testGetSoapOptions() { Cache::setType(Cache::TYPE_MEMORY); - $soapClient = new SoapClient('foo.wsdl', null, array('debug' => true)); + $soapClient = new SoapClient('foo.wsdl', array('debug' => true)); $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'typemap' => array()), $soapClient->getSoapOptions()); - $soapClient = new SoapClient('foo.wsdl', null, array('debug' => false, 'cache_type' => Cache::TYPE_NONE)); + $soapClient = new SoapClient('foo.wsdl', array('debug' => false, 'cache_type' => Cache::TYPE_NONE)); $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false, 'typemap' => array()), $soapClient->getSoapOptions()); } @@ -104,7 +104,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase $dateTypeConverter = new DateTypeConverter(); $converters->add($dateTypeConverter); - $soapClient = new SoapClient('foo.wsdl', $converters); + $soapClient = new SoapClient('foo.wsdl', array(), $converters); $soapOptions = $soapClient->getSoapOptions(); $this->assertEquals('http://www.w3.org/2001/XMLSchema', $soapOptions['typemap'][0]['type_ns']); @@ -120,7 +120,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase public function testGetNativeSoapClient() { - $soapClient = new SoapClient(__DIR__.'/Fixtures/foobar.wsdl', null, array('debug' => true)); + $soapClient = new SoapClient(__DIR__.'/Fixtures/foobar.wsdl', array('debug' => true)); $this->assertInstanceOf('SoapClient', $soapClient->getNativeSoapClient()); } From 29a388eb7db15283dc491beae34ec01f43cef2d1 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 8 Oct 2011 15:07:49 +0200 Subject: [PATCH 02/12] [SoapClient] Added Classmap --- src/BeSimple/SoapClient/SoapClient.php | 18 ++++++++++++- .../Tests/SoapClient/SoapClientTest.php | 26 ++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/BeSimple/SoapClient/SoapClient.php b/src/BeSimple/SoapClient/SoapClient.php index 9266a0c..5e7ba02 100644 --- a/src/BeSimple/SoapClient/SoapClient.php +++ b/src/BeSimple/SoapClient/SoapClient.php @@ -13,6 +13,7 @@ namespace BeSimple\SoapClient; use BeSimple\SoapCommon\Cache; +use BeSimple\SoapCommon\Classmap; use BeSimple\SoapCommon\Converter\TypeConverterCollection; /** @@ -21,6 +22,7 @@ use BeSimple\SoapCommon\Converter\TypeConverterCollection; class SoapClient { protected $wsdl; + protected $classmap; protected $converters; protected $soapClient; @@ -28,9 +30,10 @@ class SoapClient * @param string $wsdl * @param array $options */ - public function __construct($wsdl, array $options = array(), TypeConverterCollection $converters = null) + 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); @@ -163,10 +166,23 @@ class SoapClient return array( 'cache_wsdl' => $this->options['cache_type'], 'trace' => $this->options['debug'], + 'classmap' => $this->getClassmap(), 'typemap' => $this->getTypemap(), ); } + /** + * @return array + */ + protected function getClassmap() + { + if (!$this->classmap) { + return array(); + } + + return $this->classmap->all(); + } + /** * @return array */ diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php index 580bcbe..95b0d37 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php @@ -13,6 +13,7 @@ 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; @@ -88,10 +89,29 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase { Cache::setType(Cache::TYPE_MEMORY); $soapClient = new SoapClient('foo.wsdl', array('debug' => true)); - $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'typemap' => array()), $soapClient->getSoapOptions()); + $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'classmap' => array(), 'typemap' => array()), $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, 'typemap' => array()), $soapClient->getSoapOptions()); + $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false, 'classmap' => array(), 'typemap' => array()), $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() @@ -104,7 +124,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase $dateTypeConverter = new DateTypeConverter(); $converters->add($dateTypeConverter); - $soapClient = new SoapClient('foo.wsdl', array(), $converters); + $soapClient = new SoapClient('foo.wsdl', array(), null, $converters); $soapOptions = $soapClient->getSoapOptions(); $this->assertEquals('http://www.w3.org/2001/XMLSchema', $soapOptions['typemap'][0]['type_ns']); From bc9df9d2d7202be7fc98b29db7f885a3c6a25aa6 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 8 Oct 2011 22:02:54 +0200 Subject: [PATCH 03/12] [SoapClient] Added exceptions and user_agent options --- src/BeSimple/SoapClient/SoapClient.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/BeSimple/SoapClient/SoapClient.php b/src/BeSimple/SoapClient/SoapClient.php index 5e7ba02..10de074 100644 --- a/src/BeSimple/SoapClient/SoapClient.php +++ b/src/BeSimple/SoapClient/SoapClient.php @@ -45,6 +45,8 @@ class SoapClient '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 @@ -167,7 +169,9 @@ class SoapClient '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'], ); } From 0d59bd254591e61b0f99a00062fa45df7c47607c Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sun, 9 Oct 2011 20:10:13 +0200 Subject: [PATCH 04/12] Fixed tests --- tests/BeSimple/Tests/SoapClient/SoapClientTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php index 95b0d37..d947f74 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php @@ -31,7 +31,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase ); $soapClient->setOptions($options); - $this->assertEquals($options, $soapClient->getOptions()); + $this->assertEquals(array_merge($options, array('exceptions' => true, 'user_agent' => 'BeSimpleSoap')), $soapClient->getOptions()); } public function testSetOptionsThrowsAnExceptionIfOptionsDoesNotExists() @@ -89,10 +89,10 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase { Cache::setType(Cache::TYPE_MEMORY); $soapClient = new SoapClient('foo.wsdl', array('debug' => true)); - $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'classmap' => array(), 'typemap' => array()), $soapClient->getSoapOptions()); + $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(), 'typemap' => array()), $soapClient->getSoapOptions()); + $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false, 'classmap' => array(), 'exceptions' => true, 'typemap' => array(), 'user_agent' => 'BeSimpleSoap'), $soapClient->getSoapOptions()); } public function testGetSoapOptionsWithClassmap() From 28ed21530d97c2d7131fe73f4e0a0d5c2e874fb0 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sun, 9 Oct 2011 20:17:50 +0200 Subject: [PATCH 05/12] Added SoapClientBuilder --- src/BeSimple/SoapClient/SoapClientBuilder.php | 55 +++++++++++++ .../SoapClient/SoapClientBuilderTest.php | 81 +++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/BeSimple/SoapClient/SoapClientBuilder.php create mode 100644 tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php new file mode 100644 index 0000000..7134cf6 --- /dev/null +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -0,0 +1,55 @@ + + * (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\SoapClient; + +use BeSimple\SoapCommon\AbstractSoapBuilder; + +/** + * @author Francis Besset + */ +class SoapClientBuilder extends AbstractSoapBuilder +{ + protected $wsdl; + protected $options; + + /** + * @return SoapClientBuilder + */ + static public function createWithDefaults() + { + return parent::createWithDefaults() + ->withUserAgent('BeSimpleSoap') + ; + } + + public function withTrace($trace = true) + { + $this->options['trace'] = $trace; + + return $this; + } + + public function withExceptions($exceptions = true) + { + $this->options['exceptions'] = $exceptions; + + return $this; + } + + public function withUserAgent($userAgent) + { + $this->options['user_agent'] = $userAgent; + + return $this; + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php new file mode 100644 index 0000000..cad1111 --- /dev/null +++ b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php @@ -0,0 +1,81 @@ + + * (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\SoapCommon\Soap; + +use BeSimple\SoapClient\SoapClientBuilder; + +class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase +{ + private $defaultOptions = array( + 'features' => 0, + ); + + public function testContruct() + { + $options = $this + ->getSoapBuilder() + ->getOptions() + ; + + $this->assertEquals($this->mergeOptions(array()), $options); + } + + public function testWithTrace() + { + $builder = $this->getSoapBuilder(); + + $builder->withTrace(); + $this->assertEquals($this->mergeOptions(array('trace' => true)), $builder->getOptions()); + + $builder->withTrace(false); + $this->assertEquals($this->mergeOptions(array('trace' => false)), $builder->getOptions()); + } + + public function testWithExceptions() + { + $builder = $this->getSoapBuilder(); + + $builder->withExceptions(); + $this->assertEquals($this->mergeOptions(array('exceptions' => true)), $builder->getOptions()); + + $builder->withExceptions(false); + $this->assertEquals($this->mergeOptions(array('exceptions' => false)), $builder->getOptions()); + } + + public function testWithUserAgent() + { + $builder = $this->getSoapBuilder(); + + $builder->withUserAgent('BeSimpleSoap Test'); + $this->assertEquals($this->mergeOptions(array('user_agent' => 'BeSimpleSoap Test')), $builder->getOptions()); + } + + public function testCreateWithDefaults() + { + $builder = SoapClientBuilder::createWithDefaults(); + + $this->assertInstanceOf('BeSimple\SoapClient\SoapClientBuilder', $builder); + + $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'user_agent' => 'BeSimpleSoap')), $builder->getOptions()); + } + + private function getSoapBuilder() + { + return new SoapClientBuilder(); + } + + private function mergeOptions(array $options) + { + return array_merge($this->defaultOptions, $options); + } +} \ No newline at end of file From 46ced393ca55680ecff04cc5018078af506cf341 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Mon, 10 Oct 2011 00:02:20 +0200 Subject: [PATCH 06/12] Updated SoapClientBuilder --- src/BeSimple/SoapClient/SoapClientBuilder.php | 9 +++------ .../Tests/SoapClient/SoapClientBuilderTest.php | 16 +++++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index 7134cf6..5ec9678 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -19,9 +19,6 @@ use BeSimple\SoapCommon\AbstractSoapBuilder; */ class SoapClientBuilder extends AbstractSoapBuilder { - protected $wsdl; - protected $options; - /** * @return SoapClientBuilder */ @@ -34,21 +31,21 @@ class SoapClientBuilder extends AbstractSoapBuilder public function withTrace($trace = true) { - $this->options['trace'] = $trace; + $this->soapOptions['trace'] = $trace; return $this; } public function withExceptions($exceptions = true) { - $this->options['exceptions'] = $exceptions; + $this->soapOptions['exceptions'] = $exceptions; return $this; } public function withUserAgent($userAgent) { - $this->options['user_agent'] = $userAgent; + $this->soapOptions['user_agent'] = $userAgent; return $this; } diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php index cad1111..7ba11c2 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php @@ -18,13 +18,15 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase { private $defaultOptions = array( 'features' => 0, + 'classmap' => array(), + 'typemap' => array(), ); public function testContruct() { $options = $this ->getSoapBuilder() - ->getOptions() + ->getSoapOptions() ; $this->assertEquals($this->mergeOptions(array()), $options); @@ -35,10 +37,10 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $builder = $this->getSoapBuilder(); $builder->withTrace(); - $this->assertEquals($this->mergeOptions(array('trace' => true)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('trace' => true)), $builder->getSoapOptions()); $builder->withTrace(false); - $this->assertEquals($this->mergeOptions(array('trace' => false)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('trace' => false)), $builder->getSoapOptions()); } public function testWithExceptions() @@ -46,10 +48,10 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $builder = $this->getSoapBuilder(); $builder->withExceptions(); - $this->assertEquals($this->mergeOptions(array('exceptions' => true)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('exceptions' => true)), $builder->getSoapOptions()); $builder->withExceptions(false); - $this->assertEquals($this->mergeOptions(array('exceptions' => false)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('exceptions' => false)), $builder->getSoapOptions()); } public function testWithUserAgent() @@ -57,7 +59,7 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $builder = $this->getSoapBuilder(); $builder->withUserAgent('BeSimpleSoap Test'); - $this->assertEquals($this->mergeOptions(array('user_agent' => 'BeSimpleSoap Test')), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('user_agent' => 'BeSimpleSoap Test')), $builder->getSoapOptions()); } public function testCreateWithDefaults() @@ -66,7 +68,7 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('BeSimple\SoapClient\SoapClientBuilder', $builder); - $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'user_agent' => 'BeSimpleSoap')), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'user_agent' => 'BeSimpleSoap')), $builder->getSoapOptions()); } private function getSoapBuilder() From a9b1bdc71416a2a878cd1be444dcde54b44702ce Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Mon, 10 Oct 2011 00:21:23 +0200 Subject: [PATCH 07/12] Deleted SoapClient --- src/BeSimple/SoapClient/SoapClient.php | 183 +----------------- src/BeSimple/SoapClient/SoapClientBuilder.php | 24 +++ .../Tests/SoapClient/SoapClientTest.php | 147 -------------- 3 files changed, 25 insertions(+), 329 deletions(-) delete mode 100644 tests/BeSimple/Tests/SoapClient/SoapClientTest.php 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 From 9ce673b8cb9eda615fe3934dd9946d2822ed37a9 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Mon, 10 Oct 2011 00:40:21 +0200 Subject: [PATCH 08/12] Added Authentication in SoapClientBuilder --- src/BeSimple/SoapClient/SoapClientBuilder.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index 63c9934..fdb7f39 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -16,9 +16,12 @@ use BeSimple\SoapCommon\AbstractSoapBuilder; /** * @author Francis Besset + * @author Christian Kerl */ class SoapClientBuilder extends AbstractSoapBuilder { + protected $soapOptionAuthentication = array(); + /** * @return SoapClientBuilder */ @@ -36,7 +39,7 @@ class SoapClientBuilder extends AbstractSoapBuilder { $this->validateOptions(); - return new SoapClient($this->optionWsdl, $this->options); + return new SoapClient($this->optionWsdl, $this->getSoapOptions() + $this->soapOptionAuthentication); } /** @@ -69,6 +72,34 @@ class SoapClientBuilder extends AbstractSoapBuilder return $this; } + /** + * @return SoapClientBuilder + */ + public function withBasicAuthentication($username, $password) + { + $this->soapOptionAuthentication = array( + 'authentication' => SOAP_AUTHENTICATION_BASIC, + 'login' => $username, + 'password' => $password + ); + + return $this; + } + + /** + * @return SoapClientBuilder + */ + public function withDigestAuthentication($certificate, $password) + { + $this->soapOptionAuthentication = array( + 'authentication' => SOAP_AUTHENTICATION_DIGEST, + 'local_cert' => $certificate, + 'passphrase' => $password + ); + + return $this; + } + protected function validateOptions() { $this->validateWsdl(); From 24a912b50d175ee9b222c09015711c4a84c47350 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Mon, 10 Oct 2011 20:13:42 +0200 Subject: [PATCH 09/12] Fixed typo --- src/BeSimple/SoapClient/SoapClientBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index fdb7f39..20e5dc0 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -39,7 +39,7 @@ class SoapClientBuilder extends AbstractSoapBuilder { $this->validateOptions(); - return new SoapClient($this->optionWsdl, $this->getSoapOptions() + $this->soapOptionAuthentication); + return new SoapClient($this->wsdl, $this->getSoapOptions() + $this->soapOptionAuthentication); } /** From 55a9b6e2335db1c5b6400baebc11a8cc7ea1610e Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Tue, 11 Oct 2011 21:50:07 +0200 Subject: [PATCH 10/12] Fixed typo and added unit tests --- src/BeSimple/SoapClient/SoapClientBuilder.php | 16 ++++++++++++---- .../Tests/SoapClient/SoapClientBuilderTest.php | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index 20e5dc0..a8d5e15 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -39,7 +39,12 @@ class SoapClientBuilder extends AbstractSoapBuilder { $this->validateOptions(); - return new SoapClient($this->wsdl, $this->getSoapOptions() + $this->soapOptionAuthentication); + return new SoapClient($this->wsdl, $this->getSoapOptions()); + } + + public function getSoapOptions() + { + return parent::getSoapOptions() + $this->soapOptionAuthentication; } /** @@ -80,7 +85,7 @@ class SoapClientBuilder extends AbstractSoapBuilder $this->soapOptionAuthentication = array( 'authentication' => SOAP_AUTHENTICATION_BASIC, 'login' => $username, - 'password' => $password + 'password' => $password, ); return $this; @@ -89,14 +94,17 @@ class SoapClientBuilder extends AbstractSoapBuilder /** * @return SoapClientBuilder */ - public function withDigestAuthentication($certificate, $password) + public function withDigestAuthentication($certificate, $passphrase = null) { $this->soapOptionAuthentication = array( 'authentication' => SOAP_AUTHENTICATION_DIGEST, 'local_cert' => $certificate, - 'passphrase' => $password ); + if ($passphrase) { + $this->soapOptionAuthentication['passphrase'] = $passphrase; + } + return $this; } diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php index 7ba11c2..d256a01 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php @@ -62,6 +62,20 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->mergeOptions(array('user_agent' => 'BeSimpleSoap Test')), $builder->getSoapOptions()); } + public function testWithAuthentication() + { + $builder = $this->getSoapBuilder(); + + $builder->withDigestAuthentication(__DIR__.'/Fixtures/cert.pem', 'foobar'); + $this->assertEquals($this->mergeOptions(array('authentication' => SOAP_AUTHENTICATION_DIGEST, 'local_cert' => __DIR__.'/Fixtures/cert.pem', 'passphrase' => 'foobar')), $builder->getSoapOptions()); + + $builder->withDigestAuthentication(__DIR__.'/Fixtures/cert.pem'); + $this->assertEquals($this->mergeOptions(array('authentication' => SOAP_AUTHENTICATION_DIGEST, 'local_cert' => __DIR__.'/Fixtures/cert.pem')), $builder->getSoapOptions()); + + $builder->withBasicAuthentication('foo', 'bar'); + $this->assertEquals($this->mergeOptions(array('authentication' => SOAP_AUTHENTICATION_BASIC, 'login' => 'foo', 'password' => 'bar')), $builder->getSoapOptions()); + } + public function testCreateWithDefaults() { $builder = SoapClientBuilder::createWithDefaults(); From d6c9074c942af4395eb8ae10cff77892618d3aeb Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Tue, 11 Oct 2011 22:07:16 +0200 Subject: [PATCH 11/12] Added SoapClientBuilder::withProxy() --- src/BeSimple/SoapClient/SoapClientBuilder.php | 13 +++++++++++++ .../Tests/SoapClient/SoapClientBuilderTest.php | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index a8d5e15..9d5cfa2 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -108,6 +108,19 @@ class SoapClientBuilder extends AbstractSoapBuilder return $this; } + public function withProxy($host, $port, $username = null, $password = null) + { + $this->soapOptions['proxy_host'] = $host; + $this->soapOptions['proxy_port'] = $port; + + if ($username) { + $this->soapOptions['proxy_login'] = $username; + $this->soapOptions['proxy_password'] = $password; + } + + return $this; + } + protected function validateOptions() { $this->validateWsdl(); diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php index d256a01..b04f145 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php @@ -76,6 +76,17 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->mergeOptions(array('authentication' => SOAP_AUTHENTICATION_BASIC, 'login' => 'foo', 'password' => 'bar')), $builder->getSoapOptions()); } + public function testWithProxy() + { + $builder = $this->getSoapBuilder(); + + $builder->withProxy('localhost', 8080); + $this->assertEquals($this->mergeOptions(array('proxy_host' => 'localhost', 'proxy_port' => 8080)), $builder->getSoapOptions()); + + $builder->withProxy('127.0.0.1', 8585, 'foo', 'bar'); + $this->assertEquals($this->mergeOptions(array('proxy_host' => '127.0.0.1', 'proxy_port' => 8585, 'proxy_login' => 'foo', 'proxy_password' => 'bar')), $builder->getSoapOptions()); + } + public function testCreateWithDefaults() { $builder = SoapClientBuilder::createWithDefaults(); From 004d88f564ee6bb206adf8085e22c3401bef293c Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Tue, 11 Oct 2011 22:39:55 +0200 Subject: [PATCH 12/12] Added SoapClientBuilder::withCompression() --- src/BeSimple/SoapClient/SoapClientBuilder.php | 10 ++++++++++ .../Tests/SoapClient/SoapClientBuilderTest.php | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php index 9d5cfa2..65a6f9b 100644 --- a/src/BeSimple/SoapClient/SoapClientBuilder.php +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -77,6 +77,16 @@ class SoapClientBuilder extends AbstractSoapBuilder return $this; } + public function withCompressionGzip() + { + $this->soapOptions['compression'] = SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP; + } + + public function withCompressionDeflate() + { + $this->soapOptions['compression'] = SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_DEFLATE; + } + /** * @return SoapClientBuilder */ diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php index b04f145..9b33cce 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php @@ -62,6 +62,17 @@ class SoapClientBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->mergeOptions(array('user_agent' => 'BeSimpleSoap Test')), $builder->getSoapOptions()); } + public function testWithCompression() + { + $builder = $this->getSoapBuilder(); + + $builder->withCompressionGzip(); + $this->assertEquals($this->mergeOptions(array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP)), $builder->getSoapOptions()); + + $builder->withCompressionDeflate(); + $this->assertEquals($this->mergeOptions(array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_DEFLATE)), $builder->getSoapOptions()); + } + public function testWithAuthentication() { $builder = $this->getSoapBuilder();