Skip to content

Commit

Permalink
Feature/verify v2 byop (#400)
Browse files Browse the repository at this point in the history
* Added BYOP with tests

* Move BYOP to outside workflow scope, remove client ref from constructor
  • Loading branch information
SecondeJK authored Apr 17, 2023
1 parent b9d3b25 commit 6f6bb09
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 30 deletions.
22 changes: 20 additions & 2 deletions src/Verify2/Request/BaseVerifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ abstract class BaseVerifyRequest implements RequestInterface

protected array $workflows = [];

protected ?string $code = null;

public function getLocale(): ?VerificationLocale
{
return $this->locale;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -132,6 +146,10 @@ public function getBaseVerifyUniversalOutputArray(): array
$returnArray['client_ref'] = $this->getClientRef();
}

if ($this->getCode()) {
$returnArray['code'] = $this->getCode();
}

return $returnArray;
}
}
}
8 changes: 6 additions & 2 deletions src/Verify2/Request/EmailRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ 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);
}

public function toArray(): array
{
return $this->getBaseVerifyUniversalOutputArray();
}
}
}
2 changes: 2 additions & 0 deletions src/Verify2/Request/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions src/Verify2/Request/SMSRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ 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);
}

public function toArray(): array
{
return $this->getBaseVerifyUniversalOutputArray();
}
}
}
8 changes: 6 additions & 2 deletions src/Verify2/Request/VoiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/Verify2/Request/WhatsAppInteractiveRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/Verify2/Request/WhatsAppRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ 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);
}

public function toArray(): array
{
return $this->getBaseVerifyUniversalOutputArray();
}
}
}
10 changes: 6 additions & 4 deletions src/Verify2/VerifyObjects/VerificationWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -89,6 +92,5 @@ public function toArray(): array
}

return $returnArray;

}
}
}
58 changes: 45 additions & 13 deletions test/Verify2/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 = [
Expand All @@ -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);

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 6f6bb09

Please sign in to comment.