From 6f6bb09b1d9c65abb42cca1e19ec71ff1ef3a347 Mon Sep 17 00:00:00 2001 From: James Seconde Date: Mon, 17 Apr 2023 15:26:58 +0100 Subject: [PATCH] Feature/verify v2 byop (#400) * Added BYOP with tests * Move BYOP to outside workflow scope, remove client ref from constructor --- src/Verify2/Request/BaseVerifyRequest.php | 22 ++++++- src/Verify2/Request/EmailRequest.php | 8 ++- src/Verify2/Request/RequestInterface.php | 2 + src/Verify2/Request/SMSRequest.php | 6 +- src/Verify2/Request/VoiceRequest.php | 8 ++- .../Request/WhatsAppInteractiveRequest.php | 1 - src/Verify2/Request/WhatsAppRequest.php | 6 +- .../VerifyObjects/VerificationWorkflow.php | 10 ++-- test/Verify2/ClientTest.php | 58 ++++++++++++++----- 9 files changed, 91 insertions(+), 30 deletions(-) diff --git a/src/Verify2/Request/BaseVerifyRequest.php b/src/Verify2/Request/BaseVerifyRequest.php index c8520cab..a07eb584 100644 --- a/src/Verify2/Request/BaseVerifyRequest.php +++ b/src/Verify2/Request/BaseVerifyRequest.php @@ -24,6 +24,8 @@ abstract class BaseVerifyRequest implements RequestInterface protected array $workflows = []; + protected ?string $code = null; + public function getLocale(): ?VerificationLocale { return $this->locale; @@ -57,6 +59,18 @@ public function setTimeout(int $timeout): static return $this; } + public function getCode(): ?string + { + return $this->code; + } + + public function setCode(string $code): static + { + $this->code = $code; + + return $this; + } + public function getClientRef(): ?string { return $this->clientRef; @@ -106,7 +120,7 @@ public function setBrand(string $brand): static public function getWorkflows(): array { - return array_map(static function($workflow) { + return array_map(static function ($workflow) { return $workflow->toArray(); }, $this->workflows); } @@ -132,6 +146,10 @@ public function getBaseVerifyUniversalOutputArray(): array $returnArray['client_ref'] = $this->getClientRef(); } + if ($this->getCode()) { + $returnArray['code'] = $this->getCode(); + } + return $returnArray; } -} \ No newline at end of file +} diff --git a/src/Verify2/Request/EmailRequest.php b/src/Verify2/Request/EmailRequest.php index eecce3e7..d7694e96 100644 --- a/src/Verify2/Request/EmailRequest.php +++ b/src/Verify2/Request/EmailRequest.php @@ -11,14 +11,18 @@ public function __construct( protected string $to, protected string $brand, protected string $from, - protected ?string $clientRef = null, protected ?VerificationLocale $locale = null, ) { if (!$this->locale) { $this->locale = new VerificationLocale(); } + if ($this->code) { + $this->setCode($this->code); + } + $workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_EMAIL, $to, $from); + $this->addWorkflow($workflow); } @@ -26,4 +30,4 @@ public function toArray(): array { return $this->getBaseVerifyUniversalOutputArray(); } -} \ No newline at end of file +} diff --git a/src/Verify2/Request/RequestInterface.php b/src/Verify2/Request/RequestInterface.php index 9ce0fc05..e4d0538e 100644 --- a/src/Verify2/Request/RequestInterface.php +++ b/src/Verify2/Request/RequestInterface.php @@ -20,4 +20,6 @@ public function getLength(): int; public function getBrand(): string; public function getWorkflows(): array; public function getBaseVerifyUniversalOutputArray(): array; + public function setCode(string $code): static; + public function getCode(): ?string; } \ No newline at end of file diff --git a/src/Verify2/Request/SMSRequest.php b/src/Verify2/Request/SMSRequest.php index 2398d711..ff7ee98f 100644 --- a/src/Verify2/Request/SMSRequest.php +++ b/src/Verify2/Request/SMSRequest.php @@ -10,14 +10,14 @@ class SMSRequest extends BaseVerifyRequest public function __construct( protected string $to, protected string $brand, - protected ?string $clientRef = null, - protected ?VerificationLocale $locale = null + protected ?VerificationLocale $locale = null, ) { if (!$this->locale) { $this->locale = new VerificationLocale(); } $workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_SMS, $to); + $this->addWorkflow($workflow); } @@ -25,4 +25,4 @@ public function toArray(): array { return $this->getBaseVerifyUniversalOutputArray(); } -} \ No newline at end of file +} diff --git a/src/Verify2/Request/VoiceRequest.php b/src/Verify2/Request/VoiceRequest.php index 99b4b601..5d04cd89 100644 --- a/src/Verify2/Request/VoiceRequest.php +++ b/src/Verify2/Request/VoiceRequest.php @@ -10,14 +10,18 @@ class VoiceRequest extends BaseVerifyRequest public function __construct( protected string $to, protected string $brand, - protected ?string $clientRef, - protected ?VerificationLocale $locale = null + protected ?VerificationLocale $locale = null, ) { if (!$this->locale) { $this->locale = new VerificationLocale(); } $workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_VOICE, $to); + + if ($this->code) { + $workflow->setCode($this->code); + } + $this->addWorkflow($workflow); } diff --git a/src/Verify2/Request/WhatsAppInteractiveRequest.php b/src/Verify2/Request/WhatsAppInteractiveRequest.php index f9d8ca91..1cee7d39 100644 --- a/src/Verify2/Request/WhatsAppInteractiveRequest.php +++ b/src/Verify2/Request/WhatsAppInteractiveRequest.php @@ -10,7 +10,6 @@ class WhatsAppInteractiveRequest extends BaseVerifyRequest public function __construct( protected string $to, protected string $brand, - protected ?string $clientRef, protected ?VerificationLocale $locale = null ) { if (!$this->locale) { diff --git a/src/Verify2/Request/WhatsAppRequest.php b/src/Verify2/Request/WhatsAppRequest.php index 4991c8b3..394112af 100644 --- a/src/Verify2/Request/WhatsAppRequest.php +++ b/src/Verify2/Request/WhatsAppRequest.php @@ -11,14 +11,14 @@ public function __construct( protected string $to, protected string $brand, protected ?string $from = null, - protected ?string $clientRef = null, - protected ?VerificationLocale $locale = null + protected ?VerificationLocale $locale = null, ) { if (!$this->locale) { $this->locale = new VerificationLocale(); } $workflow = new VerificationWorkflow(VerificationWorkflow::WORKFLOW_WHATSAPP, $to); + $this->addWorkflow($workflow); } @@ -26,4 +26,4 @@ public function toArray(): array { return $this->getBaseVerifyUniversalOutputArray(); } -} \ No newline at end of file +} diff --git a/src/Verify2/VerifyObjects/VerificationWorkflow.php b/src/Verify2/VerifyObjects/VerificationWorkflow.php index 9ac568b3..129e7733 100644 --- a/src/Verify2/VerifyObjects/VerificationWorkflow.php +++ b/src/Verify2/VerifyObjects/VerificationWorkflow.php @@ -22,8 +22,11 @@ class VerificationWorkflow implements ArrayHydrateInterface self::WORKFLOW_SILENT_AUTH ]; - public function __construct(protected string $channel, protected string $to, protected string $from = '') - { + public function __construct( + protected string $channel, + protected string $to, + protected string $from = '' + ) { if (! in_array($channel, $this->allowedWorkflows, true)) { throw new \InvalidArgumentException($this->channel . ' is not a valid workflow'); } @@ -89,6 +92,5 @@ public function toArray(): array } return $returnArray; - } -} \ No newline at end of file +} diff --git a/test/Verify2/ClientTest.php b/test/Verify2/ClientTest.php index e112a947..2b0b51f2 100644 --- a/test/Verify2/ClientTest.php +++ b/test/Verify2/ClientTest.php @@ -64,7 +64,8 @@ public function testSetsRequestAuthCorrectly(): void 'brand' => 'my-brand', ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) { $this->assertEquals( @@ -85,7 +86,8 @@ public function testCanRequestSMS(): void 'brand' => 'my-brand', ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { $uri = $request->getUri(); @@ -131,7 +133,8 @@ public function testCannotRequestSMSWithInvalidLocale($locale, $valid): void 'locale' => $verificationLocale, ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref'], $payload['locale']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['locale']); + $smsVerification->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { $this->assertEquals( @@ -167,7 +170,8 @@ public function testTimeoutParsesCorrectly($timeout, $valid): void 'timeout' => $timeout ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setClientRef($payload['client_ref']); $smsVerification->setTimeout($timeout); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { @@ -203,7 +207,8 @@ public function testCannotRequestSMSWithInvalidCodeLength($length, $valid): void 'length' => $length ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setClientRef($payload['client_ref']); $smsVerification->setLength($payload['length']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { @@ -225,7 +230,8 @@ public function testCanRequestWhatsApp(): void 'brand' => 'my-brand', ]; - $whatsAppVerification = new WhatsAppRequest($payload['to'], $payload['brand'],null, $payload['client_ref']); + $whatsAppVerification = new WhatsAppRequest($payload['to'], $payload['brand']); + $whatsAppVerification->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { $this->assertRequestJsonBodyContains('locale', 'en-us', $request); @@ -254,7 +260,8 @@ public function testCanRequestWhatsAppInteractive(): void 'brand' => 'my-brand', ]; - $whatsAppInteractiveRequest = new WhatsAppInteractiveRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $whatsAppInteractiveRequest = new WhatsAppInteractiveRequest($payload['to'], $payload['brand']); + $whatsAppInteractiveRequest->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { $this->assertRequestJsonBodyContains('locale', 'en-us', $request); @@ -282,7 +289,8 @@ public function testCanRequestVoice(): void 'brand' => 'my-brand', ]; - $voiceRequest = new VoiceRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $voiceRequest = new VoiceRequest($payload['to'], $payload['brand']); + $voiceRequest->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { $this->assertRequestJsonBodyContains('locale', 'en-us', $request); @@ -311,7 +319,8 @@ public function testCanRequestEmail(): void 'brand' => 'my-brand', ]; - $emailRequest = new EmailRequest($payload['to'], $payload['brand'], $payload['from'], $payload['client_ref']); + $emailRequest = new EmailRequest($payload['to'], $payload['brand'], $payload['from']); + $emailRequest->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) use ($payload) { $this->assertRequestJsonBodyContains('locale', 'en-us', $request); @@ -332,6 +341,25 @@ public function testCanRequestEmail(): void $this->assertArrayHasKey('request_id', $result); } + public function testCanRenderOptionalCode(): void + { + $payload = [ + 'to' => '07785648870', + 'brand' => 'my-brand', + ]; + + $smsRequest = new SMSRequest($payload['to'], $payload['brand']); + $smsRequest->setCode('123456789'); + + $this->vonageClient->send(Argument::that(function (Request $request) { + $this->assertRequestJsonBodyContains('code', '123456789', $request, true); + + return true; + }))->willReturn($this->getResponse('verify-request-success', 202)); + + $result = $this->verify2Client->startVerification($smsRequest); + } + public function testCanHandleMultipleWorkflows(): void { $payload = [ @@ -340,7 +368,8 @@ public function testCanHandleMultipleWorkflows(): void 'brand' => 'my-brand', ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setClientRef($payload['client_ref']); $voiceWorkflow = new VerificationWorkflow('voice', '07785254785'); $smsVerification->addWorkflow($voiceWorkflow); @@ -389,7 +418,8 @@ public function testCannotSendConcurrentVerifications(): void 'brand' => 'my-brand', ]; - $smsVerification = new SMSRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $smsVerification = new SMSRequest($payload['to'], $payload['brand']); + $smsVerification->setClientRef( $payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) { $this->assertEquals('POST', $request->getMethod()); @@ -418,7 +448,8 @@ public function testCannotSendWithoutBrand(): void 'brand' => '', ]; - $voiceRequest = new VoiceRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $voiceRequest = new VoiceRequest($payload['to'], $payload['brand']); + $voiceRequest->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) { $this->assertEquals('POST', $request->getMethod()); @@ -439,7 +470,8 @@ public function testCanHandleThrottle(): void 'brand' => 'my-brand', ]; - $voiceRequest = new VoiceRequest($payload['to'], $payload['brand'], $payload['client_ref']); + $voiceRequest = new VoiceRequest($payload['to'], $payload['brand']); + $voiceRequest->setClientRef($payload['client_ref']); $this->vonageClient->send(Argument::that(function (Request $request) { $this->assertEquals('POST', $request->getMethod());