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

[5.x] Provide search index name callback #10435

Open
wants to merge 6 commits into
base: 5.x
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions src/Search/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Search;

use Closure;
use Statamic\Contracts\Search\Searchable;
use Statamic\Support\Arr;
use Statamic\Support\Str;
Expand All @@ -11,6 +12,7 @@ abstract class Index
protected $name;
protected $locale;
protected $config;
protected static ?Closure $nameCallback = null;

abstract public function search($query);

Expand All @@ -31,9 +33,18 @@ public function __construct($name, array $config, ?string $locale = null)

public function name()
{
if (static::$nameCallback) {
return call_user_func(static::$nameCallback, $this->name);
}

return $this->name;
}

public static function resolveNameUsing(?Closure $callback)
{
static::$nameCallback = $callback;
}

public function title()
{
return $this->config['title'] ?? Str::title($this->name);
Expand Down
16 changes: 7 additions & 9 deletions tests/Search/AlgoliaIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Tests\Search;

use Mockery;
use Statamic\Search\Algolia\Index;
use Statamic\Search\ItemResolver;
use Statamic\Search\Algolia\Index as AlgoliaIndex;
use Tests\TestCase;

class AlgoliaIndexTest extends TestCase
Expand All @@ -13,15 +12,14 @@ class AlgoliaIndexTest extends TestCase

public function getIndex()
{
$resolver = Mockery::mock(ItemResolver::class);
$resolver->shouldReceive('setIndex');
$name = 'algolia';

$client = Mockery::mock(\AlgoliaSearch\Client::class);
$index = Mockery::mock(\AlgoliaSearch\Index::class);
$locale = 'en';

$client->shouldReceive('initIndex')->andReturn($index);
$index->shouldReceive('search')->andReturn(['hits' => []]);
$config = [];

return new Index($resolver, $client);
$client = Mockery::mock(\Algolia\AlgoliaSearch\SearchClient::class);

return new AlgoliaIndex($client, $name, $config, $locale);
}
}
6 changes: 5 additions & 1 deletion tests/Search/CombIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ protected function beforeSearched()

public function getIndex()
{
return app(Index::class);
$name = 'local';

$config = [];

return new Index($name, $config);
}
}
23 changes: 23 additions & 0 deletions tests/Search/IndexTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Events\SearchQueryPerformed;
use Statamic\Search\Index;

trait IndexTests
{
Expand All @@ -24,6 +25,28 @@ public function search_event_gets_emitted()
});
}

#[Test]
public function it_respects_an_index_prefix()
{
$index = $this->getIndex();

$originalName = $index->name();

$prefix = 'foo';

$index::resolveNameUsing(function ($name) use ($prefix) {
return $prefix.'_'.$name;
});

$this->assertEquals($prefix.'_'.$originalName, $index->name());
}

public function tearDown(): void
{
// Reset the static state of the Index class
Index::resolveNameUsing(null);
}

protected function beforeSearched()
{
}
Expand Down
Loading