PaginatorTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Illuminate\Tests\Pagination;
  3. use Illuminate\Pagination\Paginator;
  4. use PHPUnit\Framework\TestCase;
  5. class PaginatorTest extends TestCase
  6. {
  7. public function testSimplePaginatorReturnsRelevantContextInformation()
  8. {
  9. $p = new Paginator($array = ['item3', 'item4', 'item5'], 2, 2);
  10. $this->assertEquals(2, $p->currentPage());
  11. $this->assertTrue($p->hasPages());
  12. $this->assertTrue($p->hasMorePages());
  13. $this->assertEquals(['item3', 'item4'], $p->items());
  14. $pageInfo = [
  15. 'per_page' => 2,
  16. 'current_page' => 2,
  17. 'first_page_url' => '/?page=1',
  18. 'next_page_url' => '/?page=3',
  19. 'prev_page_url' => '/?page=1',
  20. 'from' => 3,
  21. 'to' => 4,
  22. 'data' => ['item3', 'item4'],
  23. 'path' => '/',
  24. ];
  25. $this->assertEquals($pageInfo, $p->toArray());
  26. }
  27. public function testPaginatorRemovesTrailingSlashes()
  28. {
  29. $p = new Paginator($array = ['item1', 'item2', 'item3'], 2, 2,
  30. ['path' => 'http://website.com/test/']);
  31. $this->assertSame('http://website.com/test?page=1', $p->previousPageUrl());
  32. }
  33. public function testPaginatorGeneratesUrlsWithoutTrailingSlash()
  34. {
  35. $p = new Paginator($array = ['item1', 'item2', 'item3'], 2, 2,
  36. ['path' => 'http://website.com/test']);
  37. $this->assertSame('http://website.com/test?page=1', $p->previousPageUrl());
  38. }
  39. public function testItRetrievesThePaginatorOptions()
  40. {
  41. $p = new Paginator($array = ['item1', 'item2', 'item3'], 2, 2,
  42. $options = ['path' => 'http://website.com/test']);
  43. $this->assertSame($p->getOptions(), $options);
  44. }
  45. public function testPaginatorReturnsPath()
  46. {
  47. $p = new Paginator($array = ['item1', 'item2', 'item3'], 2, 2,
  48. ['path' => 'http://website.com/test']);
  49. $this->assertSame($p->path(), 'http://website.com/test');
  50. }
  51. public function testCanTransformPaginatorItems()
  52. {
  53. $p = new Paginator($array = ['item1', 'item2', 'item3'], 3, 1,
  54. ['path' => 'http://website.com/test']);
  55. $p->through(function ($item) {
  56. return substr($item, 4, 1);
  57. });
  58. $this->assertInstanceOf(Paginator::class, $p);
  59. $this->assertSame(['1', '2', '3'], $p->items());
  60. }
  61. }