Skip to content

Commit 6f6bb09

Browse files
authored
Feature/verify v2 byop (#400)
* Added BYOP with tests * Move BYOP to outside workflow scope, remove client ref from constructor
1 parent b9d3b25 commit 6f6bb09

File tree

9 files changed

+91
-30
lines changed

9 files changed

+91
-30
lines changed

src/Verify2/Request/BaseVerifyRequest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ abstract class BaseVerifyRequest implements RequestInterface
2424

2525
protected array $workflows = [];
2626

27+
protected ?string $code = null;
28+
2729
public function getLocale(): ?VerificationLocale
2830
{
2931
return $this->locale;
@@ -57,6 +59,18 @@ public function setTimeout(int $timeout): static
5759
return $this;
5860
}
5961

62+
public function getCode(): ?string
63+
{
64+
return $this->code;
65+
}
66+
67+
public function setCode(string $code): static
68+
{
69+
$this->code = $code;
70+
71+
return $this;
72+
}
73+
6074
public function getClientRef(): ?string
6175
{
6276
return $this->clientRef;
@@ -106,7 +120,7 @@ public function setBrand(string $brand): static
106120

107121
public function getWorkflows(): array
108122
{
109-
return array_map(static function($workflow) {
123+
return array_map(static function ($workflow) {
110124
return $workflow->toArray();
111125
}, $this->workflows);
112126
}
@@ -132,6 +146,10 @@ public function getBaseVerifyUniversalOutputArray(): array
132146
$returnArray['client_ref'] = $this->getClientRef();
133147
}
134148

149+
if ($this->getCode()) {
150+
$returnArray['code'] = $this->getCode();
151+
}
152+
135153
return $returnArray;
136154
}
137-
}
155+
}

src/Verify2/Request/EmailRequest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ public function __construct(
1111
protected string $to,
1212
protected string $brand,
1313
protected string $from,
14-
protected ?string $clientRef = null,
1514
protected ?VerificationLocale $locale = null,
1615
) {
1716
if (!$this->locale) {
1817
$this->locale = new VerificationLocale();
1918
}
2019

20+
if ($this->code) {
21+
$this->setCode($this->code);
22+
}
23+
2124
$workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_EMAIL, $to, $from);
25+
2226
$this->addWorkflow($workflow);
2327
}
2428

2529
public function toArray(): array
2630
{
2731
return $this->getBaseVerifyUniversalOutputArray();
2832
}
29-
}
33+
}

src/Verify2/Request/RequestInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ public function getLength(): int;
2020
public function getBrand(): string;
2121
public function getWorkflows(): array;
2222
public function getBaseVerifyUniversalOutputArray(): array;
23+
public function setCode(string $code): static;
24+
public function getCode(): ?string;
2325
}

src/Verify2/Request/SMSRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ class SMSRequest extends BaseVerifyRequest
1010
public function __construct(
1111
protected string $to,
1212
protected string $brand,
13-
protected ?string $clientRef = null,
14-
protected ?VerificationLocale $locale = null
13+
protected ?VerificationLocale $locale = null,
1514
) {
1615
if (!$this->locale) {
1716
$this->locale = new VerificationLocale();
1817
}
1918

2019
$workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_SMS, $to);
20+
2121
$this->addWorkflow($workflow);
2222
}
2323

2424
public function toArray(): array
2525
{
2626
return $this->getBaseVerifyUniversalOutputArray();
2727
}
28-
}
28+
}

src/Verify2/Request/VoiceRequest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ class VoiceRequest extends BaseVerifyRequest
1010
public function __construct(
1111
protected string $to,
1212
protected string $brand,
13-
protected ?string $clientRef,
14-
protected ?VerificationLocale $locale = null
13+
protected ?VerificationLocale $locale = null,
1514
) {
1615
if (!$this->locale) {
1716
$this->locale = new VerificationLocale();
1817
}
1918

2019
$workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_VOICE, $to);
20+
21+
if ($this->code) {
22+
$workflow->setCode($this->code);
23+
}
24+
2125
$this->addWorkflow($workflow);
2226
}
2327

