commit fd162d8ca668a1ef9c5089b9475ef5ba8c68d0e0 Author: Francis Besset Date: Sat Sep 3 21:15:51 2011 +0200 Initial commit diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..4f8e547 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,31 @@ + + + + + + ./tests/BeSimple/ + + + + + + benchmark + + + + + + ./src/BeSimple/ + + + diff --git a/src/BeSimple/SoapClient/Soap/SoapClient.php b/src/BeSimple/SoapClient/Soap/SoapClient.php new file mode 100644 index 0000000..61273b1 --- /dev/null +++ b/src/BeSimple/SoapClient/Soap/SoapClient.php @@ -0,0 +1,143 @@ + + * (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\Soap; + +/** + * @author Francis Besset + */ +class SoapClient +{ + protected $wsdl; + protected $soapClient; + + /** + * @param string $wsdl + * @param array $options + */ + public function __construct($wsdl, array $options = array()) + { + $this->wsdl = $wsdl; + $this->setOptions($options); + } + + public function setOptions(array $options) + { + $this->options = array( + 'cache_dir' => null, + 'debug' => false, + ); + + // 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() + ); + } + + /** + * @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() + { + $options = array(); + + $options['cache_wsdl'] = $this->options['debug'] ? WSDL_CACHE_NONE : WSDL_CACHE_DISK; + $options['trace'] = $this->options['debug']; + + return $options; + } +} \ No newline at end of file diff --git a/src/BeSimple/SoapClient/Soap/SoapRequest.php b/src/BeSimple/SoapClient/Soap/SoapRequest.php new file mode 100644 index 0000000..1963a64 --- /dev/null +++ b/src/BeSimple/SoapClient/Soap/SoapRequest.php @@ -0,0 +1,159 @@ + + * (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\Soap; + +/** + * @author Francis Besset + */ +class SoapRequest +{ + protected $function; + protected $arguments; + protected $options; + + public function __construct($function = null, array $arguments = array(), array $options = array()) + { + $this->function = $function; + $this->arguments = $arguments; + $this->options = $options; + } + + /** + * @return string The function name + */ + public function getFunction() + { + return $this->function; + } + + /** + * @param string The function name + * + * @return SoapRequest + */ + public function setFunction($function) + { + $this->function = $function; + + return $this; + } + + /** + * @return array An array with all arguments + */ + public function getArguments() + { + return $this->arguments; + } + + /** + * @param string The name of the argument + * @param mixed The default value returned if the argument is not exists + * + * @return mixed + */ + public function getArgument($name, $default = null) + { + return $this->hasArgument($name) ? $this->arguments[$name] : $default; + } + + /** + * @param string The name of the argument + * + * @return boolean + */ + public function hasArgument($name) + { + return isset($this->arguments[$name]); + } + + /** + * @param array An array with arguments + * + * @return SoapRequest + */ + public function setArguments(array $arguments) + { + $this->arguments = $arguments; + + return $this; + } + + /** + * @param string The name of argument + * @param mixed The value of argument + * + * @return SoapRequest + */ + public function addArgument($name, $value) + { + $this->arguments[$name] = $value; + + return $this; + } + + /** + * @return array An array with all options + */ + public function getOptions() + { + return $this->options; + } + + /** + * @param string The name of the option + * @param mixed The default value returned if the option is not exists + * + * @return mixed + */ + public function getOption($name, $default = null) + { + return $this->hasOption($name) ? $this->options[$name] : $default; + } + + /** + * @param string The name of the option + * + * @return boolean + */ + public function hasOption($name) + { + return isset($this->options[$name]); + } + + /** + * @param array An array with options + * + * @return SoapRequest + */ + public function setOptions(array $options) + { + $this->options = $options; + + return $this; + } + + /** + * @param string The name of option + * @param mixed The value of option + * + * @return SoapRequest + */ + public function addOption($name, $value) + { + $this->options[$name] = $value; + + return $this; + } + +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapClient/Soap/Fixtures/foobar.wsdl b/tests/BeSimple/Tests/SoapClient/Soap/Fixtures/foobar.wsdl new file mode 100644 index 0000000..a890dd8 --- /dev/null +++ b/tests/BeSimple/Tests/SoapClient/Soap/Fixtures/foobar.wsdl @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/BeSimple/Tests/SoapClient/Soap/SoapClientTest.php b/tests/BeSimple/Tests/SoapClient/Soap/SoapClientTest.php new file mode 100644 index 0000000..e82ca30 --- /dev/null +++ b/tests/BeSimple/Tests/SoapClient/Soap/SoapClientTest.php @@ -0,0 +1,76 @@ + + * (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\Soap; + +use BeSimple\SoapClient\Soap\SoapClient; + +class SoapClientTest extends \PHPUnit_Framework_TestCase +{ + public function testSetOptions() + { + $soapClient = new SoapClient('foo.wsdl'); + $options = array( + 'cache_dir' => '/tmp', + 'debug' => true, + ); + $soapClient->setOptions($options); + + $this->assertEquals($options, $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 testGetSoapOptions() + { + $soapClient = new SoapClient('foo.wsdl', array('debug' => true)); + + $this->assertEquals(array('cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true), $soapClient->getSoapOptions()); + } + + public function testGetNativeSoapClient() + { + $soapClient = new SoapClient(__DIR__.'/Fixtures/foobar.wsdl', array('debug' => true)); + + $this->assertInstanceOf('SoapClient', $soapClient->getNativeSoapClient()); + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapClient/Soap/SoapRequestTest.php b/tests/BeSimple/Tests/SoapClient/Soap/SoapRequestTest.php new file mode 100644 index 0000000..8e06a23 --- /dev/null +++ b/tests/BeSimple/Tests/SoapClient/Soap/SoapRequestTest.php @@ -0,0 +1,89 @@ + + * (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\Soap; + +use BeSimple\SoapClient\Soap\SoapRequest; + +class SoapRequestTest extends \PHPUnit_Framework_TestCase +{ + public function testSetFunction() + { + $soapRequest = new SoapRequest(); + $soapRequest->setFunction('foo'); + + $this->assertEquals('foo', $soapRequest->getFunction()); + } + + public function testSetArguments() + { + $soapRequest = new SoapRequest(); + $arguments = array( + 'foo' => true, + 'bar' => false, + ); + $soapRequest->setArguments($arguments); + + $this->assertEquals($arguments, $soapRequest->getArguments()); + } + + public function testGetArgument() + { + $soapRequest = new SoapRequest(); + + $this->assertEquals(false, $soapRequest->getArgument('foo', false)); + + $soapRequest->addArgument('foo', 'bar'); + + $this->assertEquals('bar', $soapRequest->getArgument('foo', false)); + } + + public function testSetOptions() + { + $soapRequest = new SoapRequest(); + $options = array( + 'uri' => 'foo', + 'soapaction' => 'bar', + ); + $soapRequest->setOptions($options); + + $this->assertEquals($options, $soapRequest->getOptions()); + } + + public function testGetOption() + { + $soapRequest = new SoapRequest(); + + $this->assertEquals(false, $soapRequest->getOption('soapaction')); + + $soapRequest->addOption('soapaction', 'foo'); + + $this->assertEquals('foo', $soapRequest->getOption('soapaction')); + } + + public function testConstruct() + { + $soapRequest = new SoapRequest(); + + $this->assertNull($soapRequest->getFunction()); + $this->assertEquals(array(), $soapRequest->getArguments()); + $this->assertEquals(array(), $soapRequest->getOptions()); + + $arguments = array('bar' => 'foobar'); + $options = array('soapaction' => 'foobar'); + $soapRequest = new SoapRequest('foo', $arguments, $options); + + $this->assertEquals('foo', $soapRequest->getFunction()); + $this->assertEquals($arguments, $soapRequest->getArguments()); + $this->assertEquals($options, $soapRequest->getOptions()); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..80850f4 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,22 @@ +