Skip to content

Commit

Permalink
Fix insight to return data when it is chargeable (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK authored Feb 6, 2024
1 parent 2b0306e commit 8cc9df0
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/Insights/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
use Psr\Http\Client\ClientExceptionInterface;
use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Client\ClientAwareInterface;
use Vonage\Client\ClientAwareTrait;
use Vonage\Client\Exception as ClientException;
use Vonage\Client\Exception\Exception;
use Vonage\Client\Exception\Request;
use Vonage\Client\Exception\Server;
use Vonage\Entity\Filter\KeyValueFilter;
use Vonage\Entity\IterableAPICollection;
use Vonage\Numbers\Number;

use function is_null;

/**
* Class Client
*/
class Client implements APIClient
{
protected array $chargeableCodes = [0, 43, 44, 45];

public function __construct(protected ?APIResource $api = null)
{
}
Expand All @@ -39,10 +41,11 @@ public function getApiResource(): APIResource
/**
* @param $number
*
* @return Basic
* @throws ClientExceptionInterface
* @throws ClientException\Exception
* @throws ClientException\Request
* @throws ClientException\Server
* @throws Exception
* @throws Request
* @throws Server
*/
public function basic($number): Basic
{
Expand All @@ -56,10 +59,11 @@ public function basic($number): Basic
/**
* @param $number
*
* @return StandardCnam
* @throws ClientExceptionInterface
* @throws ClientException\Exception
* @throws ClientException\Request
* @throws ClientException\Server
* @throws Exception
* @throws Request
* @throws Server
*/
public function standardCNam($number): StandardCnam
{
Expand All @@ -72,10 +76,11 @@ public function standardCNam($number): StandardCnam
/**
* @param $number
*
* @return AdvancedCnam
* @throws ClientExceptionInterface
* @throws ClientException\Exception
* @throws ClientException\Request
* @throws ClientException\Server
* @throws Exception
* @throws Request
* @throws Server
*/
public function advancedCnam($number): AdvancedCnam
{
Expand Down Expand Up @@ -146,6 +151,9 @@ public function makeRequest(string $path, $number, array $additionalParams = [])
{
$api = $this->getApiResource();
$api->setBaseUri($path);
$collectionPrototype = new IterableAPICollection();
$collectionPrototype->setHasPagination(false);
$api->setCollectionPrototype($collectionPrototype);

if ($number instanceof Number) {
$number = $number->getMsisdn();
Expand All @@ -156,7 +164,7 @@ public function makeRequest(string $path, $number, array $additionalParams = [])
$data = $result->getPageData();

// check the status field in response (HTTP status is 200 even for errors)
if ((int)$data['status'] !== 0) {
if (! in_array((int)$data['status'], $this->chargeableCodes, true)) {
throw $this->getNIException($data);
}

Expand Down
74 changes: 74 additions & 0 deletions test/Insights/AdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,41 @@

namespace VonageTest\Insights;

use Laminas\Diactoros\Response;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Vonage\Client;
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\BasicQueryHandler;
use Vonage\Client\Exception\Request;
use VonageTest\VonageTestCase;
use Vonage\Insights\Advanced;
use Vonage\Insights\Client as InsightClient;

class AdvancedTest extends VonageTestCase
{
public InsightClient $insightClient;
public Client|ObjectProphecy $vonageClient;
public APIResource $api;

public function setUp(): void
{
$this->vonageClient = $this->prophesize(Client::class);
$this->vonageClient->getRestUrl()->willReturn('https://api.nexmo.com');
$this->vonageClient->getCredentials()->willReturn(
new Client\Credentials\Container(
new Client\Credentials\Basic('abc', 'def'),
)
);

$this->api = (new APIResource())
->setClient($this->vonageClient->reveal())
->setIsHAL(false)
->setAuthHandler(new BasicQueryHandler())
->setBaseUrl('https://api.nexmo.com/ni/advanced');

$this->insightClient = new InsightClient($this->api);
}
/**
* @dataProvider advancedTestProvider
*
Expand All @@ -28,6 +58,42 @@ public function testObjectAccess($advanced, $inputData): void
$this->assertEquals($inputData['reachable'], $advanced->getReachable());
}

/**
* @dataProvider advancedExceptionResponseProvider
* @param $responseName
* @param $expectException
*
* @return void
*/
public function testExceptionWhenNotChargeable($responseName, $expectException): void
{
if ($expectException) {
$this->expectException(Request::class);
}

$this->vonageClient->send(Argument::that(function (\Laminas\Diactoros\Request $request) use ($responseName) {
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals('https://api.nexmo.com/ni/advanced/ni/advanced/json?number=12345&api_key=abc&api_secret=def', $uriString);
return true;
}))->willReturn($this->getResponse($responseName, 200));

$response = $this->insightClient->advanced('12345');
$this->assertInstanceOf(Advanced::class, $response);
}

public function advancedExceptionResponseProvider(): array
{
return [
['advanced', false],
['advanced3', true],
['advanced4', true],
['advanced43', false],
['advanced44', false],
['advanced45', false]
];
}

public function advancedTestProvider(): array
{
$r = [];
Expand All @@ -43,4 +109,12 @@ public function advancedTestProvider(): array

return $r;
}

/**
* This method gets the fixtures and wraps them in a Response object to mock the API
*/
protected function getResponse(string $identifier, int $status = 200): Response
{
return new Response(fopen(__DIR__ . '/responses/' . $identifier . '.json', 'rb'), $status);
}
}
4 changes: 4 additions & 0 deletions test/Insights/responses/advanced3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": 3,
"status_message": "Your request is incomplete and missing some mandatory parameters"
}
4 changes: 4 additions & 0 deletions test/Insights/responses/advanced4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": 4,
"status_message": "Invalid credentials"
}
32 changes: 32 additions & 0 deletions test/Insights/responses/advanced43.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"status": 43,
"status_message": "Lookup Handler unable to handle request",
"lookup_outcome": 1,
"lookup_outcome_message": "Partial success - some fields populated",
"request_id": "XXc89636-3714-42be-a731-9d8f52d7baXX",
"international_format_number": "18104967XXX",
"national_format_number": "(XXX) 496-7360",
"country_code": "US",
"country_code_iso3": "USA",
"country_name": "United States of America",
"country_prefix": "1",
"request_price": "0.03000000",
"remaining_balance": "98.24739334",
"current_carrier": {
"network_code": "US-FIXED",
"name": "United States of America Landline",
"country": "US",
"network_type": "landline"
},
"original_carrier": {
"network_code": "US-FIXED",
"name": "United States of America Landline",
"country": "US",
"network_type": "landline"
},
"valid_number": "valid",
"reachable": "unknown",
"ported": "unknown",
"roaming": "unknown",
"ip_warnings": "unknown"
}
32 changes: 32 additions & 0 deletions test/Insights/responses/advanced44.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"status": 43,
"status_message": "Lookup Handler unable to handle request",
"lookup_outcome": 1,
"lookup_outcome_message": "Partial success - some fields populated",
"request_id": "XXc89636-3714-42be-a731-9d8f52d7baXX",
"international_format_number": "18104967XXX",
"national_format_number": "(XXX) 496-7360",
"country_code": "US",
"country_code_iso3": "USA",
"country_name": "United States of America",
"country_prefix": "1",
"request_price": "0.03000000",
"remaining_balance": "98.24739334",
"current_carrier": {
"network_code": "US-FIXED",
"name": "United States of America Landline",
"country": "US",
"network_type": "landline"
},
"original_carrier": {
"network_code": "US-FIXED",
"name": "United States of America Landline",
"country": "US",
"network_type": "landline"
},
"valid_number": "valid",
"reachable": "unknown",
"ported": "unknown",
"roaming": "unknown",
"ip_warnings": "unknown"
}
32 changes: 32 additions & 0 deletions test/Insights/responses/advanced45.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"status": 43,
"status_message": "Lookup Handler unable to handle request",
"lookup_outcome": 1,
"lookup_outcome_message": "Partial success - some fields populated",
"request_id": "XXc89636-3714-42be-a731-9d8f52d7baXX",
"international_format_number": "18104967XXX",
"national_format_number": "(XXX) 496-7360",
"country_code": "US",
"country_code_iso3": "USA",
"country_name": "United States of America",
"country_prefix": "1",
"request_price": "0.03000000",
"remaining_balance": "98.24739334",
"current_carrier": {
"network_code": "US-FIXED",
"name": "United States of America Landline",
"country": "US",
"network_type": "landline"
},
"original_carrier": {
"network_code": "US-FIXED",
"name": "United States of America Landline",
"country": "US",
"network_type": "landline"
},
"valid_number": "valid",
"reachable": "unknown",
"ported": "unknown",
"roaming": "unknown",
"ip_warnings": "unknown"
}

0 comments on commit 8cc9df0

Please sign in to comment.