src/Verify2/Request/WhatsAppInteractiveRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class WhatsAppInteractiveRequest extends BaseVerifyRequest
1010
public function __construct(
1111
protected string $to,
1212
protected string $brand,
13-
protected ?string $clientRef,
1413
protected ?VerificationLocale $locale = null
1514
) {
1615
if (!$this->locale) {

src/Verify2/Request/WhatsAppRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ public function __construct(
1111
protected string $to,
1212
protected string $brand,
1313
protected ?string $from = null,
14-
protected ?string $clientRef = null,
15-
protected ?VerificationLocale $locale = null
14+
protected ?VerificationLocale $locale = null,
1615
) {
1716
if (!$this->locale) {
1817
$this->locale = new VerificationLocale();
1918
}
2019

2120
$workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_WHATSAPP, $to);
21+
2222
$this->addWorkflow($workflow);
2323
}
2424

2525
public function toArray(): array
2626
{
2727
return $this->getBaseVerifyUniversalOutputArray();
2828
}
29-
}
29+
}

src/Verify2/VerifyObjects/VerificationWorkflow.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ class VerificationWorkflow implements ArrayHydrateInterface
2222
self::WORKFLOW_SILENT_AUTH
2323
];
2424

25-
public function __construct(protected string $channel, protected string $to, protected string $from = '')
26-
{
25+
public function __construct(
26+
protected string $channel,
27+
protected string $to,
28+
protected string $from = ''
29+
) {
2730
if (! in_array($channel, $this->allowedWorkflows, true)) {
2831
throw new \InvalidArgumentException($this->channel . ' is not a valid workflow');
2932
}
@@ -89,6 +92,5 @@ public function toArray(): array
8992
}
9093

9194
return $returnArray;
92-
9395
}
94-
}
96+
}

test/Verify2/ClientTest.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public function testSetsRequestAuthCorrectly(): void
6464
'brand' => 'my-brand',
6565
];
6666

67-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']);
67+
$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
68+
$smsVerification->setClientRef($payload['client_ref']);
6869

6970
$this->vonageClient->send(Argument::that(function (Request $request) {
7071
$this->assertEquals(
@@ -85,7 +86,8 @@ public function testCanRequestSMS(): void
8586
'brand' => 'my-brand',
8687
];
8788

88-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']);
89+
$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
90+
$smsVerification->setClientRef($payload['client_ref']);
8991

9092
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
9193
$uri = $request->getUri();
@@ -131,7 +133,8 @@ public function testCannotRequestSMSWithInvalidLocale($locale, $valid): void
131133
'locale' => $verificationLocale,
132134
];
133135

134-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref'], $payload['locale']);
136+
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['locale']);
137+
$smsVerification->setClientRef($payload['client_ref']);
135138

136139
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
137140
$this->assertEquals(
@@ -167,7 +170,8 @@ public function testTimeoutParsesCorrectly($timeout, $valid): void
167170
'timeout' => $timeout
168171
];
169172

170-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']);
173+
$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
174+
$smsVerification->setClientRef($payload['client_ref']);
171175
$smsVerification->setTimeout($timeout);
172176

173177
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
@@ -203,7 +207,8 @@ public function testCannotRequestSMSWithInvalidCodeLength($length, $valid): void
203207
'length' => $length
204208
];
205209

206-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']);
210+
$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
211+
$smsVerification->setClientRef($payload['client_ref']);
207212
$smsVerification->setLength($payload['length']);
208213

209214
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
@@ -225,7 +230,8 @@ public function testCanRequestWhatsApp(): void
225230
'brand' => 'my-brand',
226231
];
227232

228-
$whatsAppVerification = new WhatsAppRequest($payload['to'], $payload['brand'],null, $payload['client_ref']);
233+
$whatsAppVerification = new WhatsAppRequest($payload['to'], $payload['brand']);
234+
$whatsAppVerification->setClientRef($payload['client_ref']);
229235

230236
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
231237
$this->assertRequestJsonBodyContains('locale', 'en-us', $request);
@@ -254,7 +260,8 @@ public function testCanRequestWhatsAppInteractive(): void
254260
'brand' => 'my-brand',
255261
];
256262

257-
$whatsAppInteractiveRequest = new WhatsAppInteractiveRequest($payload['to'], $payload['brand'], $payload['client_ref']);
263+
$whatsAppInteractiveRequest = new WhatsAppInteractiveRequest($payload['to'], $payload['brand']);
264+
$whatsAppInteractiveRequest->setClientRef($payload['client_ref']);
258265

