Skip to content

Commit

Permalink
Iterators completed
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Sep 13, 2024
1 parent 6d11b44 commit 9c10394
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 25 deletions.
30 changes: 28 additions & 2 deletions src/Verify2/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ public function listCustomTemplates(TemplateFilter $filter = null): IterableAPIC
{
$collection = $this->api->search($filter, '/templates');
$collection->setNaiveCount(true);
$collection->setPageIndexKey('page');

if (is_null($filter)) {
$collection->setNoQueryParameters(true);
}

// $collection->setPageIndexKey('page');

$hydrator = new ArrayHydrator();
$hydrator->setPrototype(new Template());
$collection->setHydrator($hydrator);
Expand Down Expand Up @@ -158,4 +157,31 @@ public function deleteCustomTemplateFragment(string $templateId, string $fragmen
$this->api->delete('templates/' . $templateId . '/template_fragments/' . $fragmentId);
return true;
}

public function listTemplateFragments(string $templateId, TemplateFilter $filter = null): IterableAPICollection
{
$api = clone $this->getAPIResource();
$api->setCollectionName('template_fragments');

$collection = $api->search($filter, '/templates/' . $templateId . '/template_fragments');
$collection->setNaiveCount(true);
$collection->setPageIndexKey('page');

if (is_null($filter)) {
$collection->setNoQueryParameters(true);
}

if (!is_null($filter)) {
if ($filter->getQuery()['page']) {
$collection->setAutoAdvance(false);
$collection->setIndex($filter->getQuery()['page']);
}
}

$hydrator = new ArrayHydrator();
$hydrator->setPrototype(new TemplateFragment());
$collection->setHydrator($hydrator);

return $collection;
}
}
12 changes: 3 additions & 9 deletions src/Verify2/Filters/TemplateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function getPageSize(): ?int
return $this->pageSize;
}

/**
* @return $this
*/
public function setPageSize(int $pageSize): self
{
$this->pageSize = $pageSize;
Expand All @@ -43,15 +40,12 @@ public function setPageSize(int $pageSize): self

public function getPage(): ?int
{
return $this->pageSize;
return $this->page;
}

/**
* @return $this
*/
public function setPage(int $pageSize): self
public function setPage(int $page): self
{
$this->pageSize = $pageSize;
$this->page = $page;

return $this;
}
Expand Down
52 changes: 38 additions & 14 deletions test/Verify2/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Vonage\Client;
use Vonage\Client\APIResource;
use Vonage\Verify2\Client as Verify2Client;
use Vonage\Verify2\Filters\TemplateFilter;
use Vonage\Verify2\Request\CreateCustomTemplateFragmentRequest;
use Vonage\Verify2\Request\EmailRequest;
use Vonage\Verify2\Request\SilentAuthRequest;
Expand All @@ -23,7 +24,6 @@
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;
use VonageTest\Traits\HTTPTestTrait;
use VonageTest\Traits\Psr7AssertionTrait;
use VonageTest\Verify2\Request\UpdateTemplateRequest;
use VonageTest\VonageTestCase;

class ClientTest extends VonageTestCase
Expand All @@ -34,6 +34,7 @@ class ClientTest extends VonageTestCase
protected ObjectProphecy $vonageClient;
protected Verify2Client $verify2Client;
protected APIResource $api;
private int $requestCount;

public function setUp(): void
{
Expand All @@ -49,7 +50,7 @@ public function setUp(): void

/** @noinspection PhpParamsInspection */
$this->api = (new APIResource())
->setIsHAL(false)
->setIsHAL(true)
->setErrorsOn200(false)
->setClient($this->vonageClient->reveal())
->setAuthHandlers([new Client\Credentials\Handler\BasicHandler(), new Client\Credentials\Handler\KeypairHandler()])
Expand Down Expand Up @@ -727,7 +728,7 @@ public function testWillListCustomTemplatesWithQuery(): void
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v2/verify/templates?page_size=100&page=1',
'https://api.nexmo.com/v2/verify/templates?page=1&page_size=5',
$uriString
);

Expand All @@ -741,7 +742,10 @@ public function testWillListCustomTemplatesWithQuery(): void
$filter->setPage(2);

$response = $this->verify2Client->listCustomTemplates($filter);
$this->assertCount(5, $response);

foreach ($response as $template) {
$this->assertInstanceOf(Template::class, $template);
}
}

