Skip to content

Commit 4681164

Browse files
ajnsnjasonvarga
andauthored
[5.x] Provide search index name callback (#10435)
Co-authored-by: Jason Varga <[email protected]>
1 parent d2d53f5 commit 4681164

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

src/Search/Index.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Statamic\Search;
44

5+
use Closure;
56
use Statamic\Contracts\Search\Searchable;
67
use Statamic\Support\Arr;
78
use Statamic\Support\Str;
@@ -11,6 +12,7 @@ abstract class Index
1112
protected $name;
1213
protected $locale;
1314
protected $config;
15+
protected static ?Closure $nameCallback = null;
1416

1517
abstract public function search($query);
1618

@@ -24,7 +26,10 @@ abstract protected function deleteIndex();
2426

2527
public function __construct($name, array $config, ?string $locale = null)
2628
{
27-
$this->name = $locale ? $name.'_'.$locale : $name;
29+
$this->name = static::$nameCallback
30+
? call_user_func(static::$nameCallback, $name, $locale)
31+
: ($locale ? $name.'_'.$locale : $name);
32+
2833
$this->config = $config;
2934
$this->locale = $locale;
3035
}
@@ -34,6 +39,11 @@ public function name()
3439
return $this->name;
3540
}
3641

42+
public static function resolveNameUsing(?Closure $callback)
43+
{
44+
static::$nameCallback = $callback;
45+
}
46+
3747
public function title()
3848
{
3949
return $this->config['title'] ?? Str::title($this->name);

tests/Search/AlgoliaIndexTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
33
namespace Tests\Search;
44

55
use Mockery;
6-
use Statamic\Search\Algolia\Index;
7-
use Statamic\Search\ItemResolver;
6+
use Statamic\Search\Algolia\Index as AlgoliaIndex;
87
use Tests\TestCase;
98

109
class AlgoliaIndexTest extends TestCase
1110
{
1211
use IndexTests;
1312

14-
public function getIndex()
13+
public function getIndexClass()
1514
{
16-
$resolver = Mockery::mock(ItemResolver::class);
17-
$resolver->shouldReceive('setIndex');
18-
19-
$client = Mockery::mock(\AlgoliaSearch\Client::class);
20-
$index = Mockery::mock(\AlgoliaSearch\Index::class);
15+
return AlgoliaIndex::class;
16+
}
2117

22-
$client->shouldReceive('initIndex')->andReturn($index);
23-
$index->shouldReceive('search')->andReturn(['hits' => []]);
18+
public function getIndex($name, $config, $locale)
19+
{
20+
$client = Mockery::mock(\Algolia\AlgoliaSearch\SearchClient::class);
2421

25-
return new Index($resolver, $client);
22+
return new AlgoliaIndex($client, $name, $config, $locale);
2623
}
2724
}

tests/Search/CombIndexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ protected function beforeSearched()
3535
->andReturn('[[]]');
3636
}
3737

38-
public function getIndex()
38+
public function getIndexClass()
3939
{
40-
return app(Index::class);
40+
return Index::class;
4141
}
4242
}

tests/Search/IndexTests.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,48 @@
22

33
namespace Tests\Search;
44

5-
use Illuminate\Support\Facades\Event;
5+
use PHPUnit\Framework\Attributes\DataProvider;
66
use PHPUnit\Framework\Attributes\Test;
7-
use Statamic\Events\SearchQueryPerformed;
7+
use Statamic\Search\Index;
88

99
trait IndexTests
1010
{
11-
#[Test]
12-
public function search_event_gets_emitted()
11+
public function tearDown(): void
1312
{
14-
$this->markTestSkipped();
13+
// Reset the static state of the Index class
14+
Index::resolveNameUsing(null);
1515

16-
Event::fake();
16+
parent::tearDown();
17+
}
18+
19+
abstract public function getIndexClass();
20+
21+
public function getIndex($name, $config, $locale)
22+
{
23+
$class = $this->getIndexClass();
1724

18-
$this->beforeSearched();
25+
return new $class($name, $config, $locale);
26+
}
27+
28+
#[Test, DataProvider('nameProvider')]
29+
public function it_can_get_the_name($name, $config, $locale, $resolver, $expected)
30+
{
31+
if ($resolver) {
32+
$this->getIndexClass()::resolveNameUsing($resolver);
33+
}
1934

20-
$this->getIndex()->setName('test')->search('foo');
35+
$index = $this->getIndex($name, $config, $locale);
2136

22-
Event::assertDispatched(SearchQueryPerformed::class, function ($event) {
23-
return $event->query === 'foo';
24-
});
37+
$this->assertEquals($expected, $index->name());
2538
}
2639

27-
protected function beforeSearched()
40+
public static function nameProvider()
2841
{
42+
return [
43+
'basic' => ['test', [], null, null, 'test'],
44+
'with locale' => ['test', [], 'en', null, 'test_en'],
45+
'resolver' => ['test', [], null, fn ($name, $locale) => 'prefix_'.$name.'_'.$locale, 'prefix_test_'],
46+
'resolver with locale' => ['test', [], 'en', fn ($name, $locale) => 'prefix_'.$name.'_'.$locale, 'prefix_test_en'],
47+
];
2948
}
3049
}

0 commit comments

Comments
 (0)