|
3 | 3 | namespace Hussainweb\DrupalApi\Tests\Entity\Collection; |
4 | 4 |
|
5 | 5 | use GuzzleHttp\Psr7\Response; |
| 6 | +use Hussainweb\DrupalApi\Client; |
| 7 | +use Hussainweb\DrupalApi\Entity\Collection\CommentCollection; |
6 | 8 | use Hussainweb\DrupalApi\Entity\Collection\PagingEntityCollection; |
7 | 9 | use Hussainweb\DrupalApi\Entity\Comment; |
| 10 | +use Hussainweb\DrupalApi\Request\Collection\CommentCollectionRequest; |
| 11 | +use Hussainweb\DrupalApi\Request\Request; |
8 | 12 | use PHPUnit\Framework\TestCase; |
9 | 13 | use Psr\Http\Client\ClientInterface; |
10 | 14 |
|
@@ -40,4 +44,37 @@ public function testPaging() |
40 | 44 |
|
41 | 45 | $this->assertEquals(10, $count); |
42 | 46 | } |
| 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 | + } |
43 | 80 | } |
0 commit comments