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

[9.x] Overridable jobs #476

Merged
merged 8 commits into from
Jun 1, 2021
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
45 changes: 45 additions & 0 deletions src/Scout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Laravel\Scout;

use Laravel\Scout\Jobs\MakeSearchable;
use Laravel\Scout\Jobs\RemoveFromSearch;

class Scout
{
/**
* The job class that should make models searchable.
*
* @var string
*/
public static $makeSearchableJob = MakeSearchable::class;

/**
* The job that should remove models from the search index.
*
* @var string
*/
public static $removeFromSearchJob = RemoveFromSearch::class;
MatanYadaev marked this conversation as resolved.
Show resolved Hide resolved

/**
* Specify the job class that should make models searchable.
*
* @param string $class
* @return void
*/
public static function makeSearchableUsing(string $class)
{
static::$makeSearchableJob = $class;
}

/**
* Specify the job class that should remove models from the search index.
*
* @param string $class
* @return void
*/
public static function removeFromSearchUsing(string $class)
{
static::$removeFromSearchJob = $class;
}
}
4 changes: 2 additions & 2 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function queueMakeSearchable($models)
return $models->first()->searchableUsing()->update($models);
}

dispatch((new MakeSearchable($models))
dispatch((new Scout::$makeSearchableJob($models))
->onQueue($models->first()->syncWithSearchUsingQueue())
->onConnection($models->first()->syncWithSearchUsing()));
}
Expand All @@ -85,7 +85,7 @@ public function queueRemoveFromSearch($models)
return $models->first()->searchableUsing()->delete($models);
}

dispatch(new RemoveFromSearch($models))
dispatch(new Scout::$removeFromSearchJob($models))
->onQueue($models->first()->syncWithSearchUsingQueue())
->onConnection($models->first()->syncWithSearchUsing());
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/SearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
namespace Laravel\Scout\Tests\Feature;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Queue;
use Laravel\Scout\Jobs\MakeSearchable;
use Laravel\Scout\Jobs\RemoveFromSearch;
use Laravel\Scout\Scout;
use Laravel\Scout\Tests\Fixtures\OverriddenMakeSearchable;
use Laravel\Scout\Tests\Fixtures\OverriddenRemoveFromSearch;
use Laravel\Scout\Tests\Fixtures\SearchableModel;
use Mockery as m;
use Orchestra\Testbench\TestCase;
Expand All @@ -29,6 +35,24 @@ public function test_searchable_using_update_is_not_called_on_empty_collection()
$model->queueMakeSearchable($collection);
}

public function test_overridden_make_searchable_is_dispatched()
{
Queue::fake();

config()->set('scout.queue', true);
Scout::makeSearchableUsing(OverriddenMakeSearchable::class);

$collection = m::mock();
$collection->shouldReceive('isEmpty')->andReturn(false);
$collection->shouldReceive('first->syncWithSearchUsingQueue');
$collection->shouldReceive('first->syncWithSearchUsing');

$model = new SearchableModel;
$model->queueMakeSearchable($collection);

Scout::makeSearchableUsing(MakeSearchable::class);
}

public function test_searchable_using_delete_is_called_on_collection()
{
$collection = m::mock();
Expand All @@ -49,6 +73,24 @@ public function test_searchable_using_delete_is_not_called_on_empty_collection()
$model->queueRemoveFromSearch($collection);
}

public function test_overridden_remove_from_search_is_dispatched()
{
Queue::fake();

config()->set('scout.queue', true);
Scout::removeFromSearchUsing(OverriddenRemoveFromSearch::class);

$collection = m::mock();
$collection->shouldReceive('isEmpty')->andReturn(false);
$collection->shouldReceive('first->syncWithSearchUsingQueue');
$collection->shouldReceive('first->syncWithSearchUsing');

$model = new SearchableModel;
$model->queueRemoveFromSearch($collection);

Scout::removeFromSearchUsing(RemoveFromSearch::class);
}

public function test_make_all_searchable_uses_order_by()
{
ModelStubForMakeAllSearchable::makeAllSearchable();
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/OverriddenMakeSearchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Laravel\Scout\Tests\Fixtures;

use Laravel\Scout\Jobs\MakeSearchable;

class OverriddenMakeSearchable extends MakeSearchable
{
public $tries = 5;

public function backoff(): array
{
return [2, 4, 8, 16, 32];
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/OverriddenRemoveFromSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Laravel\Scout\Tests\Fixtures;

use Laravel\Scout\Jobs\RemoveFromSearch;

class OverriddenRemoveFromSearch extends RemoveFromSearch
{
public $tries = 5;

public function backoff(): array
{
return [2, 4, 8, 16, 32];
}
}
1 change: 1 addition & 0 deletions tests/Unit/MakeSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Jobs\MakeSearchable;
use Laravel\Scout\Tests\Fixtures\OverriddenMakeSearchable;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/RemoveFromSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Config;
use Laravel\Scout\Jobs\RemoveFromSearch;
use Laravel\Scout\Tests\Fixtures\OverriddenRemoveFromSearch;
use Laravel\Scout\Tests\Fixtures\SearchableModel;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down