public function testWillCreateTemplate(): void
Expand Down Expand Up @@ -829,21 +833,38 @@ public function testWillUpdateTemplate(): void

public function testWillListTemplateFragments(): void
{
$this->requestCount = 0;
$this->vonageClient->send(Argument::that(function (Request $request) {
$this->requestCount++;
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments',
$uriString
);

if ($this->requestCount == 1) {
$this->assertEquals(
'https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments',
$uriString
);
}

if ($this->requestCount == 2) {
$this->assertEquals(
'https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=3',
$uriString
);
}

$this->assertEquals('GET', $request->getMethod());

return true;
}))->willReturn($this->getResponse('list-template-fragment-success'));
}))->willReturn($this->getResponse('list-template-fragment-success'), $this->getResponse('list-template-fragment-success-2'));

$fragments = $this->verify2Client->listTemplateFragments('8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9');
$this->assertCount(2, $fragments);

foreach ($fragments as $fragment) {
$this->assertInstanceOf(TemplateFragment::class, $fragment);
}

$this->requestCount = 0;
}

public function testWillListTemplateFragmentsWithQuery(): void
Expand All @@ -852,20 +873,23 @@ public function testWillListTemplateFragmentsWithQuery(): void
$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments?page_size=5&&page=2',
'https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments?page=2&page_size=10',
$uriString
);

$this->assertEquals('GET', $request->getMethod());

return true;
}))->willReturn($this->getResponse('list-template-fragment-success-2'));
}))->willReturn($this->getResponse('list-template-fragment-success'));
$templateFilter = new TemplateFilter();
$templateFilter->setPage(2);
$templateFilter->setPageSize(5);
$templateFilter->setPageSize(10);

$fragments = $this->verify2Client->listTemplateFragments('8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9', $templateFilter);
$this->assertCount(5, $fragments);

foreach ($fragments as $fragment) {
$this->assertInstanceOf(TemplateFragment::class, $fragment);
}
}

public function testWillCreateTemplateFragment(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"page_size": 1,
"page": 2,
"total_pages": 10,
"total_items": 25,
"_embedded": {
"template_fragments": [
{
"template_fragment_id": "c70f446e-997a-4313-a081-60a02a31dc19",
"channel": "sms",
"locale": "en_US",
"text": "Text content of the template. May contain 4 reserved variables: `${code}`, `${brand}`, `${time-limit}` and `${time-limit-unit}`",
"date_updated": "2023-08-30T15:20:15.17865735Z",
"date_created": "2021-08-30T20:12:15.17865735Z",
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments/c70f446e-997a-4313-a081-60a02a31dc19"
},
"template": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9"
}
}
}
]
},
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=2"
},
"prev": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/templates_fragments?page=1"
},
"last": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/templates_fragments?page=5"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"page_size": 1,
"page": 2,
"total_pages": 10,
"total_items": 25,
"_embedded": {
"template_fragments": [
{
"template_fragment_id": "c70f446e-997a-4313-a081-60a02a31dc19",
"channel": "sms",
"locale": "en_US",
"text": "Text content of the template. May contain 4 reserved variables: `${code}`, `${brand}`, `${time-limit}` and `${time-limit-unit}`",
"date_updated": "2023-08-30T15:20:15.17865735Z",
"date_created": "2021-08-30T20:12:15.17865735Z",
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments/c70f446e-997a-4313-a081-60a02a31dc19"
},
"template": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9"
}
}
}
]
},
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=2"
},
"next": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=3"
},
"prev": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=1"
},
"last": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=5"
}
}
}

0 comments on commit 9c10394

Please sign in to comment.