diff --git a/src/BeSimple/SoapClient/SoapClientBuilder.php b/src/BeSimple/SoapClient/SoapClientBuilder.php new file mode 100644 index 0000000..65a6f9b --- /dev/null +++ b/src/BeSimple/SoapClient/SoapClientBuilder.php @@ -0,0 +1,138 @@ + + * (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 + * @author Christian Kerl + */ +class SoapClientBuilder extends AbstractSoapBuilder +{ + protected $soapOptionAuthentication = array(); + + /** + * @return SoapClientBuilder + */ + static public function createWithDefaults() + { + return parent::createWithDefaults() + ->withUserAgent('BeSimpleSoap') + ; + } + + /** + * @return SoapClient + */ + public function build() + { + $this->validateOptions(); + + return new SoapClient($this->wsdl, $this->getSoapOptions()); + } + + public function getSoapOptions() + { + return parent::getSoapOptions() + $this->soapOptionAuthentication; + } + + /** + * @return SoapClientBuilder + */ + public function withTrace($trace = true) + { + $this->soapOptions['trace'] = $trace; + + return $this; + } + + /** + * @return SoapClientBuilder + */ + public function withExceptions($exceptions = true) + { + $this->soapOptions['exceptions'] = $exceptions; + + return $this; + } + + /** + * @return SoapClientBuilder + */ + public function withUserAgent($userAgent) + { + $this->soapOptions['user_agent'] = $userAgent; + + 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 + */ + public function withBasicAuthentication($username, $password) + { + $this->soapOptionAuthentication = array( + 'authentication' => SOAP_AUTHENTICATION_BASIC, + 'login' => $username, + 'password' => $password, + ); + + return $this; + } + + /** + * @return SoapClientBuilder + */ + public function withDigestAuthentication($certificate, $passphrase = null) + { + $this->soapOptionAuthentication = array( + 'authentication' => SOAP_AUTHENTICATION_DIGEST, + 'local_cert' => $certificate, + ); + + if ($passphrase) { + $this->soapOptionAuthentication['passphrase'] = $passphrase; + } + + 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(); + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapClient/SimpleSoapClientTest.php b/tests/BeSimple/Tests/SoapClient/SimpleSoapClientTest.php deleted file mode 100644 index 89b8ef7..0000000 --- a/tests/BeSimple/Tests/SoapClient/SimpleSoapClientTest.php +++ /dev/null @@ -1,127 +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\Converter\DateTimeTypeConverter; -use BeSimple\SoapCommon\Converter\DateTypeConverter; -use BeSimple\SoapCommon\Converter\TypeConverterCollection; -use BeSimple\SoapClient\SimpleSoapClient; - -class SoapClientTest extends \PHPUnit_Framework_TestCase -{ - public function testSetOptions() - { - $soapClient = new SimpleSoapClient('foo.wsdl'); - $options = array( - 'cache_type' => Cache::TYPE_DISK_MEMORY, - 'debug' => true, - 'namespace' => 'foo', - ); - $soapClient->setOptions($options); - - $this->assertEquals($options, $soapClient->getOptions()); - } - - public function testSetOptionsThrowsAnExceptionIfOptionsDoesNotExists() - { - $soapClient = new SimpleSoapClient('foo.wsdl'); - - $this->setExpectedException('InvalidArgumentException'); - $soapClient->setOptions(array('bad_option' => true)); - } - - public function testSetOption() - { - $soapClient = new SimpleSoapClient('foo.wsdl'); - $soapClient->setOption('debug', true); - - $this->assertEquals(true, $soapClient->getOption('debug')); - } - - public function testSetOptionThrowsAnExceptionIfOptionDoesNotExists() - { - $soapClient = new SimpleSoapClient('foo.wsdl'); - - $this->setExpectedException('InvalidArgumentException'); - $soapClient->setOption('bad_option', 'bar'); - } - - public function testGetOptionThrowsAnExceptionIfOptionDoesNotExists() - { - $soapClient = new SimpleSoapClient('foo.wsdl'); - - $this->setExpectedException('InvalidArgumentException'); - $soapClient->getOption('bad_option'); - } - - public function testCreateSoapHeader() - { - $soapClient = new SimpleSoapClient('foo.wsdl', null, 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 SimpleSoapClient('foo.wsdl'); - - $this->setExpectedException('RuntimeException'); - $soapHeader = $soapClient->createSoapHeader('foo', 'bar'); - } - - public function testGetSoapOptions() - { - Cache::setType(Cache::TYPE_MEMORY); - $soapClient = new SimpleSoapClient('foo.wsdl', null, array('debug' => true)); - $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'typemap' => array()), $soapClient->getSoapOptions()); - - $soapClient = new SimpleSoapClient('foo.wsdl', null, array('debug' => false, 'cache_type' => Cache::TYPE_NONE)); - $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false, 'typemap' => array()), $soapClient->getSoapOptions()); - } - - public function testGetSoapOptionsWithTypemap() - { - $converters = new TypeConverterCollection(); - - $dateTimeTypeConverter = new DateTimeTypeConverter(); - $converters->add($dateTimeTypeConverter); - - $dateTypeConverter = new DateTypeConverter(); - $converters->add($dateTypeConverter); - - $soapClient = new SimpleSoapClient('foo.wsdl', $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 SimpleSoapClient(__DIR__.'/Fixtures/foobar.wsdl', null, array('debug' => true)); - - $this->assertInstanceOf('SoapClient', $soapClient->getNativeSoapClient()); - } -} \ 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..9b33cce --- /dev/null +++ b/tests/BeSimple/Tests/SoapClient/SoapClientBuilderTest.php @@ -0,0 +1,119 @@ + + * (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, + 'classmap' => array(), + 'typemap' => array(), + ); + + public function testContruct() + { + $options = $this + ->getSoapBuilder() + ->getSoapOptions() + ; + + $this->assertEquals($this->mergeOptions(array()), $options); + } + + public function testWithTrace() + { + $builder = $this->getSoapBuilder(); + + $builder->withTrace(); + $this->assertEquals($this->mergeOptions(array('trace' => true)), $builder->getSoapOptions()); + + $builder->withTrace(false); + $this->assertEquals($this->mergeOptions(array('trace' => false)), $builder->getSoapOptions()); + } + + public function testWithExceptions() + { + $builder = $this->getSoapBuilder(); + + $builder->withExceptions(); + $this->assertEquals($this->mergeOptions(array('exceptions' => true)), $builder->getSoapOptions()); + + $builder->withExceptions(false); + $this->assertEquals($this->mergeOptions(array('exceptions' => false)), $builder->getSoapOptions()); + } + + public function testWithUserAgent() + { + $builder = $this->getSoapBuilder(); + + $builder->withUserAgent('BeSimpleSoap Test'); + $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(); + + $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 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(); + + $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->getSoapOptions()); + } + + private function getSoapBuilder() + { + return new SoapClientBuilder(); + } + + private function mergeOptions(array $options) + { + return array_merge($this->defaultOptions, $options); + } +} \ No newline at end of file diff --git a/vendors.php b/vendors.php old mode 100755 new mode 100644