Skip to content

Commit

Permalink
Add ability to specify additional parameters for searchOwned
Browse files Browse the repository at this point in the history
There is a white list of allowed parameters. Any other parameters
will throw an exception

Resolves #87
  • Loading branch information
mheap committed Nov 12, 2017
1 parent 377175b commit c9554dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Numbers/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,34 @@ public function searchAvailable($country, $options = [])
return $this->handleNumberSearchResult($response, null);
}

public function searchOwned($number = null)
public function searchOwned($number = null, $options = [])
{
$queryString = '';
$query = [];
if ($number !== null) {
if($number instanceof Number){
$query = ['pattern' => $number->getId()];
} else {
$query = ['pattern' => $number];
}

$queryString = http_build_query($query);
}

// These are all optional parameters
$possibleParameters = [
'search_pattern',
'size',
'index'
];

foreach ($options as $param => $value) {
if (!in_array($param, $possibleParameters)) {
throw new Exception\Request("Unknown option: '".$param."'");
}
$query[$param] = $value;
}

$queryString = http_build_query($query);

$request = new Request(
\Nexmo\Client::BASE_REST . '/account/numbers?' . $queryString,
'GET',
Expand Down
45 changes: 45 additions & 0 deletions test/Numbers/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,51 @@ public function testSearchAvailableReturnsNumberList()
$this->assertSame('14155550101', $numbers[1]->getId());
}

public function testSearchOwnedErrorsOnUnknownSearchParameters()
{

$this->expectException(Exception\Request::class);
$this->expectExceptionMessage("Unknown option: 'foo'");

$this->numberClient->searchOwned('1415550100', [
'foo' => 'bar',
]);
}

public function testSearchOwnedPassesInAllowedAdditionalParameters()
{
$this->nexmoClient->send(Argument::that(function(RequestInterface $request){
$this->assertEquals('/account/numbers', $request->getUri()->getPath());
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('pattern=1415550100&index=1&size=100&search_pattern=0', $request->getUri()->getQuery());
return true;
}))->willReturn($this->getResponse('single'));

$this->numberClient->searchOwned('1415550100', [
'index' => 1,
'size' => '100',
'search_pattern' => 0
]);
}

public function testSearchOwnedReturnsSingleNumber()
{
$this->nexmoClient->send(Argument::that(function(RequestInterface $request){
$this->assertEquals('/account/numbers', $request->getUri()->getPath());
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
$this->assertEquals('GET', $request->getMethod());
return true;
}))->willReturn($this->getResponse('single'));

$numbers = $this->numberClient->searchOwned('1415550100');

$this->assertInternalType('array', $numbers);
$this->assertInstanceOf('Nexmo\Numbers\Number', $numbers[0]);

$this->assertSame('1415550100', $numbers[0]->getId());
}

public function testPurchaseNumberWithNumberObject()
{
$this->nexmoClient->send(Argument::that(function(RequestInterface $request){
Expand Down

0 comments on commit c9554dd

Please sign in to comment.