├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml ├── src └── DeSmart │ └── Pagination │ ├── Factory.php │ ├── PaginationServiceProvider.php │ └── Paginator.php └── tests ├── .gitkeep ├── FactoryTest.php └── PaginatorTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | *swp 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | 8 | before_script: 9 | - curl -s http://getcomposer.org/installer | php 10 | - php composer.phar install --dev --prefer-source 11 | 12 | script: phpunit 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | [![Build Status](https://api.travis-ci.org/DeSmart/pagination.png)](https://travis-ci.org/DeSmart/pagination) 4 | 5 | This package is an extension for Laravel4 pagination module. 6 | 7 | It provides new functionalities: 8 | 9 | * route based url generator 10 | * helpers for template render 11 | 12 | ## Installation 13 | 14 | To `composer.json` add: `"desmart/pagination": "1.2.*"` and then run `composer update desmart/pagination`. 15 | 16 | In `app/config/app.php` replace line `'Illuminate\Pagination\PaginationServiceProvider',` with `'DeSmart\Pagination\PaginationServiceProvider',`. 17 | 18 | ## Compatibilty 19 | 20 | This package should not break compatibility with Laravel pagination module. 21 | 22 | ### Laravel 4.1 23 | 24 | To use `desmart/pagination` with Laravel 4.1 switch to version `1.1.*`. 25 | 26 | ### Laravel 4.0 27 | 28 | To use `desmart/pagination` with Laravel 4.0 switch to version `1.0.*`. 29 | 30 | ## Method overview 31 | 32 | ### General usage 33 | * `withQuery()` - bind query parameters to url generator (by default query parameters are included). Works only for url generating from routes. 34 | * `withoutQuery()` - don't bind query parameters 35 | * `route($route[, array $parameters])` - use given route for generating url to pages (it can be route name, or instance of `Illuminate\Routing\Route`) 36 | * `useCurrentRoute()` - use current (active) route for url generating 37 | 38 | ### For templates 39 | * `pagesProximity($proximity)` - set pages proximity 40 | * `getPagesRange()` - get list of pages to show in template (includes proximity) 41 | * `canShowFirstPage()` - check if can show first page (returns `TRUE` when first page is not in list generated by `getPagesRange()`) 42 | * `canShowLastPage()` - check if can show last page (returns `TRUE` when last page is not in list generated by `getPagesRange()`) 43 | 44 | ## Example usage 45 | 46 | ### In controller 47 | 48 | ```php 49 | // example route (app/routes.php) 50 | Route::get('/products/{page}.html', array('as' => 'products.list', 'uses' => '')); 51 | 52 | // use the current route 53 | $list = Product::paginate(10) 54 | ->useCurrentRoute() 55 | ->pagesProximity(3); 56 | 57 | // use custom route 58 | $list = Product::paginate(10) 59 | ->route('products.list') 60 | ->pagesProximity(3); 61 | ``` 62 | 63 | ### In view 64 | ```php 65 | // app/view/products/list.blade.php 66 | 67 | @foreach ($list as $item) 68 | {{-- show item --}} 69 | @endforeach 70 | 71 | {{ $list->links('products.paginator') }} 72 | 73 | // app/view/products/paginator.blade.php 74 | 75 | @if ($paginator->getLastPage() > 1) 76 | @foreach ($paginator->getPagesRange() as $page) 77 | {{ $page }} 78 | @endforeach 79 | @endif 80 | ``` 81 | 82 | ## License 83 | 84 | This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 85 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "desmart/pagination", 3 | "description": "Laravel pagination on steroids", 4 | "keywords": ["laravel", "pagination"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Radoslaw Mejer", 9 | "email": "radmen@desmart.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "illuminate/support": "4.2.*", 15 | "illuminate/pagination": "4.2.*", 16 | "illuminate/routing": "4.2.*" 17 | }, 18 | "autoload": { 19 | "psr-0": { 20 | "DeSmart\\Pagination": "src/" 21 | } 22 | }, 23 | "minimum-stability": "dev", 24 | "require-dev": { 25 | "mockery/mockery": "0.8.*" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/DeSmart/Pagination/Factory.php: -------------------------------------------------------------------------------- 1 | urlGenerator = $generator; 24 | } 25 | 26 | /** 27 | * @param \Illuminate\Routing\Router $router 28 | */ 29 | public function setRouter(Router $router) { 30 | $this->router = $router; 31 | } 32 | 33 | /** 34 | * Get a new paginator instance. 35 | * 36 | * @param array $items 37 | * @param integer $total 38 | * @param integer $perPage 39 | * @return \DeSmart\Pagination\Paginator 40 | */ 41 | public function make(array $items, $total, $perPage = null) { 42 | $paginator = new Paginator($this, $items, $total, $perPage); 43 | $paginator->setUrlGenerator($this->urlGenerator); 44 | $paginator->setRouter($this->router); 45 | 46 | return $paginator->setupPaginationContext(); 47 | } 48 | 49 | /** 50 | * Get the number of current page. 51 | * 52 | * @return integer 53 | */ 54 | public function getCurrentPage() { 55 | $page = (int) $this->currentPage; 56 | 57 | if(true === empty($page)) { 58 | $page = $this->router->current() 59 | ->parameter($this->pageName, null); 60 | } 61 | 62 | if(null === $page) { 63 | $page = $this->request->query->get($this->pageName, 1); 64 | } 65 | 66 | if ($page < 1 || false === filter_var($page, FILTER_VALIDATE_INT)) { 67 | return 1; 68 | } 69 | 70 | return $page; 71 | } 72 | 73 | /** 74 | * Get the pagination view. 75 | * 76 | * @param \Illuminate\Pagination\Paginator $paginator 77 | * @param string $view view name 78 | * @return \Illuminate\View\View 79 | */ 80 | public function getPaginationView(\Illuminate\Pagination\Paginator $paginator, $view = null) { 81 | $data = array('environment' => $this, 'paginator' => $paginator); 82 | 83 | return $this->view->make($view ?: $this->getViewName(), $data); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/DeSmart/Pagination/PaginationServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['paginator'] = $this->app->share(function($app) { 21 | $paginator = new Factory($app['request'], $app['view'], $app['translator']); 22 | $paginator->setViewName($app['config']['view.pagination']); 23 | $paginator->setUrlGenerator($app['url']); 24 | $paginator->setRouter($app['router']); 25 | 26 | return $paginator; 27 | }); 28 | } 29 | 30 | /** 31 | * Get the services provided by the provider. 32 | * 33 | * @return array 34 | */ 35 | public function provides() { 36 | return array('paginator'); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/DeSmart/Pagination/Paginator.php: -------------------------------------------------------------------------------- 1 | urlGenerator = $generator; 51 | } 52 | 53 | /** 54 | * @param \Illuminate\Routing\Router $router 55 | */ 56 | public function setRouter(Router $router) { 57 | $this->router = $router; 58 | } 59 | 60 | /** 61 | * Pass to route query data 62 | * 63 | * @return \DeSmart\Pagination\Paginator 64 | */ 65 | public function withQuery() { 66 | $this->withQuery = true; 67 | 68 | return $this; 69 | } 70 | 71 | /** 72 | * Don't pass query data to generated route 73 | * 74 | * @return \DeSmart\Pagination\Paginator 75 | */ 76 | public function withoutQuery() { 77 | $this->withQuery = false; 78 | 79 | return $this; 80 | } 81 | 82 | /** 83 | * Set pages range proximity 84 | * 85 | * @param integer $proximity 86 | * @return \DeSmart\Pagination\Paginator 87 | */ 88 | public function pagesProximity($proximity) { 89 | $this->pagesProximity = $proximity; 90 | $this->pagesRange = null; 91 | 92 | return $this; 93 | } 94 | 95 | /** 96 | * Bind route to generated pagination links 97 | * 98 | * @param \Illuminate\Routing\Route|string $route if string route with given name will be used 99 | * @param array $parameters 100 | * @param bool $absolute 101 | * @return \DeSmart\Pagination\Paginator 102 | */ 103 | public function route($route, array $parameters = array(), $absolute = true) { 104 | $instance = null; 105 | $name = $route; 106 | 107 | if(true === is_object($route) && $route instanceof Route) { 108 | $instance = $route; 109 | $name = null; 110 | } 111 | 112 | $this->routeConfig = compact('instance', 'name', 'parameters', 'absolute'); 113 | 114 | return $this; 115 | } 116 | 117 | /** 118 | * Use current route for generating url 119 | * 120 | * @TODO $route->parameters() can throw Exception if it has no parameters defined 121 | * it should be handled, but Laravel UrlGenerator can't generate urls with extra params 122 | * so maybe it's better to leave it that way. 123 | * @return \DeSmart\Pagination\Paginator 124 | */ 125 | public function useCurrentRoute() { 126 | $route = $this->router->current(); 127 | 128 | return $this->route($route, $route->parameters()); 129 | } 130 | 131 | /** 132 | * Get a URL for a given page number. 133 | * 134 | * @param integer $page 135 | * @return string 136 | */ 137 | public function getUrl($page) { 138 | 139 | if(null === $this->routeConfig) { 140 | return parent::getUrl($page); 141 | } 142 | 143 | $parameters = $this->routeConfig['parameters']; 144 | 145 | if(true === $this->withQuery) { 146 | $parameters = array_merge($parameters, $this->factory->getRequest()->query()); 147 | } 148 | 149 | $parameters[$this->factory->getPageName()] = $page; 150 | $absolute = (null === $this->routeConfig['absolute']) ? true : $this->routeConfig['absolute']; 151 | 152 | // allow adding hash fragments to url 153 | $fragment = $this->buildFragment(); 154 | $generated_route = $this->urlGenerator->route($this->routeConfig['name'], $parameters, $absolute, $this->routeConfig['instance']); 155 | 156 | return $generated_route.$fragment; 157 | } 158 | 159 | /** 160 | * Get pages range to be shown in template 161 | * 162 | * @return array 163 | */ 164 | public function getPagesRange() { 165 | 166 | if(null !== $this->pagesRange) { 167 | return $this->pagesRange; 168 | } 169 | 170 | if(null === $this->pagesProximity) { 171 | $this->pagesRange = range(1, $this->getLastPage()); 172 | } 173 | else { 174 | $this->pagesRange = $this->calculatePagesRange(); 175 | } 176 | 177 | return $this->pagesRange; 178 | } 179 | 180 | /** 181 | * Calculate pages range for given proximity 182 | * 183 | * @return array 184 | */ 185 | protected function calculatePagesRange() { 186 | $current_page = $this->getCurrentPage(); 187 | $last_page = $this->getLastPage(); 188 | $start = $current_page - $this->pagesProximity; 189 | $end = $current_page + $this->pagesProximity; 190 | 191 | if($start < 1) { 192 | $offset = 1 - $start; 193 | $start += $offset; 194 | $end += $offset; 195 | } 196 | else if($end > $last_page) { 197 | $offset = $end - $last_page; 198 | $start -= $offset; 199 | $end -= $offset; 200 | } 201 | 202 | if($start < 1) { 203 | $start = 1; 204 | } 205 | 206 | if($end > $last_page) { 207 | $end = $last_page; 208 | } 209 | 210 | return range($start, $end); 211 | } 212 | 213 | /** 214 | * Check if can show first page in template 215 | * 216 | * @return boolean 217 | */ 218 | public function canShowFirstPage() { 219 | return false === array_search(1, $this->getPagesRange()); 220 | } 221 | 222 | /** 223 | * Check if can show last page in template 224 | * 225 | * @return boolean 226 | */ 227 | public function canShowLastPage() { 228 | return false === array_search($this->getLastPage(), $this->getPagesRange()); 229 | } 230 | 231 | } 232 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeSmart/pagination/f193faec9e8da3e915152ebcb6c67328e6b1617c/tests/.gitkeep -------------------------------------------------------------------------------- /tests/FactoryTest.php: -------------------------------------------------------------------------------- 1 | allowMockingNonExistentMethods(false); 9 | } 10 | 11 | public function tearDown() { 12 | m::close(); 13 | } 14 | 15 | public function testCreationOfFactory() { 16 | $env = $this->getFactory(); 17 | } 18 | 19 | public function testPaginatorCanBeCreated() { 20 | $env = $this->getFactory(); 21 | $request = Illuminate\Http\Request::create('http://foo.com', 'GET'); 22 | $env->setRequest($request); 23 | 24 | $this->assertInstanceOf('DeSmart\Pagination\Paginator', $env->make(array('foo', 'bar'), 2, 2)); 25 | } 26 | 27 | public function testCurrentPageCanBeRetrieved() { 28 | $env = $this->getFactory(); 29 | $request = Illuminate\Http\Request::create('http://foo.com?page=2', 'GET'); 30 | $env->setRequest($request); 31 | 32 | $this->assertEquals(2, $env->getCurrentPage()); 33 | 34 | $env = $this->getFactory(); 35 | $request = Illuminate\Http\Request::create('http://foo.com?page=-1', 'GET'); 36 | $env->setRequest($request); 37 | 38 | $this->assertEquals(1, $env->getCurrentPage()); 39 | } 40 | 41 | public function testCurrentUrlCanBeRetrievedFromRoute() { 42 | $route = m::mock('Illuminate\Routing\Route'); 43 | $route->shouldReceive('parameter')->with('page', null)->andReturn(2); 44 | $router = m::mock('Illuminate\Routing\Router'); 45 | $router->shouldReceive('current')->andReturn($route); 46 | 47 | $env = $this->getFactory($router); 48 | $request = Illuminate\Http\Request::create('http://foo.com', 'GET'); 49 | $env->setRequest($request); 50 | 51 | $this->assertEquals(2, $env->getCurrentPage()); 52 | } 53 | 54 | public function testSettingCurrentUrlOverrulesRequest() { 55 | $env = $this->getFactory(); 56 | $request = Illuminate\Http\Request::create('http://foo.com?page=2', 'GET'); 57 | $env->setRequest($request); 58 | $env->setCurrentPage(3); 59 | 60 | $this->assertEquals(3, $env->getCurrentPage()); 61 | } 62 | 63 | public function testPaginationViewCanBeCreated() { 64 | $env = $this->getFactory(); 65 | $paginator = m::mock('DeSmart\Pagination\Paginator'); 66 | $env->getViewFactory()->shouldReceive('make')->once()->with('pagination::slider', array('environment' => $env, 'paginator' => $paginator))->andReturn('foo'); 67 | 68 | $this->assertEquals('foo', $env->getPaginationView($paginator)); 69 | } 70 | 71 | public function testPaginationWithCustomViewCanBeCreated() { 72 | $env = $this->getFactory(); 73 | $paginator = m::mock('DeSmart\Pagination\Paginator'); 74 | $env->getViewFactory()->shouldReceive('make')->once()->with($view = 'foo.test', array('environment' => $env, 'paginator' => $paginator))->andReturn('foo'); 75 | 76 | $this->assertEquals('foo', $env->getPaginationView($paginator, $view)); 77 | } 78 | 79 | protected function getFactory($router = null, $urlGenerator = null) { 80 | $request = m::mock('Illuminate\Http\Request'); 81 | $view = m::mock('Illuminate\View\Factory'); 82 | $trans = m::mock('Symfony\Component\Translation\TranslatorInterface'); 83 | $view->shouldReceive('addNamespace')->once()->with('pagination', realpath(__DIR__.'/../vendor/illuminate/pagination/Illuminate/Pagination').'/views'); 84 | 85 | $env = new Factory($request, $view, $trans, 'page'); 86 | 87 | if(null === $router) { 88 | $route = m::mock('Illuminate\Routing\Route'); 89 | $route->shouldReceive('parameter')->with('page', null)->andReturn(null); 90 | $router = m::mock('Illuminate\Routing\Router'); 91 | $router->shouldReceive('current')->andReturn($route); 92 | } 93 | 94 | if(null === $urlGenerator) { 95 | $urlGenerator = m::mock('Illuminate\Routing\UrlGenerator'); 96 | } 97 | 98 | $env->setRouter($router); 99 | $env->setUrlGenerator($urlGenerator); 100 | 101 | return $env; 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /tests/PaginatorTest.php: -------------------------------------------------------------------------------- 1 | allowMockingNonExistentMethods(false); 10 | } 11 | 12 | public function tearDown() { 13 | m::close(); 14 | } 15 | 16 | public function testGetUrlProperlyFormatsUrl() { 17 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 18 | $env->shouldReceive('getCurrentUrl')->twice()->andReturn('http://foo.com'); 19 | $env->shouldReceive('getPageName')->atLeast(2)->andReturn('page'); 20 | 21 | $this->assertEquals('http://foo.com?page=1', $p->getUrl(1)); 22 | $p->addQuery('foo', 'bar'); 23 | $this->assertEquals('http://foo.com?foo=bar&page=1', $p->getUrl(1)); 24 | } 25 | 26 | public function testGetUrlFromRoute() { 27 | $generator = m::mock('Illuminate\Routing\UrlGenerator'); 28 | $request = m::mock('Illuminate\Http\Request'); 29 | $request->shouldReceive('query')->once()->andReturn($query = array('a' => 1)); 30 | 31 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 32 | $p->setUrlGenerator($generator); 33 | $env->shouldReceive('getRequest')->once()->andReturn($request); 34 | 35 | $env->shouldReceive('getPageName')->once()->andReturn('page'); 36 | $generator->shouldReceive('route')->once()->with($name = 'test.route', array('a' => 1, 'page' => 1), true, null); 37 | $p->route($name); 38 | 39 | $p->getUrl(1); 40 | } 41 | 42 | public function testGetUrlFromRouteWithoutQuery() { 43 | $generator = m::mock('Illuminate\Routing\UrlGenerator'); 44 | 45 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 46 | $p->setUrlGenerator($generator); 47 | $p->withoutQuery(); 48 | $env->shouldReceive('getRequest')->never(); 49 | $env->shouldReceive('getPageName')->andReturn('page'); 50 | 51 | $generator->shouldReceive('route')->once()->with($name = 'test.route', array('page' => 1), true, null); 52 | $p->route($name); 53 | 54 | $p->getUrl(1); 55 | } 56 | 57 | public function testGetUrlFromRouteWithGeneratorArguments() { 58 | $generator = m::mock('Illuminate\Routing\UrlGenerator'); 59 | $params = array('b' => 'foo'); 60 | 61 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 62 | $p->setUrlGenerator($generator); 63 | $p->withoutQuery(); 64 | $env->shouldReceive('getPageName')->andReturn('page'); 65 | 66 | $generator->shouldReceive('route')->once()->with($name = 'test.route', array_merge($params, array('page' => 1)), $absolute = false, null); 67 | $p->route($name, $params, $absolute); 68 | 69 | $p->getUrl(1); 70 | } 71 | 72 | public function testGetUrlFromCurrentRoute() { 73 | $generator = m::mock('Illuminate\Routing\UrlGenerator'); 74 | $route = m::mock('Illuminate\Routing\Route'); 75 | $route->shouldReceive('parameters')->once()->andReturn($params = array('b' => 'foo')); 76 | $router = m::mock('Illuminate\Routing\Router'); 77 | $router->shouldReceive('current')->once()->andReturn($route); 78 | 79 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 80 | $p->setUrlGenerator($generator); 81 | $p->setRouter($router); 82 | $p->withoutQuery(); 83 | $p->useCurrentRoute(); 84 | $env->shouldReceive('getPageName')->andReturn('page'); 85 | 86 | $generator->shouldReceive('route')->once()->with(null, array_merge($params, array('page' => 1)), true, $route); 87 | 88 | $p->getUrl(1); 89 | } 90 | 91 | public function testPaginatorIsCountable() { 92 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 93 | 94 | $this->assertEquals(3, count($p)); 95 | } 96 | 97 | public function testPaginatorIsIterable() { 98 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 99 | 100 | $this->assertInstanceOf('ArrayIterator', $p->getIterator()); 101 | $this->assertEquals(array('foo', 'bar', 'baz'), $p->getIterator()->getArrayCopy()); 102 | } 103 | 104 | public function testGetLinksCallsFactoryProperly() { 105 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 106 | $env->shouldReceive('getPaginationView')->once()->with($p, null)->andReturn('foo'); 107 | 108 | $this->assertEquals('foo', $p->links()); 109 | 110 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 111 | $env->shouldReceive('getPaginationView')->once()->with($p, $view = 'foo'); 112 | 113 | $p->links($view); 114 | } 115 | 116 | public function testGetPagesRangeReturnsValidRangeWithoutProximity() { 117 | $p = m::mock('DeSmart\Pagination\Paginator[getLastPage]'); 118 | $p->shouldReceive('getLastPage')->once()->andReturn($last_page = 10); 119 | 120 | $this->assertEquals(range(1, $last_page), $p->getPagesRange()); 121 | } 122 | 123 | public function testGetPagesRangeReturnsValidRangeWithProximity() { 124 | $p = m::mock('DeSmart\Pagination\Paginator[getLastPage,getCurrentPage]'); 125 | $p->shouldReceive('getLastPage')->andReturn($last_page = 10); 126 | $p->shouldReceive('getCurrentPage')->andReturn(5, 5, 1, $last_page); 127 | 128 | $p->pagesProximity(2); 129 | $this->assertEquals(array(3, 4, 5, 6, 7), $p->getPagesRange()); 130 | 131 | $p->pagesProximity(10); 132 | $this->assertEquals(range(1, $last_page), $p->getPagesRange()); 133 | 134 | $p->pagesProximity(2); 135 | $this->assertEquals(array(1, 2, 3, 4, 5), $p->getPagesRange()); 136 | 137 | $p->pagesProximity(2); 138 | $this->assertEquals(array(6, 7, 8, 9, 10), $p->getPagesRange()); 139 | } 140 | 141 | public function testGetPagesRangeWithProximityForShortPagesRange() { 142 | $p = m::mock('DeSmart\Pagination\Paginator[getLastPage,getCurrentPage]'); 143 | $p->shouldReceive('getLastPage')->andReturn($last_page = 2); 144 | $p->shouldReceive('getCurrentPage')->andReturn(1, 2); 145 | 146 | $p->pagesProximity(4); 147 | $this->assertEquals(array(1, 2), $p->getPagesRange()); 148 | 149 | $p->pagesProximity(4); 150 | $this->assertEquals(array(1, 2), $p->getPagesRange()); 151 | 152 | $p->pagesProximity(1); 153 | $this->assertEquals(array(1, 2), $p->getPagesRange()); 154 | } 155 | 156 | public function testCanShowFirstPage() { 157 | $p = m::mock('DeSmart\Pagination\Paginator[getPagesRange]'); 158 | $p->shouldReceive('getPagesRange')->andReturn(array(1, 2, 3), array(2, 3, 4), array(5, 6, 7)); 159 | 160 | $this->assertFalse($p->canShowFirstPage()); 161 | $this->assertTrue($p->canShowFirstPage()); 162 | $this->assertTrue($p->canShowFirstPage()); 163 | } 164 | 165 | public function testCanShowLastPage() { 166 | $p = m::mock('DeSmart\Pagination\Paginator[getPagesRange,getLastPage]'); 167 | $p->shouldReceive('getPagesRange')->andReturn(array(1, 2, 3), array(2, 3, 4), array(5, 6, 7)); 168 | $p->shouldReceive('getLastPage')->andReturn(7); 169 | 170 | $this->assertTrue($p->canShowLastPage()); 171 | $this->assertTrue($p->canShowLastPage()); 172 | $this->assertFalse($p->canShowLastPage()); 173 | } 174 | 175 | public function testCanAddFragmentWithRouteConfig() { 176 | $generator = m::mock('Illuminate\Routing\UrlGenerator'); 177 | $p = new Paginator($env = m::mock('DeSmart\Pagination\Factory'), array('foo', 'bar', 'baz'), 3, 2); 178 | $p->fragment('test'); 179 | $p->setUrlGenerator($generator); 180 | $p->withoutQuery(); 181 | $env->shouldReceive('getRequest')->never(); 182 | $env->shouldReceive('getPageName')->andReturn('page'); 183 | $generator->shouldReceive('route')->once()->with($name = 'test.route', array('page' => 1), true, null); 184 | $p->route($name); 185 | $this->assertEquals('#test', $p->getUrl(1)); 186 | } 187 | 188 | } 189 | --------------------------------------------------------------------------------