DoctrinePaginatorAdapterTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace League\Fractal\Test\Pagination;
  3. use Doctrine\ORM\Query;
  4. use League\Fractal\Pagination\DoctrinePaginatorAdapter;
  5. use Mockery;
  6. use PHPUnit\Framework\TestCase;
  7. class DoctrinePaginatorAdapterTest extends TestCase
  8. {
  9. public function testPaginationAdapter()
  10. {
  11. $total = 50;
  12. $count = 5;
  13. $perPage = 5;
  14. $currentPage = 2;
  15. $lastPage = 10;
  16. //Mock the doctrine paginator
  17. $paginator = Mockery::mock('Doctrine\ORM\Tools\Pagination\Paginator')->makePartial();
  18. $paginator->shouldReceive('count')->andReturn($total);
  19. //Mock the query that the paginator is acting on
  20. $query = Mockery::mock('Doctrine\ORM\AbstractQuery');
  21. $query->shouldReceive('getFirstResult')->andReturn(($currentPage - 1) * $perPage);
  22. $query->shouldReceive('getMaxResults')->andReturn($perPage);
  23. $paginator->shouldReceive('getQuery')->andReturn($query);
  24. //Mock the iterator of the paginator
  25. $iterator = Mockery::mock('IteratorAggregate');
  26. $iterator->shouldReceive('count')->andReturn($count);
  27. $paginator->shouldReceive('getIterator')->andReturn($iterator);
  28. $adapter = new DoctrinePaginatorAdapter($paginator, function ($page) {
  29. return 'http://example.com/foo?page='.$page;
  30. });
  31. $this->assertInstanceOf(
  32. 'League\Fractal\Pagination\PaginatorInterface',
  33. $adapter
  34. );
  35. $this->assertSame($currentPage, $adapter->getCurrentPage());
  36. $this->assertSame($lastPage, $adapter->getLastPage());
  37. $this->assertSame($count, $adapter->getCount());
  38. $this->assertSame($total, $adapter->getTotal());
  39. $this->assertSame($perPage, $adapter->getPerPage());
  40. $this->assertSame(
  41. 'http://example.com/foo?page=1',
  42. $adapter->getUrl(1)
  43. );
  44. $this->assertSame(
  45. 'http://example.com/foo?page=3',
  46. $adapter->getUrl(3)
  47. );
  48. }
  49. public function tearDown()
  50. {
  51. Mockery::close();
  52. }
  53. }