Skip to content

Commit 10b59d8

Browse files
Add a method to convert an entity collection into a paging collection
1 parent fb45fcf commit 10b59d8

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
max_line_length = 120
10+
tab_width = 4
11+
12+
[{*.ctp,*.engine,*.hphp,*.inc,*.install,*.module,*.php,*.php4,*.php5,*.phtml,*.profile,*.test,*.theme}]
13+
max_line_length = 80

src/Entity/Collection/EntityCollection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hussainweb\DrupalApi\Entity\Collection;
44

5+
use Psr\Http\Client\ClientInterface;
56
use Psr\Http\Message\ResponseInterface;
67
use Symfony\Component\Serializer\Encoder\JsonDecode;
78
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -36,6 +37,21 @@ public static function fromResponse(ResponseInterface $response)
3637
return new static((new JsonDecode())->decode((string) $response->getBody(), JsonEncoder::FORMAT));
3738
}
3839

40+
/**
41+
* Construct a paging entity collection from this collection.
42+
*
43+
* @param \Psr\Http\Client\ClientInterface $client
44+
* The HTTP client used to fetch subsequent pages.
45+
*
46+
* @see \Hussainweb\DrupalApi\Client::getEntity
47+
*
48+
* @return \Hussainweb\DrupalApi\Entity\Collection\PagingEntityCollection
49+
*/
50+
public function toPagingEntityCollection(ClientInterface $client): PagingEntityCollection
51+
{
52+
return new PagingEntityCollection($this->rawData, $client, $this->getListItemClass());
53+
}
54+
3955
/**
4056
* Get the name of the class for a list item.
4157
*

tests/Entity/Collection/PagingEntityCollectionTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
namespace Hussainweb\DrupalApi\Tests\Entity\Collection;
44

55
use GuzzleHttp\Psr7\Response;
6+
use Hussainweb\DrupalApi\Client;
7+
use Hussainweb\DrupalApi\Entity\Collection\CommentCollection;
68
use Hussainweb\DrupalApi\Entity\Collection\PagingEntityCollection;
79
use Hussainweb\DrupalApi\Entity\Comment;
10+
use Hussainweb\DrupalApi\Request\Collection\CommentCollectionRequest;
11+
use Hussainweb\DrupalApi\Request\Request;
812
use PHPUnit\Framework\TestCase;
913
use Psr\Http\Client\ClientInterface;
1014

@@ -40,4 +44,37 @@ public function testPaging()
4044

4145
$this->assertEquals(10, $count);
4246
}
47+
48+
public function testFromClient()
49+
{
50+
$data = json_decode(file_get_contents(__DIR__ . '/../../fixtures/comment-collection-page-0.json'));
51+
/** @var \Hussainweb\DrupalApi\Client|\PHPUnit\Framework\MockObject\MockObject $client */
52+
$client = $this->createMock(Client::class);
53+
$page1 = json_decode(file_get_contents(
54+
__DIR__ . '/../../fixtures/comment-collection-page-1.json'
55+
));
56+
57+
// Modify the fixture so there's only two pages.
58+
unset($page1->next);
59+
$page1 = json_encode($page1);
60+
61+
$comment_collection = new CommentCollection($data);
62+
$client->expects($this->once())->method('getEntity')
63+
->willReturn($comment_collection);
64+
65+
$request = new CommentCollectionRequest();
66+
$entity_collection = $client->getEntity($request);
67+
68+
$http_client = $this->createMock(ClientInterface::class);
69+
$http_client->expects($this->once())->method('sendRequest')
70+
->willReturn(new Response(200, ['Content-Type' => 'application/json'], $page1));
71+
$paging_collection = $entity_collection->toPagingEntityCollection($http_client);
72+
$count = 0;
73+
foreach ($paging_collection as $comment) {
74+
$count++;
75+
$this->assertInstanceOf(Comment::class, $comment);
76+
}
77+
78+
$this->assertEquals(10, $count);
79+
}
4380
}

0 commit comments

Comments
 (0)