Skip to content

Commit

Permalink
Fix deprecated errors in tests (#575)
Browse files Browse the repository at this point in the history
* 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
KorvinSzanto authored Feb 14, 2025
1 parent 8b9d39b commit 573ca2e
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-20.04]
php: [7.4, 8.0, 8.1]
php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4]

name: League - PHP ${{ matrix.php }} on ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
php-version: 8.1
extensions: apcu, redis
coverage: none
tools: vimeo/psalm:4.22.0
tools: vimeo/psalm:4.30.0

- name: Download dependencies
uses: ramsey/composer-install@v1
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
"require-dev": {
"doctrine/orm": "^2.5",
"illuminate/contracts": "~5.0",
"laminas/laminas-paginator": "~2.12",
"mockery/mockery": "^1.3",
"pagerfanta/pagerfanta": "~1.0.0",
"pagerfanta/pagerfanta": "~1.0.0|~4.0.0",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "~3.4",
"vimeo/psalm": "^4.22",
"zendframework/zend-paginator": "~2.3"
"vimeo/psalm": "^4.30"
},
"suggest": {
"illuminate/pagination": "The Illuminate Pagination component.",
"pagerfanta/pagerfanta": "Pagerfanta Paginator",
"zendframework/zend-paginator": "Zend Framework Paginator"
"laminas/laminas-paginator": "Laminas Framework Paginator"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ parameters:
reportUnmatchedIgnoredErrors: false
paths:
- src
bootstrapFiles:
- test/phpstan.php
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
errorBaseline="psalm.baseline.xml"
>
<projectFiles>
<file name="test/phpstan.php" />
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
Expand Down
4 changes: 2 additions & 2 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Manager
*/
private ScopeFactoryInterface $scopeFactory;

public function __construct(ScopeFactoryInterface $scopeFactory = null)
public function __construct(?ScopeFactoryInterface $scopeFactory = null)
{
$this->scopeFactory = $scopeFactory ?: new ScopeFactory();
}
Expand All @@ -72,7 +72,7 @@ public function __construct(ScopeFactoryInterface $scopeFactory = null)
public function createData(
ResourceInterface $resource,
?string $scopeIdentifier = null,
Scope $parentScopeInstance = null
?Scope $parentScopeInstance = null
): Scope {
if ($parentScopeInstance !== null) {
return $this->scopeFactory->createChildScopeFor($this, $parentScopeInstance, $resource, $scopeIdentifier);
Expand Down
6 changes: 4 additions & 2 deletions src/Pagination/DoctrinePaginatorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class DoctrinePaginatorAdapter implements PaginatorInterface
{
use PaginatorCountTrait;

/**
* The paginator instance.
* @var Paginator
Expand Down Expand Up @@ -73,7 +75,7 @@ public function getTotal(): int
*/
public function getCount(): int
{
return $this->paginator->getIterator()->count();
return $this->getTraversableCount($this->paginator->getIterator());
}

/**
Expand All @@ -93,7 +95,7 @@ public function getUrl(int $page): string
}

/**
* Get the the route generator.
* Get the route generator.
*/
private function getRouteGenerator(): callable
{
Expand Down
95 changes: 95 additions & 0 deletions src/Pagination/LaminasPaginatorAdapter.php
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;
}
}
4 changes: 3 additions & 1 deletion src/Pagination/PagerfantaPaginatorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class PagerfantaPaginatorAdapter implements PaginatorInterface
{
use PaginatorCountTrait;

protected Pagerfanta $paginator;

/**
Expand Down Expand Up @@ -64,7 +66,7 @@ public function getTotal(): int
*/
public function getCount(): int
{
return count($this->paginator->getCurrentPageResults());
return $this->getIterableCount($this->paginator->getCurrentPageResults());
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Pagination/PaginatorCountTrait.php
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;
}

}
20 changes: 17 additions & 3 deletions test/Pagination/DoctrinePaginatorAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
namespace League\Fractal\Test\Pagination;

use Doctrine\ORM\Query;
use Doctrine\ORM\Tools\Pagination\Paginator;
use League\Fractal\Pagination\DoctrinePaginatorAdapter;
use League\Fractal\Test\Stub\SimpleTraversable;
use Mockery;
use PHPUnit\Framework\TestCase;

Expand All @@ -28,11 +30,9 @@ public function testPaginationAdapter()
$paginator->shouldReceive('getQuery')->andReturn($query);

//Mock the iterator of the paginator
$iterator = Mockery::mock('IteratorAggregate');
$iterator->shouldReceive('count')->andReturn($count);
$iterator = new \ArrayIterator(range(1, $count));
$paginator->shouldReceive('getIterator')->andReturn($iterator);


$adapter = new DoctrinePaginatorAdapter($paginator, function ($page) {
return 'http://example.com/foo?page='.$page;
});
Expand All @@ -57,6 +57,20 @@ public function testPaginationAdapter()
);
}

public function testCountingTraversables()
{
$traversable = new SimpleTraversable(range(1, 100));
$adapter = Mockery::mock('Doctrine\ORM\Tools\Pagination\Paginator');
$adapter->shouldReceive('getIterator')->andReturn($traversable);
$adapter = new DoctrinePaginatorAdapter($adapter, function ($page) {
return (string) $page;
});

$this->assertEquals($traversable->key(), 0);
$this->assertEquals($adapter->getCount(), 100);
$this->assertEquals($traversable->key(), 0);
}

public function tearDown(): void
{
Mockery::close();
Expand Down
53 changes: 53 additions & 0 deletions test/Pagination/LaminasFrameworkPaginatorAdapterTest.php
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();
}
}
Loading

0 comments on commit 573ca2e

Please sign in to comment.