diff --git a/README.md b/README.md index 9c8806f..449739b 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,15 @@ 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). +#### 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. + +#### getWsdlTarget() + +This method allows you to retrieve the target URL specified in the WSDL file. + #### getLocation() The `getLocation($function)` method can be used to return the location (URI) diff --git a/src/Client.php b/src/Client.php index 395005b..2b7b7c6 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) @@ -69,6 +69,18 @@ public function getTypes() return $this->encoder->__getTypes(); } + public function withTarget($target) + { + $copy = clone $this; + $copy->encoder = $this->encoder->withTarget($target); + return $copy; + } + + public function getWsdlTarget() + { + return $this->encoder->getWsdlTarget(); + } + /** * get location (URI) for given function name or number * diff --git a/src/Protocol/ClientEncoder.php b/src/Protocol/ClientEncoder.php index afd625a..d1fbb11 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 $targetOverride = 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->targetOverride !== null ? $this->targetOverride : $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 withTarget($newTarget) + { + $copy = clone $this; + $copy->targetOverride = $newTarget; + return $copy; + } } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 4a2c0b6..9125ecd 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -94,4 +94,23 @@ public function testGetLocationForUnknownFunctionNumberFails() { $this->assertEquals('http://www.thomas-bayer.com/axis2/services/BLZService', $this->client->getLocation(100)); } + + public function testWrongLocationOverride() + { + $api = new Proxy($this->client->withTarget('nonsense.not.existing')); + + $promise = $api->getBank(array('blz' => '12070000')); + + $this->expectPromiseReject($promise); + + $this->setExpectedException('Exception'); + Block\await($promise, $this->loop); + } + + public function testCorrectLocationOverride() + { + $this->client->withTarget('nonsense.not.existing'); + $this->client->withTarget('http://www.thomas-bayer.com/axis2/services/BLZService'); + $this->testBlzService(); + } }