Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ElasticaAdapter] use count() instead search() for getNbResult() #280

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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