Skip to content

Commit 8cc9df0

Browse files
authored
Fix insight to return data when it is chargeable (#466)
1 parent 2b0306e commit 8cc9df0

File tree

7 files changed

+200
-14
lines changed

7 files changed

+200
-14
lines changed

src/Insights/Client.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414
use Psr\Http\Client\ClientExceptionInterface;
1515
use Vonage\Client\APIClient;
1616
use Vonage\Client\APIResource;
17-
use Vonage\Client\ClientAwareInterface;
18-
use Vonage\Client\ClientAwareTrait;
1917
use Vonage\Client\Exception as ClientException;
18+
use Vonage\Client\Exception\Exception;
19+
use Vonage\Client\Exception\Request;
20+
use Vonage\Client\Exception\Server;
2021
use Vonage\Entity\Filter\KeyValueFilter;
22+
use Vonage\Entity\IterableAPICollection;
2123
use Vonage\Numbers\Number;
2224

23-
use function is_null;
24-
2525
/**
2626
* Class Client
2727
*/
2828
class Client implements APIClient
2929
{
30+
protected array $chargeableCodes = [0, 43, 44, 45];
31+
3032
public function __construct(protected ?APIResource $api = null)
3133
{
3234
}
@@ -39,10 +41,11 @@ public function getApiResource(): APIResource
3941
/**
4042
* @param $number
4143
*
44+
* @return Basic
4245
* @throws ClientExceptionInterface
43-
* @throws ClientException\Exception
44-
* @throws ClientException\Request
45-
* @throws ClientException\Server
46+
* @throws Exception
47+
* @throws Request
48+
* @throws Server
4649
*/
4750
public function basic($number): Basic
4851
{
@@ -56,10 +59,11 @@ public function basic($number): Basic
5659
/**
5760
* @param $number
5861
*
62+
* @return StandardCnam
5963
* @throws ClientExceptionInterface
60-
* @throws ClientException\Exception
61-
* @throws ClientException\Request
62-
* @throws ClientException\Server
64+
* @throws Exception
65+
* @throws Request
66+
* @throws Server
6367
*/
6468
public function standardCNam($number): StandardCnam
6569
{
@@ -72,10 +76,11 @@ public function standardCNam($number): StandardCnam
7276
/**
7377
* @param $number
7478
*
79+
* @return AdvancedCnam
7580
* @throws ClientExceptionInterface
76-
* @throws ClientException\Exception
77-
* @throws ClientException\Request
78-
* @throws ClientException\Server
81+
* @throws Exception
82+
* @throws Request
83+
* @throws Server
7984
*/
8085
public function advancedCnam($number): AdvancedCnam
8186
{
@@ -146,6 +151,9 @@ public function makeRequest(string $path, $number, array $additionalParams = [])
146151
{
147152
$api = $this->getApiResource();
148153
$api->setBaseUri($path);
154+
$collectionPrototype = new IterableAPICollection();
155+
$collectionPrototype->setHasPagination(false);
156+
$api->setCollectionPrototype($collectionPrototype);
149157

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

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

test/Insights/AdvancedTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,41 @@
1111

1212
namespace VonageTest\Insights;
1313

14+
use Laminas\Diactoros\Response;
15+
use Prophecy\Argument;
16+
use Prophecy\Prophecy\ObjectProphecy;
17+
use Vonage\Client;
18+
use Vonage\Client\APIResource;
19+
use Vonage\Client\Credentials\Handler\BasicQueryHandler;
20+
use Vonage\Client\Exception\Request;
1421
use VonageTest\VonageTestCase;
1522
use Vonage\Insights\Advanced;
23+
use Vonage\Insights\Client as InsightClient;
1624

1725
class AdvancedTest extends VonageTestCase
1826
{
27+
public InsightClient $insightClient;
28+
public Client|ObjectProphecy $vonageClient;
29+
public APIResource $api;
30+
31+
public function setUp(): void
32+
{
33+
$this->vonageClient = $this->prophesize(Client::class);
34+
$this->vonageClient->getRestUrl()->willReturn('https://api.nexmo.com');
35+
$this->vonageClient->getCredentials()->willReturn(
36+
new Client\Credentials\Container(
37+
new Client\Credentials\Basic('abc', 'def'),
38+
)
39+
);
40+
41+
$this->api = (new APIResource())
42+
->setClient($this->vonageClient->reveal())
43+
->setIsHAL(false)
44+
->setAuthHandler(new BasicQueryHandler())
45+
->setBaseUrl('https://api.nexmo.com/ni/advanced');
46+
47+
$this->insightClient = new InsightClient($this->api);
48+
}
1949
/**
2050
* @dataProvider advancedTestProvider
2151
*
@@ -28,6 +58,42 @@ public function testObjectAccess($advanced, $inputData): void
2858
$this->assertEquals($inputData['reachable'], $advanced->getReachable());
2959
}
3060

61+
/**
62+
* @dataProvider advancedExceptionResponseProvider
63+
* @param $responseName
64+
* @param $expectException
65+
*
66+
* @return void
67+
*/
68+
public function testExceptionWhenNotChargeable($responseName, $expectException): void
69+
{
70+
if ($expectException) {
71+
$this->expectException(Request::class);
72+
}
73+
74+
$this->vonageClient->send(Argument::that(function (\Laminas\Diactoros\Request $request) use ($responseName) {
75+
$uri = $request->getUri();
76+
$uriString = $uri->__toString();
77+
$this->assertEquals('https://api.nexmo.com/ni/advanced/ni/advanced/json?number=12345&api_key=abc&api_secret=def', $uriString);
78+
return true;
79+
}))->willReturn($this->getResponse($responseName, 200));
80+
81+
$response = $this->insightClient->advanced('12345');
82+
$this->assertInstanceOf(Advanced::class, $response);
83+
}
84+
85+
public function advancedExceptionResponseProvider(): array
86+
{
87+
return [
88+
['advanced', false],
89+
['advanced3', true],
90+
['advanced4', true],
91+
['advanced43', false],
92+
['advanced44', false],
93+
['advanced45', false]
94+
];
95+
}
96+
3197
public function advancedTestProvider(): array
3298
{
3399
$r = [];
@@ -43,4 +109,12 @@ public function advancedTestProvider(): array
43109

44110
return $r;
45111
}
112+
113+
/**
114+
* This method gets the fixtures and wraps them in a Response object to mock the API
115+
*/
116+
protected function getResponse(string $identifier, int $status = 200): Response
117+
{
118+
return new Response(fopen(__DIR__ . '/responses/' . $identifier . '.json', 'rb'), $status);
119+
}
46120
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"status": 3,
3+
"status_message": "Your request is incomplete and missing some mandatory parameters"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"status": 4,
3+
"status_message": "Invalid credentials"
4+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"status": 43,
3+
"status_message": "Lookup Handler unable to handle request",
4+
"lookup_outcome": 1,
5+
"lookup_outcome_message": "Partial success - some fields populated",
6+
"request_id": "XXc89636-3714-42be-a731-9d8f52d7baXX",
7+
"international_format_number": "18104967XXX",
8+
"national_format_number": "(XXX) 496-7360",
9+
"country_code": "US",
10+
"country_code_iso3": "USA",
11+
"country_name": "United States of America",
12+
"country_prefix": "1",
13+
"request_price": "0.03000000",
14+
"remaining_balance": "98.24739334",
15+
"current_carrier": {
16+
"network_code": "US-FIXED",
17+
"name": "United States of America Landline",
18+
"country": "US",
19+
"network_type": "landline"
20+
},
21+
"original_carrier": {
22+
"network_code": "US-FIXED",
23+
"name": "United States of America Landline",
24+
"country": "US",
25+
"network_type": "landline"
26+
},
27+
"valid_number": "valid",
28+
"reachable": "unknown",
29+
"ported": "unknown",
30+
"roaming": "unknown",
31+
"ip_warnings": "unknown"
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"status": 43,
3+
"status_message": "Lookup Handler unable to handle request",
4+
"lookup_outcome": 1,
5+
"lookup_outcome_message": "Partial success - some fields populated",
6+
"request_id": "XXc89636-3714-42be-a731-9d8f52d7baXX",
7+
"international_format_number": "18104967XXX",
8+
"national_format_number": "(XXX) 496-7360",
9+
"country_code": "US",
10+
"country_code_iso3": "USA",
11+
"country_name": "United States of America",
12+
"country_prefix": "1",
13+
"request_price": "0.03000000",
14+
"remaining_balance": "98.24739334",
15+
"current_carrier": {
16+
"network_code": "US-FIXED",
17+
"name": "United States of America Landline",
18+
"country": "US",
19+
"network_type": "landline"
20+
},
21+
"original_carrier": {
22+
"network_code": "US-FIXED",
23+
"name": "United States of America Landline",
24+
"country": "US",
25+
"network_type": "landline"
26+
},
27+
"valid_number": "valid",
28+
"reachable": "unknown",
29+
"ported": "unknown",
30+
"roaming": "unknown",
31+
"ip_warnings": "unknown"
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"status": 43,
3+
"status_message": "Lookup Handler unable to handle request",
4+
"lookup_outcome": 1,
5+
"lookup_outcome_message": "Partial success - some fields populated",
6+
"request_id": "XXc89636-3714-42be-a731-9d8f52d7baXX",
7+
"international_format_number": "18104967XXX",
8+
"national_format_number": "(XXX) 496-7360",
9+
"country_code": "US",
10+
"country_code_iso3": "USA",
11+
"country_name": "United States of America",
12+
"country_prefix": "1",
13+
"request_price": "0.03000000",
14+
"remaining_balance": "98.24739334",
15+
"current_carrier": {
16+
"network_code": "US-FIXED",
17+
"name": "United States of America Landline",
18+
"country": "US",
19+
"network_type": "landline"
20+
},
21+
"original_carrier": {
22+
"network_code": "US-FIXED",
23+
"name": "United States of America Landline",
24+
"country": "US",
25+
"network_type": "landline"
26+
},
27+
"valid_number": "valid",
28+
"reachable": "unknown",
29+
"ported": "unknown",
30+
"roaming": "unknown",
31+
"ip_warnings": "unknown"
32+
}

0 commit comments

Comments
 (0)