Skip to content

Commit

Permalink
Merge pull request #1143 from Simperfit/hotfix/add-an-exception-for-i…
Browse files Browse the repository at this point in the history
…tem-per-page-2.0

hotfix: add an exception for item per page equal 0
  • Loading branch information
Simperfit committed May 29, 2017
2 parents 7fb10f1 + 36b8049 commit 25cb52b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryChecker;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use Doctrine\Common\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -80,6 +81,10 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$itemsPerPage = (null !== $this->maximumItemPerPage && $itemsPerPage >= $this->maximumItemPerPage ? $this->maximumItemPerPage : $itemsPerPage);
}

if (0 >= $itemsPerPage) {
throw new InvalidArgumentException('Item per page parameter should not be less than or equal to 0');
}

$queryBuilder
->setFirstResult(($request->query->get($this->pageParameterName, 1) - 1) * $itemsPerPage)
->setMaxResults($itemsPerPage);
Expand Down
72 changes: 72 additions & 0 deletions tests/Bridge/Doctrine/Orm/Extension/PaginationExtensionTest.php
Expand Up @@ -61,6 +61,78 @@ public function testApplyToCollection()
$extension->applyToCollection($queryBuilder, new QueryNameGenerator(), 'Foo', 'op');
}

/**
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
* @expectedExceptionMessage Item per page parameter should not be less than or equal to 0
*/
public function testApplyToCollectionWithItemPerPageZero()
{
$requestStack = new RequestStack();
$requestStack->push(new Request(['pagination' => true, 'itemsPerPage' => 0, '_page' => 2]));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$attributes = [
'pagination_enabled' => true,
'pagination_client_enabled' => true,
'pagination_items_per_page' => 0,
];
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata(null, null, null, [], [], $attributes))->shouldBeCalled();
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(40)->willReturn($queryBuilderProphecy)->shouldNotBeCalled();
$queryBuilderProphecy->setMaxResults(40)->shouldNotBeCalled();
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
$this->prophesize(ManagerRegistry::class)->reveal(),
$requestStack,
$resourceMetadataFactory,
true,
false,
false,
0,
'_page'
);
$extension->applyToCollection($queryBuilder, new QueryNameGenerator(), 'Foo', 'op');
}

/**
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
* @expectedExceptionMessage Item per page parameter should not be less than or equal to 0
*/
public function testApplyToCollectionWithItemPerPageLessThen0()
{
$requestStack = new RequestStack();
$requestStack->push(new Request(['pagination' => true, 'itemsPerPage' => -20, '_page' => 2]));

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$attributes = [
'pagination_enabled' => true,
'pagination_client_enabled' => true,
'pagination_items_per_page' => 0,
];
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata(null, null, null, [], [], $attributes))->shouldBeCalled();
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(40)->willReturn($queryBuilderProphecy)->shouldNotBeCalled();
$queryBuilderProphecy->setMaxResults(40)->shouldNotBeCalled();
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
$this->prophesize(ManagerRegistry::class)->reveal(),
$requestStack,
$resourceMetadataFactory,
true,
false,
false,
-20,
'_page'
);
$extension->applyToCollection($queryBuilder, new QueryNameGenerator(), 'Foo', 'op');
}

public function testApplyToCollectionWithItemPerPageTooHigh()
{
$requestStack = new RequestStack();
Expand Down

0 comments on commit 25cb52b

Please sign in to comment.