LengthAwarePaginatorTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Illuminate\Tests\Pagination;
  3. use Illuminate\Pagination\LengthAwarePaginator;
  4. use PHPUnit\Framework\TestCase;
  5. class LengthAwarePaginatorTest extends TestCase
  6. {
  7. /**
  8. * @var \Illuminate\Pagination\LengthAwarePaginator
  9. */
  10. private $p;
  11. /**
  12. * @var array
  13. */
  14. private $options;
  15. protected function setUp(): void
  16. {
  17. $this->options = ['onEachSide' => 5];
  18. $this->p = new LengthAwarePaginator($array = ['item1', 'item2', 'item3', 'item4'], 4, 2, 2, $this->options);
  19. }
  20. protected function tearDown(): void
  21. {
  22. unset($this->p);
  23. }
  24. public function testLengthAwarePaginatorGetAndSetPageName()
  25. {
  26. $this->assertSame('page', $this->p->getPageName());
  27. $this->p->setPageName('p');
  28. $this->assertSame('p', $this->p->getPageName());
  29. }
  30. public function testLengthAwarePaginatorCanGiveMeRelevantPageInformation()
  31. {
  32. $this->assertEquals(2, $this->p->lastPage());
  33. $this->assertEquals(2, $this->p->currentPage());
  34. $this->assertTrue($this->p->hasPages());
  35. $this->assertFalse($this->p->hasMorePages());
  36. $this->assertEquals(['item1', 'item2', 'item3', 'item4'], $this->p->items());
  37. }
  38. public function testLengthAwarePaginatorSetCorrectInformationWithNoItems()
  39. {
  40. $paginator = new LengthAwarePaginator([], 0, 2, 1);
  41. $this->assertEquals(1, $paginator->lastPage());
  42. $this->assertEquals(1, $paginator->currentPage());
  43. $this->assertFalse($paginator->hasPages());
  44. $this->assertFalse($paginator->hasMorePages());
  45. $this->assertEmpty($paginator->items());
  46. }
  47. public function testLengthAwarePaginatorisOnFirstAndLastPage()
  48. {
  49. $paginator = new LengthAwarePaginator(['1', '2', '3', '4'], 4, 2, 2);
  50. $this->assertTrue($paginator->onLastPage());
  51. $this->assertFalse($paginator->onFirstPage());
  52. $paginator = new LengthAwarePaginator(['1', '2', '3', '4'], 4, 2, 1);
  53. $this->assertFalse($paginator->onLastPage());
  54. $this->assertTrue($paginator->onFirstPage());
  55. }
  56. public function testLengthAwarePaginatorCanGenerateUrls()
  57. {
  58. $this->p->setPath('http://website.com');
  59. $this->p->setPageName('foo');
  60. $this->assertSame('http://website.com',
  61. $this->p->path());
  62. $this->assertSame('http://website.com?foo=2',
  63. $this->p->url($this->p->currentPage()));
  64. $this->assertSame('http://website.com?foo=1',
  65. $this->p->url($this->p->currentPage() - 1));
  66. $this->assertSame('http://website.com?foo=1',
  67. $this->p->url($this->p->currentPage() - 2));
  68. }
  69. public function testLengthAwarePaginatorCanGenerateUrlsWithQuery()
  70. {
  71. $this->p->setPath('http://website.com?sort_by=date');
  72. $this->p->setPageName('foo');
  73. $this->assertSame('http://website.com?sort_by=date&foo=2',
  74. $this->p->url($this->p->currentPage()));
  75. }
  76. public function testLengthAwarePaginatorCanGenerateUrlsWithoutTrailingSlashes()
  77. {
  78. $this->p->setPath('http://website.com/test');
  79. $this->p->setPageName('foo');
  80. $this->assertSame('http://website.com/test?foo=2',
  81. $this->p->url($this->p->currentPage()));
  82. $this->assertSame('http://website.com/test?foo=1',
  83. $this->p->url($this->p->currentPage() - 1));
  84. $this->assertSame('http://website.com/test?foo=1',
  85. $this->p->url($this->p->currentPage() - 2));
  86. }
  87. public function testLengthAwarePaginatorCorrectlyGenerateUrlsWithQueryAndSpaces()
  88. {
  89. $this->p->setPath('http://website.com?key=value%20with%20spaces');
  90. $this->p->setPageName('foo');
  91. $this->assertSame('http://website.com?key=value%20with%20spaces&foo=2',
  92. $this->p->url($this->p->currentPage()));
  93. }
  94. public function testItRetrievesThePaginatorOptions()
  95. {
  96. $this->assertSame($this->options, $this->p->getOptions());
  97. }
  98. }