From a0a92dd1b419f632566f8ab8bb789ff5e4c0ce73 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Tue, 13 Sep 2011 21:34:39 +0200 Subject: [PATCH 1/4] Added fixtures files --- .../RpcLiteralRequestMessageBinderTest.php | 2 +- .../RpcLiteralResponseMessageBinderTest.php | 227 ------------------ .../fixtures/ServiceBinding/BarRecursive.php | 13 + .../fixtures/ServiceBinding/FooRecursive.php | 8 + 4 files changed, 22 insertions(+), 228 deletions(-) delete mode 100644 Tests/ServiceBinding/RpcLiteralResponseMessageBinderTest.php create mode 100644 Tests/fixtures/ServiceBinding/BarRecursive.php create mode 100644 Tests/fixtures/ServiceBinding/FooRecursive.php diff --git a/Tests/ServiceBinding/RpcLiteralRequestMessageBinderTest.php b/Tests/ServiceBinding/RpcLiteralRequestMessageBinderTest.php index e021e94..c656611 100644 --- a/Tests/ServiceBinding/RpcLiteralRequestMessageBinderTest.php +++ b/Tests/ServiceBinding/RpcLiteralRequestMessageBinderTest.php @@ -136,7 +136,7 @@ class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase $foo = new Fixtures\FooRecursive('foo', ''); $bar = new Fixtures\BarRecursive($foo, 10394); - $foo->setBar($bar); + $foo->bar = $bar; $result = $messageBinder->processMessage( new Definition\Method('prevent_infinite_recursion', null, array(), array( diff --git a/Tests/ServiceBinding/RpcLiteralResponseMessageBinderTest.php b/Tests/ServiceBinding/RpcLiteralResponseMessageBinderTest.php deleted file mode 100644 index 9da41e8..0000000 --- a/Tests/ServiceBinding/RpcLiteralResponseMessageBinderTest.php +++ /dev/null @@ -1,227 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace BeSimple\SoapBundle\Tests\ServiceBinding; - -use BeSimple\SoapBundle\ServiceDefinition\Method; -use BeSimple\SoapBundle\ServiceDefinition\Type; -use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType; -use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType; -use BeSimple\SoapBundle\ServiceBinding\RpcLiteralResponseMessageBinder; -use BeSimple\SoapBundle\Util\Collection; -use BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes; -use BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType; -use BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters; - -/** - * UnitTest for \BeSimple\SoapBundle\ServiceBinding\RpcLiteralRequestMessageBinder. - * - * @author Francis Besset - */ -class RpcLiteralResponseMessageBinderTest extends \PHPUnit_Framework_TestCase -{ - /** - * @dataProvider messageProvider - */ - public function testProcessMessage(Method $method, $message, $assert) - { - $messageBinder = new RpcLiteralResponseMessageBinder(); - $result = $messageBinder->processMessage($method, $message); - - $this->assertSame($assert, $result); - } - - public function testProcessMessageWithComplexType() - { - $definitionComplexTypes = $this->getDefinitionComplexTypes(); - - $attributes = new Attributes(); - $attributes->foo = 'foobar'; - $attributes->bar = 20349; - $messageBinder = new RpcLiteralResponseMessageBinder(); - $result = $messageBinder->processMessage( - new Method('complextype', null, array(), array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes')), - $attributes, - $definitionComplexTypes - ); - - $this->assertInstanceOf('stdClass', $result); - $this->assertSame('foobar', $result->foo); - $this->assertSame(20349, $result->bar); - - $attributes1 = new Attributes(); - $attributes1->foo = 'bar'; - $attributes1->bar = 2929; - $attributes2 = new Attributes(); - $attributes2->foo = 'foo'; - $attributes2->bar = 123992; - $message = array($attributes1, $attributes2); - $messageBinder = new RpcLiteralResponseMessageBinder(); - $result = $messageBinder->processMessage( - new Method('complextype_argument', null, array(), array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')), - $message, - $definitionComplexTypes - ); - - $this->assertTrue(is_array($result)); - - $this->assertInstanceOf('stdClass', $result[0]); - $this->assertSame('bar', $result[0]->foo); - $this->assertSame(2929, $result[0]->bar); - - $this->assertInstanceOf('stdClass', $result[1]); - $this->assertSame('foo', $result[1]->foo); - $this->assertSame(123992, $result[1]->bar); - } - - public function testProcessMessageWithComplexTypeMethods() - { - $setters = new Setters(); - $setters->setFoo('foobar'); - $setters->setBar(42); - - $messageBinder = new RpcLiteralResponseMessageBinder(); - $result = $messageBinder->processMessage( - new Method('complextype_methods', null, array(), array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters')), - $setters, - $this->getDefinitionComplexTypes() - ); - - $this->assertInstanceOf('stdClass', $result); - $this->assertSame('foobar', $result->foo); - $this->assertSame(42, $result->bar); - } - - public function testProcessMessageWithComplexTypeIntoComplexType() - { - $complexType = new ComplexType(); - - $foo = new Attributes(); - $foo->foo = 'Hello world!'; - $foo->bar = 4242; - $complexType->setFoo($foo); - - $bar = new Setters(); - $bar->setFoo('bar foo'); - $bar->setBar(2424); - $complexType->bar = $bar; - - $messageBinder = new RpcLiteralResponseMessageBinder(); - $result = $messageBinder->processMessage( - new Method('complextype_complextype', null, array(), array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType')), - $complexType, - $this->getDefinitionComplexTypes() - ); - - $this->assertInstanceOf('stdClass', $result); - - $this->assertInstanceOf('stdClass', $result->foo); - $this->assertSame('Hello world!', $result->foo->foo); - $this->assertSame(4242, $result->foo->bar); - - $this->assertInstanceOf('stdClass', $result->bar); - $this->assertSame('bar foo', $result->bar->foo); - $this->assertSame(2424, $result->bar->bar); - } - - public function testProcessMessageWithComplexTypeReferences() - { - $attributes = new Attributes(); - $attributes->foo = 'bar'; - $attributes->bar = 2929; - - $message = array($attributes, $attributes); - $messageBinder = new RpcLiteralResponseMessageBinder(); - $result = $messageBinder->processMessage( - new Method('complextype_argument', null, array(), array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')), - $message, - $this->getDefinitionComplexTypes() - ); - - $this->assertInstanceOf('stdClass', $result[0]); - $this->assertSame($result[0], $result[1]); - } - - public function messageProvider() - { - $messages = array(); - - $messages[] = array( - new Method('boolean', null, array(), array(), new Type('boolean')), - true, - true, - ); - - $messages[] = array( - new Method('strings', null, array(), array(), new Type('string[]')), - array('hello', 'world'), - array('hello', 'world'), - ); - - return $messages; - } - - private function getDefinitionComplexTypes() - { - $this->definitionComplexTypes = array(); - - $this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes'] = $this->createPropertiesCollection(array( - array('foo', 'string'), - array('bar', 'int'), - )); - - $this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters'] = $this->createMethodsCollection(array( - array('foo', 'string', 'getFoo', 'setFoo'), - array('bar', 'int', 'getBar', 'setBar'), - )); - - $collection = $this->createMethodsCollection(array( - array('foo', '\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', 'getFoo', 'setFoo'), - )); - $this->createPropertiesCollection(array( - array('bar', '\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters'), - ), $collection); - $this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType'] = $collection; - - return $this->definitionComplexTypes; - } - - private function createPropertiesCollection(array $properties, Collection $collection = null) - { - $collection = $collection ?: new Collection('getName'); - - foreach ($properties as $property) { - $collectionProperty = new PropertyComplexType(); - $collectionProperty->setName($property[0]); - $collectionProperty->setValue($property[1]); - - $collection->add($collectionProperty); - } - - return $collection; - } - - private function createMethodsCollection(array $methods, Collection $collection = null) - { - $collection = $collection ?: new Collection('getName'); - - foreach ($methods as $method) { - $collectionMethod = new MethodComplexType(); - $collectionMethod->setName($method[0]); - $collectionMethod->setValue($method[1]); - $collectionMethod->setOriginalName($method[2]); - $collectionMethod->setSetter($method[3]); - - $collection->add($collectionMethod); - } - - return $collection; - } -} diff --git a/Tests/fixtures/ServiceBinding/BarRecursive.php b/Tests/fixtures/ServiceBinding/BarRecursive.php new file mode 100644 index 0000000..c7d3d14 --- /dev/null +++ b/Tests/fixtures/ServiceBinding/BarRecursive.php @@ -0,0 +1,13 @@ +foo = $foo; + } +} \ No newline at end of file diff --git a/Tests/fixtures/ServiceBinding/FooRecursive.php b/Tests/fixtures/ServiceBinding/FooRecursive.php new file mode 100644 index 0000000..b39c9f1 --- /dev/null +++ b/Tests/fixtures/ServiceBinding/FooRecursive.php @@ -0,0 +1,8 @@ + Date: Wed, 14 Sep 2011 22:03:46 +0200 Subject: [PATCH 2/4] Fixed issue #10 --- ServiceBinding/RpcLiteralResponseMessageBinder.php | 13 ++++--------- ServiceDefinition/Type.php | 5 +++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ServiceBinding/RpcLiteralResponseMessageBinder.php b/ServiceBinding/RpcLiteralResponseMessageBinder.php index ed3f2e4..5cb6310 100644 --- a/ServiceBinding/RpcLiteralResponseMessageBinder.php +++ b/ServiceBinding/RpcLiteralResponseMessageBinder.php @@ -67,17 +67,12 @@ class RpcLiteralResponseMessageBinder implements MessageBinderInterface $this->messageRefs[$hash] = $message; - $class = $phpType; - if ($class[0] == '\\') { - $class = substr($class, 1); - } - - if (!$message instanceof $class) { - throw new \InvalidArgumentException(sprintf('The instance class must be "%s", "%s" given.', get_class($message), $class)); + if (!$message instanceof $phpType) { + throw new \InvalidArgumentException(sprintf('The instance class must be "%s", "%s" given.', get_class($message), $phpType)); } $r = new \ReflectionClass($message); - foreach ($this->definitionComplexTypes[$class] as $type) { + foreach ($this->definitionComplexTypes[$phpType] as $type) { $p = $r->getProperty($type->getName()); if ($p->isPublic()) { $value = $message->{$type->getName()}; @@ -91,7 +86,7 @@ class RpcLiteralResponseMessageBinder implements MessageBinderInterface } if (!$type->isNillable() && null === $value) { - throw new \InvalidArgumentException(sprintf('"%s::%s" cannot be null.', $class, $type->getName())); + throw new \InvalidArgumentException(sprintf('"%s::%s" cannot be null.', $phpType, $type->getName())); } } diff --git a/ServiceDefinition/Type.php b/ServiceDefinition/Type.php index bf2c1d1..2855689 100644 --- a/ServiceDefinition/Type.php +++ b/ServiceDefinition/Type.php @@ -30,6 +30,11 @@ class Type public function setPhpType($phpType) { + $phpType = $phpType; + if ($phpType[0] == '\\') { + $phpType = substr($phpType, 1); + } + $this->phpType = $phpType; } From dbeb00eae2ad129192e2cd189dc2d3aea562ad91 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Tue, 20 Sep 2011 22:34:16 +0200 Subject: [PATCH 3/4] Added the possibility to add headers for a controller Example: reader->getClassAnnotations($class) as $annotation) { + if ($annotation instanceof Annotation\Header) { + $serviceMethodHeaders[$annotation->getValue()] = $annotation; + } + } + foreach ($class->getMethods() as $method) { $serviceArguments = $serviceHeaders = array(); $serviceMethod = $serviceReturn = null; - foreach ($this->reader->getMethodAnnotations($method) as $i => $annotation) { + foreach ($serviceMethodHeaders as $annotation) { + $serviceHeaders[$annotation->getValue()] = new Definition\Header( + $annotation->getValue(), + $this->getArgumentType($method, $annotation) + ); + } + + foreach ($this->reader->getMethodAnnotations($method) as $annotation) { if ($annotation instanceof Annotation\Header) { - $serviceHeaders[] = new Definition\Header( + $serviceHeaders[$annotation->getValue()] = new Definition\Header( $annotation->getValue(), $this->getArgumentType($method, $annotation) ); From edee6a4c72fda9c8505c15e3e8a4f83ae76a5d62 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Thu, 22 Sep 2011 00:16:11 +0200 Subject: [PATCH 4/4] [Doc] Updated header tutorial to specify global headers functionality --- Resources/doc/tutorial/header.rst | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Resources/doc/tutorial/header.rst b/Resources/doc/tutorial/header.rst index c3928df..687dfef 100644 --- a/Resources/doc/tutorial/header.rst +++ b/Resources/doc/tutorial/header.rst @@ -30,4 +30,60 @@ Controller return $this->container->get('besimple.soap.response')->setReturnValue("Hello ".implode(', ', $names)); } + } + +Global header +------------- + +If you want use a header for all actions of your controller you can declare the header like this: + +.. code-block:: php + + namespace My\App\Controller; + + use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap; + use Symfony\Component\DependencyInjection\ContainerAware; + use Symfony\Component\DependencyInjection\ContainerInterface; + + /** + * @Soap\Header("api_key", phpType = "string") + */ + class DemoController extends ContainerAware + { + /** + * @Soap\Method("hello") + * @Soap\Param("names", phpType = "string[]") + * @Soap\Result(phpType = "string") + */ + public function helloAction(array $names) + { + return $this->container->get('besimple.soap.response')->setReturnValue("Hello ".implode(', ', $names)); + } + + /** + * @Soap\Method("welcome") + * @Soap\Param("names", phpType = "string[]") + * @Soap\Result(phpType = "string") + */ + public function welcomeAction() + { + return $this->container->get('besimple.soap.response')->setReturnValue("Welcome ".implode(', ', $names)); + } + + public function setContainer(ContainerInterface $container = null) + { + parent::setContainer($container); + + $this->checkApiKeyHeader(); + } + + private function checkApiKeyHeader() + { + $soapHeaders = $this->container->get('request')->getSoapHeaders(); + + // You can use '1234' !== (string) $soapHeaders->get('api_key') + if (!$soapHeaders->has('api_key') || '1234' !== $soapHeaders->get('api_key')->getData()) { + throw new \SoapFault("INVALID_API_KEY", "The api_key is invalid."); + } + } } \ No newline at end of file