SimplePagerTest.php 3.5 KB

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