Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/Search/Comb/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

class Query extends QueryBuilder
{
protected $lookups = [];

public function getSearchResults($query)
{
return $this->index->lookup($this->query);
return $this->lookups[$this->query] ??= $this->index->lookup($this->query);
}
}
41 changes: 41 additions & 0 deletions tests/Search/Comb/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\Search\Comb;

use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Search\Comb\Index;
use Statamic\Search\Comb\Query;
use Tests\TestCase;

class QueryTest extends TestCase
{
#[Test]
public function paginating_looks_up_the_index_once()
{
$index = Mockery::mock(Index::class);
$index->shouldReceive('lookup')->with('foo')->once()->andReturn(collect([
['reference' => 'a', 'search_score' => 2],
['reference' => 'b', 'search_score' => 1],
]));

$paginator = (new Query($index))->query('foo')->withoutData()->paginate(1);

$this->assertEquals(2, $paginator->total());
$this->assertEquals(['a'], $paginator->getCollection()->map->reference->all());
}

#[Test]
public function changing_the_query_looks_up_the_index_again()
{
$index = Mockery::mock(Index::class);
$index->shouldReceive('lookup')->with('foo')->once()->andReturn(collect([['reference' => 'a']]));
$index->shouldReceive('lookup')->with('bar')->once()->andReturn(collect([['reference' => 'b']]));

$builder = (new Query($index))->withoutData();

$this->assertEquals(['a'], $builder->query('foo')->get()->map->reference->all());
$this->assertEquals(['b'], $builder->query('bar')->get()->map->reference->all());
$this->assertEquals(['a'], $builder->query('foo')->get()->map->reference->all());
}
}
Loading