Skip to content

Commit

Permalink
[ElasticaAdapter] use count() instead search()
Browse files Browse the repository at this point in the history
  • Loading branch information
romainnorberg committed Jun 19, 2019
1 parent 61ab942 commit 7f6e179
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 7f6e179

Please sign in to comment.