Skip to content

Commit

Permalink
Merge pull request #670 from FabianKoestring/doctrine-paginator-json-…
Browse files Browse the repository at this point in the history
…serializeable

Have `DoctrinePaginator` implement `JsonSerializable` to improve cache key creation
  • Loading branch information
driehle authored Oct 18, 2021
2 parents f4c096b + e6f94d7 commit 2da90be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/DoctrineORMModule/Paginator/Adapter/DoctrinePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace DoctrineORMModule\Paginator\Adapter;

use Doctrine\ORM\Tools\Pagination\Paginator;
use JsonSerializable;
use Laminas\Paginator\Adapter\AdapterInterface;

/**
* Paginator adapter for the Laminas\Paginator component
*/
class DoctrinePaginator implements AdapterInterface
class DoctrinePaginator implements AdapterInterface, JsonSerializable
{
/** @var Paginator */
protected $paginator;
Expand Down Expand Up @@ -55,4 +56,15 @@ public function count()
{
return $this->paginator->count();
}

/**
* @return array{select: string, count_select: int}
*/
public function jsonSerialize(): array
{
return [
'select' => $this->paginator->getQuery()->getSQL(),
'count_select' => $this->paginator->count(),
];
}
}
2 changes: 1 addition & 1 deletion test/DoctrineORMModuleTest/Assets/Entity/FormEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FormEntity
protected $id;

/**
* @ORM\Column(type="bool")
* @ORM\Column(type="boolean")
*
* @var bool
*/
Expand Down
2 changes: 1 addition & 1 deletion test/DoctrineORMModuleTest/Assets/Entity/Issue237.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* @ORM\Entity
* @ORM\Table(name="doctrine_orm_module_form_entity")
* @ORM\Table(name="doctrine_orm_module_form_entity_issue237")
*/
class Issue237
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
use DoctrineORMModuleTest\Assets\Fixture\TestFixture;
use DoctrineORMModuleTest\Framework\TestCase;

use function class_exists;
use function count;

class AdapterTestIgnore extends TestCase
class AdapterTest extends TestCase
{
/** @var QueryBuilder */
protected $queryBuilder;
Expand All @@ -29,6 +30,12 @@ public function setUp(): void
{
parent::setUp();

if (! class_exists(FixtureLoader::class)) {
$this->markTestIncomplete(
'DataFixtures must be installed to run this test.'
);
}

$this->createDb();
$loader = new FixtureLoader();
$loader->addFixture(new TestFixture());
Expand Down Expand Up @@ -82,4 +89,14 @@ public function testGetsItemsAtOffsetTen(): void
$this->assertEquals($expectedItem, $actual[$key]);
}
}

public function testJsonSerialize(): void
{
$result = $this->paginatorAdapter->jsonSerialize();

$this->assertArrayHasKey('select', $result);
$this->assertArrayHasKey('count_select', $result);
$this->assertSame($result['count_select'], $this->paginator->count());
$this->assertSame($result['select'], $this->paginator->getQuery()->getSQL());
}
}

0 comments on commit 2da90be

Please sign in to comment.