Skip to content

Commit

Permalink
Remove count from async persister (#1905)
Browse files Browse the repository at this point in the history
* — 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
  • Loading branch information
oleg-andreyev committed May 22, 2023
1 parent 612ab17 commit 5d15338
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 53 deletions.
8 changes: 2 additions & 6 deletions src/Persister/AsyncPagerPersister.php
Expand Up @@ -76,7 +76,7 @@ public function insert(PagerInterface $pager, array $options = []): void
/**
* @phpstan-param TPagerPersisterOptions $options
*/
public function insertPage(int $page, array $options = []): int
public function insertPage(int $page, array $options = []): void
{
if (!isset($options['indexName'])) {
throw new \RuntimeException('Invalid call. $options is missing the indexName key.');
Expand All @@ -93,12 +93,8 @@ public function insertPage(int $page, array $options = []): int
$pager->setMaxPerPage($options['max_per_page']);
$pager->setCurrentPage($options['first_page']);

$results = $pager->getCurrentPageResults();
$objectCount = $results instanceof \Traversable ? \iterator_count($results) : \count($results);

/** @var InPlacePagerPersister $pagerPersister */
$pagerPersister = $this->pagerPersisterRegistry->getPagerPersister(InPlacePagerPersister::NAME);
$pagerPersister->insert($pager, $options);

return $objectCount;
}
}
47 changes: 0 additions & 47 deletions tests/Unit/Persister/AsyncPagerPersisterTest.php
Expand Up @@ -13,17 +13,12 @@

use FOS\ElasticaBundle\Message\AsyncPersistPage;
use FOS\ElasticaBundle\Persister\AsyncPagerPersister;
use FOS\ElasticaBundle\Persister\InPlacePagerPersister;
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
use FOS\ElasticaBundle\Persister\PagerPersisterInterface;
use FOS\ElasticaBundle\Persister\PagerPersisterRegistry;
use FOS\ElasticaBundle\Persister\PersisterRegistry;
use FOS\ElasticaBundle\Provider\PagerInterface;
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

Expand Down Expand Up @@ -56,46 +51,4 @@ function ($message) {
$pager = $this->createMock(PagerInterface::class);
$sut->insert($pager);
}

public function testInsertPageReturnObjectCount()
{
$persistersLocator = $this->createMock(ServiceLocator::class);
$persistersLocator->expects($this->once())->method('has')->with('foo')->willReturn(true);
$persistersLocator->expects($this->once())->method('get')->with('foo')->willReturn($this->createMock(ObjectPersisterInterface::class));

$pagerPersistersLocator = $this->createMock(ServiceLocator::class);
$pagerPersistersLocator->expects($this->once())->method('has')->with('in_place')->willReturn(true);
$pagerPersistersLocator->expects($this->once())->method('get')->with('in_place')->willReturn(
new InPlacePagerPersister(
new PersisterRegistry($persistersLocator),
$this->createMock(EventDispatcherInterface::class)
)
);

$pagerPersisterRegistry = new PagerPersisterRegistry($pagerPersistersLocator);

$pagerMock = $this->createMock(PagerInterface::class);
$pagerMock->expects($this->exactly(2))->method('setMaxPerPage')->with(10);
$pagerMock->method('setCurrentPage')->withConsecutive([1], [1], [0]);
$pagerMock->expects($this->exactly(2))->method('getCurrentPageResults')->willReturn([]);

$provider = $this->createMock(PagerProviderInterface::class);
$provider->expects($this->once())->method('provide')->with([
'first_page' => 1,
'last_page' => 1,
'indexName' => 'foo',
'max_per_page' => 10,
])->willReturn($pagerMock);

$pagerProviderRegistry = $this->createMock(PagerProviderRegistry::class);
$pagerProviderRegistry->expects($this->once())->method('getProvider')->with('foo')->willReturn($provider);

$messageBus = $this->createMock(MessageBusInterface::class);
$sut = new AsyncPagerPersister($pagerPersisterRegistry, $pagerProviderRegistry, $messageBus);

$sut->insertPage(1, [
'indexName' => 'foo',
'max_per_page' => 10,
]);
}
}

0 comments on commit 5d15338

Please sign in to comment.