From 7f6e1799a7123bd73f735c5cda45ed7317631eaf Mon Sep 17 00:00:00 2001 From: Romain Norberg Date: Wed, 19 Jun 2019 13:36:13 +0200 Subject: [PATCH] [ElasticaAdapter] use count() instead search() --- src/Pagerfanta/Adapter/ElasticaAdapter.php | 2 +- .../Tests/Adapter/ElasticaAdapterTest.php | 42 ++++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/Pagerfanta/Adapter/ElasticaAdapter.php b/src/Pagerfanta/Adapter/ElasticaAdapter.php index d974b7c4..df9c1d0a 100644 --- a/src/Pagerfanta/Adapter/ElasticaAdapter.php +++ b/src/Pagerfanta/Adapter/ElasticaAdapter.php @@ -60,7 +60,7 @@ public function __construct(SearchableInterface $searchable, Query $query, array public function getNbResults() { if (!$this->resultSet) { - $totalHits = $this->searchable->search($this->query, $this->options)->getTotalHits(); + $totalHits = $this->searchable->count($this->query); } else { $totalHits = $this->resultSet->getTotalHits(); } diff --git a/tests/Pagerfanta/Tests/Adapter/ElasticaAdapterTest.php b/tests/Pagerfanta/Tests/Adapter/ElasticaAdapterTest.php index f0302ae5..0559eb1a 100644 --- a/tests/Pagerfanta/Tests/Adapter/ElasticaAdapterTest.php +++ b/tests/Pagerfanta/Tests/Adapter/ElasticaAdapterTest.php @@ -2,8 +2,6 @@ namespace Pagerfanta\Tests\Adapter; -use Elastica\Response; -use Elastica\ResultSet; use Pagerfanta\Adapter\ElasticaAdapter; use PHPUnit\Framework\TestCase; @@ -56,32 +54,48 @@ public function testGetSlice() $this->assertSame($this->resultSet, $this->adapter->getResultSet()); } - public function testGetNbResults() + /** + * Returns the number of results before search, use count() method if resultSet is empty + */ + public function testGetNbResultsBeforeSearch() { - $this->searchable->expects($this->any()) + $this->searchable->expects($this->once()) + ->method('count') + ->with($this->query) + ->willReturn(100); + + $this->assertSame(100, $this->adapter->getNbResults()); + } + + /** + * Returns the number of results after search, use getTotalHits() method if resultSet is not empty + */ + public function testGetNbResultsAfterSearch() + { + $adapter = new ElasticaAdapter($this->searchable, $this->query, [], 30); + + $this->searchable->expects($this->once()) ->method('search') - ->with($this->query, $this->options) + ->with($this->query, array('from' => 10, 'size' => 30)) ->will($this->returnValue($this->resultSet)); $this->resultSet->expects($this->once()) ->method('getTotalHits') ->will($this->returnValue(100)); - $this->assertSame(100, $this->adapter->getNbResults()); + $adapter->getSlice(10, 30); + + $this->assertSame(30, $adapter->getNbResults()); } public function testGetNbResultsWithMaxResultsSet() { $adapter = new ElasticaAdapter($this->searchable, $this->query, [], 10); - $this->searchable->expects($this->any()) - ->method('search') - ->with($this->query, []) - ->will($this->returnValue($this->resultSet)); - - $this->resultSet->expects($this->once()) - ->method('getTotalHits') - ->will($this->returnValue(100)); + $this->searchable->expects($this->once()) + ->method('count') + ->with($this->query) + ->willReturn(100); $this->assertSame(10, $adapter->getNbResults()); }