Skip to content

Commit

Permalink
Merge pull request #38 from clue-labs/with-location
Browse files Browse the repository at this point in the history
Add new Client::withLocation() method
  • Loading branch information
clue authored Nov 6, 2018
2 parents c6495d3 + 29d70bd commit 0b66596
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This project provides a *simple* API for invoking *async* RPCs to remote web ser
* [getFunctions()](#getfunctions)
* [getTypes()](#gettypes)
* [getLocation()](#getlocation)
* [withLocation()](#withlocation)
* [Proxy](#proxy)
* [Functions](#functions)
* [Promises](#promises)
Expand Down Expand Up @@ -270,10 +271,29 @@ assert('http://example.com/soap/service' === $client->getLocation(0));
```
When the `location` option has been set in the `Client` constructor
(such as when in non-WSDL mode), this method returns the value of the
given `location` option.
(such as when in non-WSDL mode) or via the `withLocation()` method, this
method returns the value of the given location.
Passing a `$function` not defined in the WSDL file will throw a `SoapFault`.
Passing a `$function` not defined in the WSDL file will throw a `SoapFault`.
#### withLocation()
The `withLocation(string $location): self` method can be used to
return a new `Client` with the updated location (URI) for all functions.
Note that this is not to be confused with the WSDL file location.
A WSDL file can contain any number of function definitions.
It's very common that all of these functions use the same location definition.
However, technically each function can potentially use a different location.

```php
$client = $client->withLocation('http://example.com/soap');

assert('http://example.com/soap' === $client->getLocation('echo'));
```

As an alternative to this method, you can also set the `location` option
in the `Client` constructor (such as when in non-WSDL mode).

### Proxy

Expand Down
34 changes: 32 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public function getTypes()
* ```
*
* When the `location` option has been set in the `Client` constructor
* (such as when in non-WSDL mode), this method returns the value of the
* given `location` option.
* (such as when in non-WSDL mode) or via the `withLocation()` method, this
* method returns the value of the given location.
*
* Passing a `$function` not defined in the WSDL file will throw a `SoapFault`.
*
Expand All @@ -290,4 +290,34 @@ public function getLocation($function)
// encode request for given $function
return (string)$this->encoder->encode($function, array())->getUri();
}

/**
* Returns a new `Client` with the updated location (URI) for all functions.
*
* Note that this is not to be confused with the WSDL file location.
* A WSDL file can contain any number of function definitions.
* It's very common that all of these functions use the same location definition.
* However, technically each function can potentially use a different location.
*
* ```php
* $client = $client->withLocation('http://example.com/soap');
*
* assert('http://example.com/soap' === $client->getLocation('echo'));
* ```
*
* As an alternative to this method, you can also set the `location` option
* in the `Client` constructor (such as when in non-WSDL mode).
*
* @param string $location
* @return self
* @see self::getLocation()
*/
public function withLocation($location)
{
$client = clone $this;
$client->encoder = clone $this->encoder;
$client->encoder->__setLocation($location);

return $client;
}
}
34 changes: 34 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,38 @@ public function testGetLocationWithExplicitLocationOptionReturnsAsIs()

$this->assertEquals('http://example.com/', $this->client->getLocation(0));
}

public function testWithLocationReturnsUpdatedClient()
{
$original = $this->client->getLocation(0);
$client = $this->client->withLocation('http://nonsense.invalid');

$this->assertEquals('http://nonsense.invalid', $client->getLocation(0));
$this->assertEquals($original, $this->client->getLocation(0));
}

/**
* @expectedException RuntimeException
*/
public function testWithLocationInvalidRejectsWithRuntimeException()
{
$api = new Proxy($this->client->withLocation('http://nonsense.invalid'));

$promise = $api->getBank(array('blz' => '12070000'));

Block\await($promise, $this->loop);
}

public function testWithLocationRestoredToOriginalResolves()
{
$original = $this->client->getLocation(0);
$client = $this->client->withLocation('http://nonsense.invalid');
$client = $client->withLocation($original);
$api = new Proxy($client);

$promise = $api->getBank(array('blz' => '12070000'));

$result = Block\await($promise, $this->loop);
$this->assertInternalType('object', $result);
}
}

0 comments on commit 0b66596

Please sign in to comment.