PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Pagination/PaginationPaginatorTest.php

https://gitlab.com/tillkruss/framework
PHP | 268 lines | 217 code | 47 blank | 4 comment | 13 complexity | 40565b774e00e7b1eab44b365414ba19 MD5 | raw file
  1. <?php
  2. use Mockery as m;
  3. use Illuminate\Pagination\UrlWindow;
  4. use Illuminate\Pagination\AbstractPaginator;
  5. use Illuminate\Pagination\LengthAwarePaginator;
  6. use Illuminate\Pagination\Paginator as Paginator;
  7. use Illuminate\Pagination\BootstrapThreePresenter as BootstrapPresenter;
  8. use Illuminate\Pagination\BootstrapFourPresenter as BootstrapFourPresenter;
  9. class PaginationPaginatorTest extends PHPUnit_Framework_TestCase
  10. {
  11. public function tearDown()
  12. {
  13. m::close();
  14. }
  15. public function testPaginatorGetPageName()
  16. {
  17. $p = new LengthAwarePaginator($array = ['item3', 'item4'], 4, 2, 2);
  18. $this->assertEquals('page', $p->getPageName());
  19. $p->setPageName('p');
  20. $this->assertEquals('p', $p->getPageName());
  21. }
  22. public function testPaginatorCanGiveMeRelevantPageInformation()
  23. {
  24. $p = new LengthAwarePaginator($array = ['item3', 'item4'], 4, 2, 2);
  25. $this->assertEquals(2, $p->lastPage());
  26. $this->assertEquals(2, $p->currentPage());
  27. $this->assertTrue($p->hasPages());
  28. $this->assertFalse($p->hasMorePages());
  29. $this->assertEquals(['item3', 'item4'], $p->items());
  30. }
  31. public function testPaginatorCanGenerateUrls()
  32. {
  33. $p = new LengthAwarePaginator($array = ['item1', 'item2', 'item3', 'item4'], 4, 2, 2, ['path' => 'http://website.com/', 'pageName' => 'foo']);
  34. $this->assertEquals('http://website.com?foo=2', $p->url($p->currentPage()));
  35. $this->assertEquals('http://website.com?foo=1', $p->url($p->currentPage() - 1));
  36. $this->assertEquals('http://website.com?foo=1', $p->url($p->currentPage() - 2));
  37. }
  38. public function testPaginatorCanGenerateUrlsWithQuery()
  39. {
  40. $p = new LengthAwarePaginator($array = ['item1', 'item2', 'item3', 'item4'], 4, 2, 2, ['path' => 'http://website.com?sort_by=date', 'pageName' => 'foo']);
  41. $this->assertEquals('http://website.com?sort_by=date&foo=2', $p->url($p->currentPage()));
  42. }
  43. public function testLengthAwarePaginatorCanGenerateUrlsWithoutTrailingSlashes()
  44. {
  45. $p = new LengthAwarePaginator($array = ['item1', 'item2', 'item3', 'item4'], 4, 2, 2, ['path' => 'http://website.com/test/', 'pageName' => 'foo']);
  46. $this->assertEquals('http://website.com/test?foo=2', $p->url($p->currentPage()));
  47. $this->assertEquals('http://website.com/test?foo=1', $p->url($p->currentPage() - 1));
  48. $this->assertEquals('http://website.com/test?foo=1', $p->url($p->currentPage() - 2));
  49. }
  50. public function testPresenterCanDetermineIfThereAreAnyPagesToShow()
  51. {
  52. $p = new LengthAwarePaginator($array = ['item1', 'item2', 'item3', 'item4'], 4, 2, 2);
  53. $window = new UrlWindow($p);
  54. $this->assertTrue($window->hasPages());
  55. }
  56. public function testPresenterCanGetAUrlRangeForASmallNumberOfUrls()
  57. {
  58. $p = new LengthAwarePaginator($array = ['item1', 'item2', 'item3', 'item4'], 4, 2, 2);
  59. $window = new UrlWindow($p);
  60. $this->assertEquals(['first' => [1 => '/?page=1', 2 => '/?page=2'], 'slider' => null, 'last' => null], $window->get());
  61. }
  62. public function testPresenterCanGetAUrlRangeForAWindowOfLinks()
  63. {
  64. $array = [];
  65. for ($i = 1; $i <= 13; $i++) {
  66. $array[$i] = 'item'.$i;
  67. }
  68. $p = new LengthAwarePaginator($array, count($array), 1, 7);
  69. $window = new UrlWindow($p);
  70. $slider = [];
  71. for ($i = 4; $i <= 10; $i++) {
  72. $slider[$i] = '/?page='.$i;
  73. }
  74. $this->assertEquals(['first' => [1 => '/?page=1', 2 => '/?page=2'], 'slider' => $slider, 'last' => [12 => '/?page=12', 13 => '/?page=13']], $window->get());
  75. /*
  76. * Test Being Near The End Of The List
  77. */
  78. $p = new LengthAwarePaginator($array, count($array), 1, 8);
  79. $window = new UrlWindow($p);
  80. $last = [];
  81. for ($i = 5; $i <= 13; $i++) {
  82. $last[$i] = '/?page='.$i;
  83. }
  84. $this->assertEquals(['first' => [1 => '/?page=1', 2 => '/?page=2'], 'slider' => null, 'last' => $last], $window->get());
  85. }
  86. public function testBootstrapPresenterCanGeneratorLinksForSlider()
  87. {
  88. $array = [];
  89. for ($i = 1; $i <= 13; $i++) {
  90. $array[$i] = 'item'.$i;
  91. }
  92. $p = new LengthAwarePaginator($array, count($array), 1, 7);
  93. $presenter = new BootstrapPresenter($p);
  94. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/slider.html')), $presenter->render());
  95. }
  96. public function testBootstrapFourPresenterCanGeneratorLinksForSlider()
  97. {
  98. $array = [];
  99. for ($i = 1; $i <= 13; $i++) {
  100. $array[$i] = 'item'.$i;
  101. }
  102. $p = new LengthAwarePaginator($array, count($array), 1, 7);
  103. $presenter = new BootstrapFourPresenter($p);
  104. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/slider_bs4.html')), $presenter->render());
  105. }
  106. public function testCustomPresenter()
  107. {
  108. $p = new LengthAwarePaginator([], 1, 1, 1);
  109. $presenter = m::mock('StdClass');
  110. AbstractPaginator::presenter(function () use ($presenter) {
  111. return $presenter;
  112. });
  113. $presenter->shouldReceive('render')->andReturn('presenter');
  114. $this->assertEquals('presenter', $p->render());
  115. AbstractPaginator::presenter(function () {
  116. //
  117. });
  118. }
  119. public function testBootstrapPresenterCanGeneratorLinksForTooCloseToBeginning()
  120. {
  121. $array = [];
  122. for ($i = 1; $i <= 13; $i++) {
  123. $array[$i] = 'item'.$i;
  124. }
  125. $p = new LengthAwarePaginator($array, count($array), 1, 2);
  126. $presenter = new BootstrapPresenter($p);
  127. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/beginning.html')), $presenter->render());
  128. }
  129. public function testBootstrapFourPresenterCanGeneratorLinksForTooCloseToBeginning()
  130. {
  131. $array = [];
  132. for ($i = 1; $i <= 13; $i++) {
  133. $array[$i] = 'item'.$i;
  134. }
  135. $p = new LengthAwarePaginator($array, count($array), 1, 2);
  136. $presenter = new BootstrapFourPresenter($p);
  137. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/beginning_bs4.html')), $presenter->render());
  138. }
  139. public function testBootstrapPresenterCanGeneratorLinksForTooCloseToEnding()
  140. {
  141. $array = [];
  142. for ($i = 1; $i <= 13; $i++) {
  143. $array[$i] = 'item'.$i;
  144. }
  145. $p = new LengthAwarePaginator($array, count($array), 1, 12);
  146. $presenter = new BootstrapPresenter($p);
  147. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/ending.html')), $presenter->render());
  148. }
  149. public function testBootstrapFourPresenterCanGeneratorLinksForTooCloseToEnding()
  150. {
  151. $array = [];
  152. for ($i = 1; $i <= 13; $i++) {
  153. $array[$i] = 'item'.$i;
  154. }
  155. $p = new LengthAwarePaginator($array, count($array), 1, 12);
  156. $presenter = new BootstrapFourPresenter($p);
  157. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/ending_bs4.html')), $presenter->render());
  158. }
  159. public function testBootstrapPresenterCanGeneratorLinksForWhenOnLastPage()
  160. {
  161. $array = [];
  162. for ($i = 1; $i <= 13; $i++) {
  163. $array[$i] = 'item'.$i;
  164. }
  165. $p = new LengthAwarePaginator($array, count($array), 1, 13);
  166. $presenter = new BootstrapPresenter($p);
  167. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/last_page.html')), $presenter->render());
  168. }
  169. public function testBootstrapFourPresenterCanGeneratorLinksForWhenOnLastPage()
  170. {
  171. $array = [];
  172. for ($i = 1; $i <= 13; $i++) {
  173. $array[$i] = 'item'.$i;
  174. }
  175. $p = new LengthAwarePaginator($array, count($array), 1, 13);
  176. $presenter = new BootstrapFourPresenter($p);
  177. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/last_page_bs4.html')), $presenter->render());
  178. }
  179. public function testBootstrapPresenterCanGeneratorLinksForWhenOnFirstPage()
  180. {
  181. $array = [];
  182. for ($i = 1; $i <= 13; $i++) {
  183. $array[$i] = 'item'.$i;
  184. }
  185. $p = new LengthAwarePaginator($array, count($array), 1, 1);
  186. $presenter = new BootstrapPresenter($p);
  187. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/first_page.html')), $presenter->render());
  188. }
  189. public function testBootstrapFourPresenterCanGeneratorLinksForWhenOnFirstPage()
  190. {
  191. $array = [];
  192. for ($i = 1; $i <= 13; $i++) {
  193. $array[$i] = 'item'.$i;
  194. }
  195. $p = new LengthAwarePaginator($array, count($array), 1, 1);
  196. $presenter = new BootstrapFourPresenter($p);
  197. $this->assertEquals(trim(file_get_contents(__DIR__.'/fixtures/first_page_bs4.html')), $presenter->render());
  198. }
  199. public function testSimplePaginatorReturnsRelevantContextInformation()
  200. {
  201. $p = new Paginator($array = ['item3', 'item4', 'item5'], 2, 2);
  202. $this->assertEquals(2, $p->currentPage());
  203. $this->assertTrue($p->hasPages());
  204. $this->assertTrue($p->hasMorePages());
  205. $this->assertEquals(['item3', 'item4'], $p->items());
  206. $this->assertEquals([
  207. 'per_page' => 2, 'current_page' => 2, 'next_page_url' => '/?page=3',
  208. 'prev_page_url' => '/?page=1', 'from' => 3, 'to' => 4, 'data' => ['item3', 'item4'],
  209. ], $p->toArray());
  210. }
  211. public function testPaginatorRemovesTrailingSlashes()
  212. {
  213. $p = new Paginator($array = ['item1', 'item2', 'item3'], 2, 2, ['path' => 'http://website.com/test/']);
  214. $this->assertEquals('http://website.com/test?page=1', $p->previousPageUrl());
  215. }
  216. public function testPaginatorGeneratesUrlsWithoutTrailingSlash()
  217. {
  218. $p = new Paginator($array = ['item1', 'item2', 'item3'], 2, 2, ['path' => 'http://website.com/test']);
  219. $this->assertEquals('http://website.com/test?page=1', $p->previousPageUrl());
  220. }
  221. }