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 3 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
14 changes: 14 additions & 0 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@

'identify' => env('SCOUT_IDENTIFY', false),

/*
|--------------------------------------------------------------------------
| Scout Jobs
|--------------------------------------------------------------------------
|
| @TODO add description
|
*/

'jobs' => [
'make_searchable' => Laravel\Scout\Jobs\MakeSearchable::class,
'remove_from_search' => Laravel\Scout\Jobs\RemoveFromSearch::class,
],
MatanYadaev marked this conversation as resolved.
Show resolved Hide resolved

/*
|--------------------------------------------------------------------------
| Algolia Configuration
Expand Down
8 changes: 6 additions & 2 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function queueMakeSearchable($models)
return $models->first()->searchableUsing()->update($models);
}

dispatch((new MakeSearchable($models))
$makeSearchableJob = config('scout.jobs.make_searchable', MakeSearchable::class);

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

dispatch(new RemoveFromSearch($models))
$removeFromSearchJob = config('scout.jobs.remove_from_search', RemoveFromSearch::class);

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

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Queue;
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 +32,23 @@ 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);
config()->set('scout.jobs.make_searchable', OverriddenMakeSearchable::class);

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

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

public function test_searchable_using_delete_is_called_on_collection()
{
$collection = m::mock();
Expand All @@ -49,6 +69,23 @@ 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);
config()->set('scout.jobs.remove_from_search', OverriddenRemoveFromSearch::class);

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

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

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];
}
}
12 changes: 12 additions & 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 All @@ -24,4 +25,15 @@ public function test_handle_passes_the_collection_to_engine()

$job->handle();
}

public function test_overridden_make_searchable_handle_passes_the_collection_to_engine()
{
$job = new OverriddenMakeSearchable($collection = Collection::make([
$model = m::mock(),
]));

$model->shouldReceive('searchableUsing->update')->with($collection);

$job->handle();
}
MatanYadaev marked this conversation as resolved.
Show resolved Hide resolved
}
12 changes: 12 additions & 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 Expand Up @@ -32,6 +33,17 @@ public function test_handle_passes_the_collection_to_engine()
$job->handle();
}

public function test_overridden_remove_from_search_handle_passes_the_collection_to_engine()
{
$job = new OverriddenRemoveFromSearch($collection = Collection::make([
$model = m::mock(),
]));

$model->shouldReceive('searchableUsing->delete')->with($collection);

$job->handle();
}

MatanYadaev marked this conversation as resolved.
Show resolved Hide resolved
public function test_models_are_deserialized_without_the_database()
{
$job = new RemoveFromSearch($collection = Collection::make([
Expand Down