259266
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
260267
$this->assertRequestJsonBodyContains('locale', 'en-us', $request);
@@ -282,7 +289,8 @@ public function testCanRequestVoice(): void
282289
'brand' => 'my-brand',
283290
];
284291

285-
$voiceRequest = new VoiceRequest($payload['to'], $payload['brand'], $payload['client_ref']);
292+
$voiceRequest = new VoiceRequest($payload['to'], $payload['brand']);
293+
$voiceRequest->setClientRef($payload['client_ref']);
286294

287295
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
288296
$this->assertRequestJsonBodyContains('locale', 'en-us', $request);
@@ -311,7 +319,8 @@ public function testCanRequestEmail(): void
311319
'brand' => 'my-brand',
312320
];
313321

314-
$emailRequest = new EmailRequest($payload['to'], $payload['brand'], $payload['from'], $payload['client_ref']);
322+
$emailRequest = new EmailRequest($payload['to'], $payload['brand'], $payload['from']);
323+
$emailRequest->setClientRef($payload['client_ref']);
315324

316325
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
317326
$this->assertRequestJsonBodyContains('locale', 'en-us', $request);
@@ -332,6 +341,25 @@ public function testCanRequestEmail(): void
332341
$this->assertArrayHasKey('request_id', $result);
333342
}
334343

344+
public function testCanRenderOptionalCode(): void
345+
{
346+
$payload = [
347+
'to' => '07785648870',
348+
'brand' => 'my-brand',
349+
];
350+
351+
$smsRequest = new SMSRequest($payload['to'], $payload['brand']);
352+
$smsRequest->setCode('123456789');
353+
354+
$this->vonageClient->send(Argument::that(function (Request $request) {
355+
$this->assertRequestJsonBodyContains('code', '123456789', $request, true);
356+
357+
return true;
358+
}))->willReturn($this->getResponse('verify-request-success', 202));
359+
360+
$result = $this->verify2Client->startVerification($smsRequest);
361+
}
362+
335363
public function testCanHandleMultipleWorkflows(): void
336364
{
337365
$payload = [
@@ -340,7 +368,8 @@ public function testCanHandleMultipleWorkflows(): void
340368
'brand' => 'my-brand',
341369
];
342370

343-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']);
371+
$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
372+
$smsVerification->setClientRef($payload['client_ref']);
344373
$voiceWorkflow = new VerificationWorkflow('voice', '07785254785');
345374
$smsVerification->addWorkflow($voiceWorkflow);
346375

@@ -389,7 +418,8 @@ public function testCannotSendConcurrentVerifications(): void
389418
'brand' => 'my-brand',
390419
];
391420

392-
$smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']);
421+
$smsVerification = new SMSRequest($payload['to'], $payload['brand']);
422+
$smsVerification->setClientRef( $payload['client_ref']);
393423

394424
$this->vonageClient->send(Argument::that(function (Request $request) {
395425
$this->assertEquals('POST', $request->getMethod());
@@ -418,7 +448,8 @@ public function testCannotSendWithoutBrand(): void
418448
'brand' => '',
419449
];
420450

421-
$voiceRequest = new VoiceRequest($payload['to'], $payload['brand'], $payload['client_ref']);
451+
$voiceRequest = new VoiceRequest($payload['to'], $payload['brand']);
452+
$voiceRequest->setClientRef($payload['client_ref']);
422453

423454
$this->vonageClient->send(Argument::that(function (Request $request) {
424455
$this->assertEquals('POST', $request->getMethod());
@@ -439,7 +470,8 @@ public function testCanHandleThrottle(): void
439470
'brand' => 'my-brand',
440471
];
441472

442-
$voiceRequest = new VoiceRequest($payload['to'], $payload['brand'], $payload['client_ref']);
473+
$voiceRequest = new VoiceRequest($payload['to'], $payload['brand']);
474+
$voiceRequest->setClientRef($payload['client_ref']);
443475

444476
$this->vonageClient->send(Argument::that(function (Request $request) {
445477
$this->assertEquals('POST', $request->getMethod());

0 commit comments

Comments
 (0)