Skip to content

Commit

Permalink
Remove throwing an error if gambits registered without fulltext gambit
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Apr 9, 2021
1 parent dc07338 commit b10af17
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 51 deletions.
23 changes: 2 additions & 21 deletions src/Search/SearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,14 @@ public function register()
public function boot()
{
$fullTextGambits = $this->container->make('flarum.simple_search.fulltext_gambits');
$gambits = $this->container->make('flarum.simple_search.gambits');

$searchersWithGambitsWithoutFulltext = array_diff(array_keys($gambits), array_keys($fullTextGambits));

if (count($searchersWithGambitsWithoutFulltext)) {
$affectedGambits = [];
foreach ($searchersWithGambitsWithoutFulltext as $searcher) {
// This check is in place to support adding gambits to searchers
// registered in extensions that are optional dependencies of the
// current extension.
if (class_exists($searcher)) {
$affectedGambits += $gambits[$searcher];
}
}

if (count($affectedGambits)) {
throw new \RuntimeException('You cannot add gambits to searchers that do not have fulltext gambits. The following searchers have this issue: '.implode(', ', $affectedGambits));
}
}

foreach ($fullTextGambits as $searcher => $fullTextGambitClass) {
$this->container
->when($searcher)
->needs(GambitManager::class)
->give(function () use ($searcher, $fullTextGambitClass, $gambits) {
->give(function () use ($searcher, $fullTextGambitClass) {
$gambitManager = new GambitManager($this->container->make($fullTextGambitClass));
foreach (Arr::get($gambits, $searcher, []) as $gambit) {
foreach (Arr::get($this->container->make('flarum.simple_search.gambits'), $searcher, []) as $gambit) {
$gambitManager->add($this->container->make($gambit));
}

Expand Down
30 changes: 0 additions & 30 deletions tests/integration/extenders/SimpleFlarumSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,36 +170,6 @@ public function can_resolve_custom_searcher_with_fulltext_gambit()

$this->assertFalse($anExceptionWasThrown);
}


/**
* @test
*/
public function can_add_gambit_to_nonexistent_searcher_class_without_fulltext()
{
$this->extend((new Extend\SimpleFlarumSearch(NonexistentClass::class))->addGambit(NoResultFilterGambit::class));

$anExceptionWasThrown = false;

try {
$this->app();
} catch (\RuntimeException $e) {
$anExceptionWasThrown = true;
}

$this->assertFalse($anExceptionWasThrown);
}

/**
* @test
*/
public function cant_add_gambit_to_existent_searcher_class_without_fulltext()
{
$this->expectException(\RuntimeException::class);

$this->extend((new Extend\SimpleFlarumSearch(AbstractSearcher::class))->addGambit(NoResultFilterGambit::class));
$this->app();
}
}

class NoResultFullTextGambit implements GambitInterface
Expand Down

0 comments on commit b10af17

Please sign in to comment.