SimplePagerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Datagrid;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Sonata\AdminBundle\Datagrid\SimplePager;
  13. /**
  14. * Simple pager.
  15. *
  16. * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  17. * @author Sjoerd Peters <sjoerd.peters@gmail.com>
  18. */
  19. class SimplePagerTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function setUp()
  22. {
  23. $this->pager = new SimplePager(10, 2);
  24. $this->proxyQuery = $this->getMockBuilder('Sonata\AdminBundle\Datagrid\ProxyQueryInterface')
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. }
  28. public function testInitNumPages()
  29. {
  30. $pager = new SimplePager(10, 2);
  31. $this->proxyQuery->expects($this->once())
  32. ->method('execute')
  33. ->with(array(), null)
  34. ->will($this->returnValue(new ArrayCollection(range(0, 12))));
  35. $this->proxyQuery->expects($this->once())
  36. ->method('setMaxResults')
  37. ->with($this->equalTo(21));
  38. $this->proxyQuery->expects($this->once())
  39. ->method('setFirstResult')
  40. ->with($this->equalTo(0));
  41. $pager->setQuery($this->proxyQuery);
  42. $pager->init();
  43. $this->assertSame(2, $pager->getLastPage());
  44. }
  45. public function testInitOffset()
  46. {
  47. $this->proxyQuery->expects($this->once())
  48. ->method('execute')
  49. ->with(array(), null)
  50. ->will($this->returnValue(new ArrayCollection(range(0, 12))));
  51. $this->proxyQuery->expects($this->once())
  52. ->method('setMaxResults')
  53. ->with($this->equalTo(21));
  54. // Asserting that the offset will be set correctly
  55. $this->proxyQuery->expects($this->once())
  56. ->method('setFirstResult')
  57. ->with($this->equalTo(10));
  58. $this->pager->setQuery($this->proxyQuery);
  59. $this->pager->setPage(2);
  60. $this->pager->init();
  61. $this->assertSame(3, $this->pager->getLastPage());
  62. }
  63. public function testNoPagesPerConfig()
  64. {
  65. $this->proxyQuery->expects($this->once())
  66. ->method('setMaxResults')
  67. ->with($this->equalTo(0));
  68. $this->proxyQuery->expects($this->once())
  69. ->method('setFirstResult')
  70. ->with($this->equalTo(0));
  71. $this->pager->setQuery($this->proxyQuery);
  72. // Max per page 0 means no pagination
  73. $this->pager->setMaxPerPage(0);
  74. $this->pager->init();
  75. $this->assertSame(0, $this->pager->getLastPage());
  76. }
  77. public function testNoPagesForNoResults()
  78. {
  79. $this->proxyQuery->expects($this->once())
  80. ->method('execute')
  81. ->with(array(), null)
  82. ->will($this->returnValue(array()));
  83. $this->proxyQuery->expects($this->once())
  84. ->method('setMaxResults')
  85. ->with($this->equalTo(21));
  86. $this->proxyQuery->expects($this->once())
  87. ->method('setFirstResult')
  88. ->with($this->equalTo(0));
  89. $this->pager->setQuery($this->proxyQuery);
  90. $this->pager->init();
  91. $this->AssertEquals(0, $this->pager->getLastPage());
  92. }
  93. public function testInitNoQuery()
  94. {
  95. $this->setExpectedException('RuntimeException');
  96. $this->pager->init();
  97. }
  98. }