Skip to content

Commit 2da90be

Browse files
authored
Merge pull request #670 from FabianKoestring/doctrine-paginator-json-serializeable
Have `DoctrinePaginator` implement `JsonSerializable` to improve cache key creation
2 parents f4c096b + e6f94d7 commit 2da90be

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/DoctrineORMModule/Paginator/Adapter/DoctrinePaginator.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace DoctrineORMModule\Paginator\Adapter;
66

77
use Doctrine\ORM\Tools\Pagination\Paginator;
8+
use JsonSerializable;
89
use Laminas\Paginator\Adapter\AdapterInterface;
910

1011
/**
1112
* Paginator adapter for the Laminas\Paginator component
1213
*/
13-
class DoctrinePaginator implements AdapterInterface
14+
class DoctrinePaginator implements AdapterInterface, JsonSerializable
1415
{
1516
/** @var Paginator */
1617
protected $paginator;
@@ -55,4 +56,15 @@ public function count()
5556
{
5657
return $this->paginator->count();
5758
}
59+
60+
/**
61+
* @return array{select: string, count_select: int}
62+
*/
63+
public function jsonSerialize(): array
64+
{
65+
return [
66+
'select' => $this->paginator->getQuery()->getSQL(),
67+
'count_select' => $this->paginator->count(),
68+
];
69+
}
5870
}

test/DoctrineORMModuleTest/Assets/Entity/FormEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class FormEntity
2020
protected $id;
2121

2222
/**
23-
* @ORM\Column(type="bool")
23+
* @ORM\Column(type="boolean")
2424
*
2525
* @var bool
2626
*/

test/DoctrineORMModuleTest/Assets/Entity/Issue237.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* @ORM\Entity
9-
* @ORM\Table(name="doctrine_orm_module_form_entity")
9+
* @ORM\Table(name="doctrine_orm_module_form_entity_issue237")
1010
*/
1111
class Issue237
1212
{

test/DoctrineORMModuleTest/Paginator/AdapterTestIgnore.php renamed to test/DoctrineORMModuleTest/Paginator/AdapterTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
use DoctrineORMModuleTest\Assets\Fixture\TestFixture;
1313
use DoctrineORMModuleTest\Framework\TestCase;
1414

15+
use function class_exists;
1516
use function count;
1617

17-
class AdapterTestIgnore extends TestCase
18+
class AdapterTest extends TestCase
1819
{
1920
/** @var QueryBuilder */
2021
protected $queryBuilder;
@@ -29,6 +30,12 @@ public function setUp(): void
2930
{
3031
parent::setUp();
3132

33+
if (! class_exists(FixtureLoader::class)) {
34+
$this->markTestIncomplete(
35+
'DataFixtures must be installed to run this test.'
36+
);
37+
}
38+
3239
$this->createDb();
3340
$loader = new FixtureLoader();
3441
$loader->addFixture(new TestFixture());
@@ -82,4 +89,14 @@ public function testGetsItemsAtOffsetTen(): void
8289
$this->assertEquals($expectedItem, $actual[$key]);
8390
}
8491
}
92+
93+
public function testJsonSerialize(): void
94+
{
95+
$result = $this->paginatorAdapter->jsonSerialize();
96+
97+
$this->assertArrayHasKey('select', $result);
98+
$this->assertArrayHasKey('count_select', $result);
99+
$this->assertSame($result['count_select'], $this->paginator->count());
100+
$this->assertSame($result['select'], $this->paginator->getQuery()->getSQL());
101+
}
85102
}

0 commit comments

Comments
 (0)