-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deprecated errors in tests (#575)
* Fix deprecated errors in tests * Safely count all possible types * Support iterable type fully in newer pagerfanta versions * Use psalm 4.30
- Loading branch information
1 parent
8b9d39b
commit 573ca2e
Showing
18 changed files
with
340 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
composer.lock | ||
build | ||
vendor | ||
.phpunit.result.cache | ||
.phpunit.result.cache | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ parameters: | |
reportUnmatchedIgnoredErrors: false | ||
paths: | ||
- src | ||
bootstrapFiles: | ||
- test/phpstan.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the League\Fractal package. | ||
* | ||
* (c) Phil Sturgeon <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace League\Fractal\Pagination; | ||
|
||
use Laminas\Paginator\Paginator; | ||
|
||
/** | ||
* A paginator adapter for laminas/laminas-paginator. | ||
* | ||
* @author Abdul Malik Ikhsan <[email protected]> | ||
*/ | ||
class LaminasPaginatorAdapter implements PaginatorInterface | ||
{ | ||
protected Paginator $paginator; | ||
|
||
/** | ||
* The route generator. | ||
* | ||
* @var callable | ||
*/ | ||
protected $routeGenerator; | ||
|
||
public function __construct(Paginator $paginator, callable $routeGenerator) | ||
{ | ||
$this->paginator = $paginator; | ||
$this->routeGenerator = $routeGenerator; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getCurrentPage(): int | ||
{ | ||
return $this->paginator->getCurrentPageNumber(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getLastPage(): int | ||
{ | ||
return $this->paginator->count(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getTotal(): int | ||
{ | ||
return $this->paginator->getTotalItemCount(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getCount(): int | ||
{ | ||
return $this->paginator->getCurrentItemCount(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPerPage(): int | ||
{ | ||
return $this->paginator->getItemCountPerPage(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getUrl(int $page): string | ||
{ | ||
return call_user_func($this->routeGenerator, $page); | ||
} | ||
|
||
public function getPaginator(): Paginator | ||
{ | ||
return $this->paginator; | ||
} | ||
|
||
public function getRouteGenerator(): callable | ||
{ | ||
return $this->routeGenerator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace League\Fractal\Pagination; | ||
|
||
trait PaginatorCountTrait | ||
{ | ||
/** | ||
* Safely get the count from an iterable | ||
*/ | ||
private function getIterableCount(iterable $iterable): int | ||
{ | ||
if ($iterable instanceof \Traversable) { | ||
return $this->getTraversableCount($iterable); | ||
} | ||
|
||
return count($iterable); | ||
} | ||
|
||
/** | ||
* Safely get the count from a traversable | ||
*/ | ||
private function getTraversableCount(\Traversable $traversable): int | ||
{ | ||
if ($traversable instanceof \Countable) { | ||
return count($traversable); | ||
} | ||
|
||
// Call the "count" method if it exists | ||
if (method_exists($traversable, 'count')) { | ||
return $traversable->count(); | ||
} | ||
|
||
// If not, fall back to iterator_count and rewind if possible | ||
$count = iterator_count($traversable); | ||
if ($traversable instanceof \Iterator || $traversable instanceof \IteratorAggregate) { | ||
$traversable->rewind(); | ||
} | ||
|
||
return $count; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace League\Fractal\Test\Pagination; | ||
|
||
use League\Fractal\Pagination\LaminasPaginatorAdapter; | ||
use Mockery; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class LaminasFrameworkPaginatorAdapterTest extends TestCase | ||
{ | ||
public function testPaginationAdapter() | ||
{ | ||
$items = [ | ||
'Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7', 'Item 8', 'Item 9', 'Item 10', | ||
'Item 11', 'Item 12', 'Item 13', 'Item 14', 'Item 15', 'Item 16', 'Item 17', 'Item 18', 'Item 19', 'Item 20', | ||
'Item 21', 'Item 22', 'Item 23', 'Item 24', 'Item 25', 'Item 26', 'Item 27', 'Item 28', 'Item 29', 'Item 30', | ||
'Item 31', 'Item 32', 'Item 33', 'Item 34', 'Item 35', 'Item 36', 'Item 37', 'Item 38', 'Item 39', 'Item 40', | ||
'Item 41', 'Item 42', 'Item 43', 'Item 44', 'Item 45', 'Item 46', 'Item 47', 'Item 48', 'Item 49', 'Item 50', | ||
]; | ||
|
||
$adapter = Mockery::mock('Laminas\Paginator\Adapter\ArrayAdapter', [$items])->makePartial(); | ||
|
||
$total = 50; | ||
$count = 10; | ||
$perPage = 10; | ||
$currentPage = 2; | ||
$lastPage = 5; | ||
|
||
$paginator = Mockery::mock('Laminas\Paginator\Paginator', [$adapter])->makePartial(); | ||
|
||
$paginator->shouldReceive('getCurrentPageNumber')->andReturn($currentPage); | ||
$paginator->shouldReceive('count')->andReturn($lastPage); | ||
$paginator->shouldReceive('getItemCountPerPage')->andReturn($perPage); | ||
|
||
$adapter = new LaminasPaginatorAdapter($paginator, function ($page) { | ||
return 'http://example.com/foo?page='.$page; | ||
}); | ||
|
||
$this->assertInstanceOf('League\Fractal\Pagination\PaginatorInterface', $adapter); | ||
|
||
$this->assertSame($currentPage, $adapter->getCurrentPage()); | ||
$this->assertSame($lastPage, $adapter->getLastPage()); | ||
$this->assertSame($count, $adapter->getCount()); | ||
$this->assertSame($total, $adapter->getTotal()); | ||
$this->assertSame($perPage, $adapter->getPerPage()); | ||
$this->assertSame('http://example.com/foo?page=1', $adapter->getUrl(1)); | ||
$this->assertSame('http://example.com/foo?page=3', $adapter->getUrl(3)); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
Mockery::close(); | ||
} | ||
} |
Oops, something went wrong.