From 7d7603634d043a9aa7c95d853fca4a8a3b8cb9d4 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 15:15:23 +0200 Subject: [PATCH 01/11] Added the possibility to override the webservice location to allow one to load the WSDL from one place and to query a server that is different from the one specified in the WSDL --- src/Client.php | 7 +++++++ src/Protocol/ClientEncoder.php | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index 75c8bc0..13f71e5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -68,4 +68,11 @@ public function getTypes() { return $this->encoder->__getTypes(); } + + public function overrideLocation($location) + { + $copy = clone $this; + $copy->encoder = $this->encoder->overrideLocation($location); + return $copy; + } } diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index afd625a..35934b3 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -10,7 +10,8 @@ class ClientEncoder extends SoapClient { - private $request = null; + private $request = null; + private $locationOverride = null; public function encode($name, $args) { @@ -24,18 +25,27 @@ public function encode($name, $args) public function __doRequest($request, $location, $action, $version, $one_way = 0) { + $finalLocation = $this->locationOverride !== null ? $this->locationOverride : $location; + $this->request = new Request( 'POST', - (string)$location, + (string) $finalLocation, new Headers(array( - 'SOAPAction' => (string)$action, + 'SOAPAction' => (string) $action, 'Content-Type' => 'text/xml; charset=utf-8', 'Content-Length' => strlen($request) )), - new Body((string)$request) + new Body((string) $request) ); // do not actually block here, just pretend we're done... return ''; } + + public function overrideLocation($newLocation) + { + $copy = clone $this; + $this->locationOverride = $newLocation; + return $copy; + } } From ed3001801c97e8b4a1eed9d3e9a888e0ddd82fb9 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 16:21:06 +0200 Subject: [PATCH 02/11] - Introduced PHP Unit as a development composer dependency - Added tests for the location override feature --- composer.json | 3 +++ tests/FunctionalTest.php | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/composer.json b/composer.json index 48aa356..98dd48e 100644 --- a/composer.json +++ b/composer.json @@ -19,5 +19,8 @@ "react/event-loop": "0.3.*|0.4.*", "react/promise": "~1.0|~2.0", "ext-soap": "*" + }, + "require-dev": { + "phpunit/phpunit": "^4.8" } } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 67f5d85..1e16316 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -59,4 +59,24 @@ public function testBlzServiceWithInvalidMethod() $this->setExpectedException('Exception'); $this->waitForPromise($promise, $this->loop); } + + public function testWrongLocationOverride() + { + $this->client->overrideLocation('nonsense.not.existing'); + $api = new Proxy($this->client); + + $promise = $api->getBank(array('blz' => '12070000')); + + $this->expectPromiseReject($promise); + + $this->setExpectedException('Exception'); + $this->waitForPromise($promise, $this->loop); + } + + public function testCorrectLocationOverride() + { + $this->client->overrideLocation('nonsense.not.existing'); + $this->client->overrideLocation('http://www.thomas-bayer.com/axis2/services/BLZService'); + $this->testBlzService(); + } } From 913d3faf65f28a808126d85f400b7ec52a75b8d7 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 17:12:28 +0200 Subject: [PATCH 03/11] - Documentation/better naming for the location getter/override. - Test for the location getter --- README.md | 8 +++++ src/Client.php | 9 +++-- src/Protocol/ClientEncoder.php | 61 +++++++++++++++++++++++++--------- tests/FunctionalTest.php | 14 ++++++-- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 6f4261a..cf1d73d 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,14 @@ It returns the equivalent of PHP's [`SoapClient::__getFunctions()`](http://php.n The `getTypes()` method returns an array of types defined in the WSDL. It returns the equivalent of PHP's [`SoapClient::__getTypes()`](http://php.net/manual/en/soapclient.gettypes.php). +#### overrideTarget($newTarget) + +This method allows you to change the destination of your SOAP calls. + +#### getWsdlTarget() + +This method allows you to retrieve the target URL specified in the WSDL file. + ### Proxy The `Proxy` class wraps an existing [`Client`](#client) instance in order to ease calling diff --git a/src/Client.php b/src/Client.php index 13f71e5..1f97106 100644 --- a/src/Client.php +++ b/src/Client.php @@ -69,10 +69,15 @@ public function getTypes() return $this->encoder->__getTypes(); } - public function overrideLocation($location) + public function overrideTarget($target) { $copy = clone $this; - $copy->encoder = $this->encoder->overrideLocation($location); + $copy->encoder = $this->encoder->overrideTarget($target); return $copy; } + + public function getWsdlTarget() + { + return $this->encoder->getWsdlTarget(); + } } diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index 35934b3..c1f37f5 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -10,8 +10,10 @@ class ClientEncoder extends SoapClient { - private $request = null; - private $locationOverride = null; + private $request = null; + private $targetOverride = null; + private $target = null; + private $findTarget = false; public function encode($name, $args) { @@ -25,27 +27,54 @@ public function encode($name, $args) public function __doRequest($request, $location, $action, $version, $one_way = 0) { - $finalLocation = $this->locationOverride !== null ? $this->locationOverride : $location; - - $this->request = new Request( - 'POST', - (string) $finalLocation, - new Headers(array( - 'SOAPAction' => (string) $action, - 'Content-Type' => 'text/xml; charset=utf-8', - 'Content-Length' => strlen($request) - )), - new Body((string) $request) - ); + if ($this->findTarget) { + $this->target = $location; + $this->findTarget = false; + } else { + $finalLocation = $this->targetOverride !== null ? $this->targetOverride : $location; + + $this->request = new Request( + 'POST', + (string) $finalLocation, + new Headers(array( + 'SOAPAction' => (string) $action, + 'Content-Type' => 'text/xml; charset=utf-8', + 'Content-Length' => strlen($request) + )), + new Body((string) $request) + ); + } // do not actually block here, just pretend we're done... return ''; } - public function overrideLocation($newLocation) + public function overrideTarget($newTarget) { $copy = clone $this; - $this->locationOverride = $newLocation; + $this->targetOverride = $newTarget; return $copy; } + + public function getWsdlTarget() + { + /* + * We can't just use a function with an empty name. + * SoapClient complains if the request does not exist. + */ + $functionDescriptions = $this->__getFunctions(); + $functionDescription = $functionDescriptions[0]; /* PHP 5.3 support. */ + $spaceIndex = strpos($functionDescription, ' '); + $openingParenIndex = strpos($functionDescription, '('); + $function = substr( + $functionDescription, + $spaceIndex + 1, + $openingParenIndex - $spaceIndex - 1 + ); + + $this->findTarget = true; + $this->__soapCall($function, []); + return $this->target; + } + } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 1e16316..abc9ca1 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -62,7 +62,7 @@ public function testBlzServiceWithInvalidMethod() public function testWrongLocationOverride() { - $this->client->overrideLocation('nonsense.not.existing'); + $this->client->overrideTarget('nonsense.not.existing'); $api = new Proxy($this->client); $promise = $api->getBank(array('blz' => '12070000')); @@ -75,8 +75,16 @@ public function testWrongLocationOverride() public function testCorrectLocationOverride() { - $this->client->overrideLocation('nonsense.not.existing'); - $this->client->overrideLocation('http://www.thomas-bayer.com/axis2/services/BLZService'); + $this->client->overrideTarget('nonsense.not.existing'); + $this->client->overrideTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); $this->testBlzService(); } + + public function testGetLocation() + { + $this->assertEquals( + $this->client->getWsdlTarget(), + 'http://www.thomas-bayer.com/axis2/services/BLZService' + ); + } } From c20c08f57614b7b9554efef612ff6b4e3fff9cae Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 17:16:39 +0200 Subject: [PATCH 04/11] - Removed the dependency to PHPUnit in composer.json - Renamed overrideTarget to withOverridenTarget() everywhere --- README.md | 5 +++-- composer.json | 3 --- src/Client.php | 4 ++-- src/Protocol/ClientEncoder.php | 2 +- tests/FunctionalTest.php | 6 +++--- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index cf1d73d..7ceed7f 100644 --- a/README.md +++ b/README.md @@ -117,9 +117,10 @@ It returns the equivalent of PHP's [`SoapClient::__getFunctions()`](http://php.n The `getTypes()` method returns an array of types defined in the WSDL. It returns the equivalent of PHP's [`SoapClient::__getTypes()`](http://php.net/manual/en/soapclient.gettypes.php). -#### overrideTarget($newTarget) +#### withOverridenTarget($newTarget) -This method allows you to change the destination of your SOAP calls. +This method allows you to change the destination of your SOAP calls. It does not change the Client object, but returns a new +Client with the overriden target. #### getWsdlTarget() diff --git a/composer.json b/composer.json index 98dd48e..48aa356 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,5 @@ "react/event-loop": "0.3.*|0.4.*", "react/promise": "~1.0|~2.0", "ext-soap": "*" - }, - "require-dev": { - "phpunit/phpunit": "^4.8" } } diff --git a/src/Client.php b/src/Client.php index 1f97106..2798ad3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -69,10 +69,10 @@ public function getTypes() return $this->encoder->__getTypes(); } - public function overrideTarget($target) + public function withOverridenTarget($target) { $copy = clone $this; - $copy->encoder = $this->encoder->overrideTarget($target); + $copy->encoder = $this->encoder->withOverridenTarget($target); return $copy; } diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index c1f37f5..c397c68 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -49,7 +49,7 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 return ''; } - public function overrideTarget($newTarget) + public function withOverridenTarget($newTarget) { $copy = clone $this; $this->targetOverride = $newTarget; diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index abc9ca1..46d3dd8 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -62,7 +62,7 @@ public function testBlzServiceWithInvalidMethod() public function testWrongLocationOverride() { - $this->client->overrideTarget('nonsense.not.existing'); + $this->client->withOverridenTarget('nonsense.not.existing'); $api = new Proxy($this->client); $promise = $api->getBank(array('blz' => '12070000')); @@ -75,8 +75,8 @@ public function testWrongLocationOverride() public function testCorrectLocationOverride() { - $this->client->overrideTarget('nonsense.not.existing'); - $this->client->overrideTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); + $this->client->withOverridenTarget('nonsense.not.existing'); + $this->client->withOverridenTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); $this->testBlzService(); } From f9b067d111c40f9634e24bdf31f580b54e7fbc67 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 17:30:30 +0200 Subject: [PATCH 05/11] Renamed withOverridenTarget() to withTarget() everywhere --- README.md | 2 +- src/Client.php | 4 ++-- src/Protocol/ClientEncoder.php | 2 +- tests/FunctionalTest.php | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7ceed7f..e15e04e 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ It returns the equivalent of PHP's [`SoapClient::__getFunctions()`](http://php.n The `getTypes()` method returns an array of types defined in the WSDL. It returns the equivalent of PHP's [`SoapClient::__getTypes()`](http://php.net/manual/en/soapclient.gettypes.php). -#### withOverridenTarget($newTarget) +#### withTarget($newTarget) This method allows you to change the destination of your SOAP calls. It does not change the Client object, but returns a new Client with the overriden target. diff --git a/src/Client.php b/src/Client.php index 2798ad3..150bac5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -69,10 +69,10 @@ public function getTypes() return $this->encoder->__getTypes(); } - public function withOverridenTarget($target) + public function withTarget($target) { $copy = clone $this; - $copy->encoder = $this->encoder->withOverridenTarget($target); + $copy->encoder = $this->encoder->withTarget($target); return $copy; } diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index c397c68..728c878 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -49,7 +49,7 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 return ''; } - public function withOverridenTarget($newTarget) + public function withTarget($newTarget) { $copy = clone $this; $this->targetOverride = $newTarget; diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 46d3dd8..0c790ca 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -62,7 +62,7 @@ public function testBlzServiceWithInvalidMethod() public function testWrongLocationOverride() { - $this->client->withOverridenTarget('nonsense.not.existing'); + $this->client->withTarget('nonsense.not.existing'); $api = new Proxy($this->client); $promise = $api->getBank(array('blz' => '12070000')); @@ -75,8 +75,8 @@ public function testWrongLocationOverride() public function testCorrectLocationOverride() { - $this->client->withOverridenTarget('nonsense.not.existing'); - $this->client->withOverridenTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); + $this->client->withTarget('nonsense.not.existing'); + $this->client->withTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); $this->testBlzService(); } From d39e8ea951482e293bbd2e743aa411a2aeecc21b Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 17:33:56 +0200 Subject: [PATCH 06/11] Replaced one instance of shorthand array syntax use with a plain old array() call --- src/Protocol/ClientEncoder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index 728c878..fc736b6 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -73,7 +73,7 @@ public function getWsdlTarget() ); $this->findTarget = true; - $this->__soapCall($function, []); + $this->__soapCall($function, array()); return $this->target; } From cd0f0b97e5ac797bca0ba0dd950d432f678a1ef1 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 18:29:33 +0200 Subject: [PATCH 07/11] Made a copy of the location before assigning it because __doRequest was passed a reference --- src/Protocol/ClientEncoder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index fc736b6..43a8b97 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -28,7 +28,7 @@ public function encode($name, $args) public function __doRequest($request, $location, $action, $version, $one_way = 0) { if ($this->findTarget) { - $this->target = $location; + $this->target = (string) $location; $this->findTarget = false; } else { $finalLocation = $this->targetOverride !== null ? $this->targetOverride : $location; @@ -73,7 +73,7 @@ public function getWsdlTarget() ); $this->findTarget = true; - $this->__soapCall($function, array()); + $this->__soapCall($function, []); return $this->target; } From 2dfeb4c8fbd47c51ea44561a58f0a4a4958cce82 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Thu, 3 Sep 2015 18:32:49 +0200 Subject: [PATCH 08/11] Replaced one instance of shorthand array syntax use with a plain old array() call --- src/Protocol/ClientEncoder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index 43a8b97..c623708 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -73,7 +73,7 @@ public function getWsdlTarget() ); $this->findTarget = true; - $this->__soapCall($function, []); + $this->__soapCall($function, array()); return $this->target; } From e82a1cb977b4ecd180f84be9d6425a7180907f22 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Fri, 4 Sep 2015 17:29:56 +0200 Subject: [PATCH 09/11] Overwriting the target of the copy instead of the original ClientEncoder --- src/Protocol/ClientEncoder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index c623708..c041b45 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -52,7 +52,7 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 public function withTarget($newTarget) { $copy = clone $this; - $this->targetOverride = $newTarget; + $copy->targetOverride = $newTarget; return $copy; } From cbb9743387c878227cf5689ccc07137fe565ccd2 Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Sat, 19 Sep 2015 12:42:04 +0200 Subject: [PATCH 10/11] Removed features that don't belong in that pull request --- README.md | 4 --- src/Client.php | 7 +---- src/Protocol/ClientEncoder.php | 53 ++++++++-------------------------- tests/FunctionalTest.php | 11 +------ 4 files changed, 14 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index e15e04e..d142193 100644 --- a/README.md +++ b/README.md @@ -122,10 +122,6 @@ It returns the equivalent of PHP's [`SoapClient::__getTypes()`](http://php.net/m This method allows you to change the destination of your SOAP calls. It does not change the Client object, but returns a new Client with the overriden target. -#### getWsdlTarget() - -This method allows you to retrieve the target URL specified in the WSDL file. - ### Proxy The `Proxy` class wraps an existing [`Client`](#client) instance in order to ease calling diff --git a/src/Client.php b/src/Client.php index 150bac5..9e7dcd6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -51,7 +51,7 @@ public function soapCall($name, $args) public function handleResponse(Response $response) { - return $this->decoder->decode((string)$response->getBody()); + return $this->decoder->decode((string) $response->getBody()); } public function handleError(Exception $error) @@ -75,9 +75,4 @@ public function withTarget($target) $copy->encoder = $this->encoder->withTarget($target); return $copy; } - - public function getWsdlTarget() - { - return $this->encoder->getWsdlTarget(); - } } diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index c041b45..d1fbb11 100644 --- a/src/Protocol/ClientEncoder.php +++ b/src/Protocol/ClientEncoder.php @@ -12,8 +12,6 @@ class ClientEncoder extends SoapClient { private $request = null; private $targetOverride = null; - private $target = null; - private $findTarget = false; public function encode($name, $args) { @@ -27,23 +25,18 @@ public function encode($name, $args) public function __doRequest($request, $location, $action, $version, $one_way = 0) { - if ($this->findTarget) { - $this->target = (string) $location; - $this->findTarget = false; - } else { - $finalLocation = $this->targetOverride !== null ? $this->targetOverride : $location; - - $this->request = new Request( - 'POST', - (string) $finalLocation, - new Headers(array( - 'SOAPAction' => (string) $action, - 'Content-Type' => 'text/xml; charset=utf-8', - 'Content-Length' => strlen($request) - )), - new Body((string) $request) - ); - } + $finalLocation = $this->targetOverride !== null ? $this->targetOverride : $location; + + $this->request = new Request( + 'POST', + (string) $finalLocation, + new Headers(array( + 'SOAPAction' => (string) $action, + 'Content-Type' => 'text/xml; charset=utf-8', + 'Content-Length' => strlen($request) + )), + new Body((string) $request) + ); // do not actually block here, just pretend we're done... return ''; @@ -55,26 +48,4 @@ public function withTarget($newTarget) $copy->targetOverride = $newTarget; return $copy; } - - public function getWsdlTarget() - { - /* - * We can't just use a function with an empty name. - * SoapClient complains if the request does not exist. - */ - $functionDescriptions = $this->__getFunctions(); - $functionDescription = $functionDescriptions[0]; /* PHP 5.3 support. */ - $spaceIndex = strpos($functionDescription, ' '); - $openingParenIndex = strpos($functionDescription, '('); - $function = substr( - $functionDescription, - $spaceIndex + 1, - $openingParenIndex - $spaceIndex - 1 - ); - - $this->findTarget = true; - $this->__soapCall($function, array()); - return $this->target; - } - } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 0c790ca..5ae10d3 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -62,8 +62,7 @@ public function testBlzServiceWithInvalidMethod() public function testWrongLocationOverride() { - $this->client->withTarget('nonsense.not.existing'); - $api = new Proxy($this->client); + $api = new Proxy($this->client->withTarget('nonsense.not.existing')); $promise = $api->getBank(array('blz' => '12070000')); @@ -79,12 +78,4 @@ public function testCorrectLocationOverride() $this->client->withTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); $this->testBlzService(); } - - public function testGetLocation() - { - $this->assertEquals( - $this->client->getWsdlTarget(), - 'http://www.thomas-bayer.com/axis2/services/BLZService' - ); - } } From bf6966e138b0e460b8fa0a83acc8f45241490bdc Mon Sep 17 00:00:00 2001 From: Florian Simon Date: Wed, 16 Mar 2016 08:34:51 +0100 Subject: [PATCH 11/11] Fixes tests --- tests/FunctionalTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index c05b9b6..9125ecd 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -104,7 +104,7 @@ public function testWrongLocationOverride() $this->expectPromiseReject($promise); $this->setExpectedException('Exception'); - $this->waitForPromise($promise, $this->loop); + Block\await($promise, $this->loop); } public function testCorrectLocationOverride()