Skip to content

Commit 5d15338

Browse files
Remove count from async persister (#1905)
* — insertPage no longer return int, now return void. — result from insertPage not used anywhere, and it is resulted in useless and expensive query to database. * - updated test. * added annotation for variable. * phpcs
1 parent 612ab17 commit 5d15338

File tree

2 files changed

+2
-53
lines changed

2 files changed

+2
-53
lines changed

src/Persister/AsyncPagerPersister.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function insert(PagerInterface $pager, array $options = []): void
7676
/**
7777
* @phpstan-param TPagerPersisterOptions $options
7878
*/
79-
public function insertPage(int $page, array $options = []): int
79+
public function insertPage(int $page, array $options = []): void
8080
{
8181
if (!isset($options['indexName'])) {
8282
throw new \RuntimeException('Invalid call. $options is missing the indexName key.');
@@ -93,12 +93,8 @@ public function insertPage(int $page, array $options = []): int
9393
$pager->setMaxPerPage($options['max_per_page']);
9494
$pager->setCurrentPage($options['first_page']);
9595

96-
$results = $pager->getCurrentPageResults();
97-
$objectCount = $results instanceof \Traversable ? \iterator_count($results) : \count($results);
98-
96+
/** @var InPlacePagerPersister $pagerPersister */
9997
$pagerPersister = $this->pagerPersisterRegistry->getPagerPersister(InPlacePagerPersister::NAME);
10098
$pagerPersister->insert($pager, $options);
101-
102-
return $objectCount;
10399
}
104100
}

tests/Unit/Persister/AsyncPagerPersisterTest.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@
1313

1414
use FOS\ElasticaBundle\Message\AsyncPersistPage;
1515
use FOS\ElasticaBundle\Persister\AsyncPagerPersister;
16-
use FOS\ElasticaBundle\Persister\InPlacePagerPersister;
17-
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
1816
use FOS\ElasticaBundle\Persister\PagerPersisterInterface;
1917
use FOS\ElasticaBundle\Persister\PagerPersisterRegistry;
20-
use FOS\ElasticaBundle\Persister\PersisterRegistry;
2118
use FOS\ElasticaBundle\Provider\PagerInterface;
22-
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
2319
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
2420
use PHPUnit\Framework\TestCase;
2521
use Symfony\Component\DependencyInjection\ServiceLocator;
26-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2722
use Symfony\Component\Messenger\Envelope;
2823
use Symfony\Component\Messenger\MessageBusInterface;
2924

@@ -56,46 +51,4 @@ function ($message) {
5651
$pager = $this->createMock(PagerInterface::class);
5752
$sut->insert($pager);
5853
}
59-
60-
public function testInsertPageReturnObjectCount()
61-
{
62-
$persistersLocator = $this->createMock(ServiceLocator::class);
63-
$persistersLocator->expects($this->once())->method('has')->with('foo')->willReturn(true);
64-
$persistersLocator->expects($this->once())->method('get')->with('foo')->willReturn($this->createMock(ObjectPersisterInterface::class));
65-
66-
$pagerPersistersLocator = $this->createMock(ServiceLocator::class);
67-
$pagerPersistersLocator->expects($this->once())->method('has')->with('in_place')->willReturn(true);
68-
$pagerPersistersLocator->expects($this->once())->method('get')->with('in_place')->willReturn(
69-
new InPlacePagerPersister(
70-
new PersisterRegistry($persistersLocator),
71-
$this->createMock(EventDispatcherInterface::class)
72-
)
73-
);
74-
75-
$pagerPersisterRegistry = new PagerPersisterRegistry($pagerPersistersLocator);
76-
77-
$pagerMock = $this->createMock(PagerInterface::class);
78-
$pagerMock->expects($this->exactly(2))->method('setMaxPerPage')->with(10);
79-
$pagerMock->method('setCurrentPage')->withConsecutive([1], [1], [0]);
80-
$pagerMock->expects($this->exactly(2))->method('getCurrentPageResults')->willReturn([]);
81-
82-
$provider = $this->createMock(PagerProviderInterface::class);
83-
$provider->expects($this->once())->method('provide')->with([
84-
'first_page' => 1,
85-
'last_page' => 1,
86-
'indexName' => 'foo',
87-
'max_per_page' => 10,
88-
])->willReturn($pagerMock);
89-
90-
$pagerProviderRegistry = $this->createMock(PagerProviderRegistry::class);
91-
$pagerProviderRegistry->expects($this->once())->method('getProvider')->with('foo')->willReturn($provider);
92-
93-
$messageBus = $this->createMock(MessageBusInterface::class);
94-
$sut = new AsyncPagerPersister($pagerPersisterRegistry, $pagerProviderRegistry, $messageBus);
95-
96-
$sut->insertPage(1, [
97-
'indexName' => 'foo',
98-
'max_per_page' => 10,
99-
]);
100-
}
10154
}

0 commit comments

Comments
 (0)