Skip to content

Commit

Permalink
Merge pull request #280 from romainnorberg/master
Browse files Browse the repository at this point in the history
[ElasticaAdapter] use count() instead search() for getNbResult()
  • Loading branch information
Sam Partington authored Jun 20, 2019
2 parents 61ab942 + 7f6e179 commit c385484
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Pagerfanta/Adapter/ElasticaAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
42 changes: 28 additions & 14 deletions tests/Pagerfanta/Tests/Adapter/ElasticaAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Pagerfanta\Tests\Adapter;

use Elastica\Response;
use Elastica\ResultSet;
use Pagerfanta\Adapter\ElasticaAdapter;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit c385484

Please sign in to comment.