CursorPaginatorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Illuminate\Tests\Pagination;
  3. use Illuminate\Pagination\Cursor;
  4. use Illuminate\Pagination\CursorPaginator;
  5. use Illuminate\Support\Collection;
  6. use PHPUnit\Framework\TestCase;
  7. class CursorPaginatorTest extends TestCase
  8. {
  9. public function testReturnsRelevantContextInformation()
  10. {
  11. $p = new CursorPaginator($array = [['id' => 1], ['id' => 2], ['id' => 3]], 2, null, [
  12. 'parameters' => ['id'],
  13. ]);
  14. $this->assertTrue($p->hasPages());
  15. $this->assertTrue($p->hasMorePages());
  16. $this->assertEquals([['id' => 1], ['id' => 2]], $p->items());
  17. $pageInfo = [
  18. 'data' => [['id' => 1], ['id' => 2]],
  19. 'path' => '/',
  20. 'per_page' => 2,
  21. 'next_page_url' => '/?cursor='.$this->getCursor(['id' => 2]),
  22. 'prev_page_url' => null,
  23. ];
  24. $this->assertEquals($pageInfo, $p->toArray());
  25. }
  26. public function testPaginatorRemovesTrailingSlashes()
  27. {
  28. $p = new CursorPaginator($array = [['id' => 4], ['id' => 5], ['id' => 6]], 2, null,
  29. ['path' => 'http://website.com/test/', 'parameters' => ['id']]);
  30. $this->assertSame('http://website.com/test?cursor='.$this->getCursor(['id' => 5]), $p->nextPageUrl());
  31. }
  32. public function testPaginatorGeneratesUrlsWithoutTrailingSlash()
  33. {
  34. $p = new CursorPaginator($array = [['id' => 4], ['id' => 5], ['id' => 6]], 2, null,
  35. ['path' => 'http://website.com/test', 'parameters' => ['id']]);
  36. $this->assertSame('http://website.com/test?cursor='.$this->getCursor(['id' => 5]), $p->nextPageUrl());
  37. }
  38. public function testItRetrievesThePaginatorOptions()
  39. {
  40. $p = new CursorPaginator($array = [['id' => 4], ['id' => 5], ['id' => 6]], 2, null,
  41. $options = ['path' => 'http://website.com/test', 'parameters' => ['id']]);
  42. $this->assertSame($p->getOptions(), $options);
  43. }
  44. public function testPaginatorReturnsPath()
  45. {
  46. $p = new CursorPaginator($array = [['id' => 4], ['id' => 5], ['id' => 6]], 2, null,
  47. $options = ['path' => 'http://website.com/test', 'parameters' => ['id']]);
  48. $this->assertSame($p->path(), 'http://website.com/test');
  49. }
  50. public function testCanTransformPaginatorItems()
  51. {
  52. $p = new CursorPaginator($array = [['id' => 4], ['id' => 5], ['id' => 6]], 2, null,
  53. $options = ['path' => 'http://website.com/test', 'parameters' => ['id']]);
  54. $p->through(function ($item) {
  55. $item['id'] = $item['id'] + 2;
  56. return $item;
  57. });
  58. $this->assertInstanceOf(CursorPaginator::class, $p);
  59. $this->assertSame([['id' => 6], ['id' => 7]], $p->items());
  60. }
  61. public function testReturnEmptyCursorWhenItemsAreEmpty()
  62. {
  63. $cursor = new Cursor(['id' => 25], true);
  64. $p = new CursorPaginator(Collection::make(), 25, $cursor, [
  65. 'path' => 'http://website.com/test',
  66. 'cursorName' => 'cursor',
  67. 'parameters' => ['id'],
  68. ]);
  69. $this->assertInstanceOf(CursorPaginator::class, $p);
  70. $this->assertSame([
  71. 'data' => [],
  72. 'path' => 'http://website.com/test',
  73. 'per_page' => 25,
  74. 'next_page_url' => null,
  75. 'prev_page_url' => null,
  76. ], $p->toArray());
  77. }
  78. protected function getCursor($params, $isNext = true)
  79. {
  80. return (new Cursor($params, $isNext))->encode();
  81. }
  82. }