Skip to content

Commit 651773c

Browse files
authored
[10.x] searchableSync / unsearchableSync (#920)
* searchableSync / unsearchableSync * Fix tests
1 parent 8c1a380 commit 651773c

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

src/Searchable.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public function registerSearchableMacros()
4545
BaseCollection::macro('unsearchable', function () use ($self) {
4646
$self->queueRemoveFromSearch($this);
4747
});
48+
49+
BaseCollection::macro('searchableSync', function () use ($self) {
50+
$self->syncMakeSearchable($this);
51+
});
52+
53+
BaseCollection::macro('unsearchableSync', function () use ($self) {
54+
$self->syncRemoveFromSearch($this);
55+
});
4856
}
4957

5058
/**
@@ -60,14 +68,29 @@ public function queueMakeSearchable($models)
6068
}
6169

6270
if (! config('scout.queue')) {
63-
return $models->first()->makeSearchableUsing($models)->first()->searchableUsing()->update($models);
71+
return $this->syncMakeSearchable($models);
6472
}
6573

6674
dispatch((new Scout::$makeSearchableJob($models))
6775
->onQueue($models->first()->syncWithSearchUsingQueue())
6876
->onConnection($models->first()->syncWithSearchUsing()));
6977
}
7078

79+
/**
80+
* Synchronously make the given models searchable.
81+
*
82+
* @param \Illuminate\Database\Eloquent\Collection $models
83+
* @return void
84+
*/
85+
public function syncMakeSearchable($models)
86+
{
87+
if ($models->isEmpty()) {
88+
return;
89+
}
90+
91+
return $models->first()->makeSearchableUsing($models)->first()->searchableUsing()->update($models);
92+
}
93+
7194
/**
7295
* Dispatch the job to make the given models unsearchable.
7396
*
@@ -81,14 +104,29 @@ public function queueRemoveFromSearch($models)
81104
}
82105

83106
if (! config('scout.queue')) {
84-
return $models->first()->searchableUsing()->delete($models);
107+
return $this->syncRemoveFromSearch($models);
85108
}
86109

87110
dispatch(new Scout::$removeFromSearchJob($models))
88111
->onQueue($models->first()->syncWithSearchUsingQueue())
89112
->onConnection($models->first()->syncWithSearchUsing());
90113
}
91114

115+
/**
116+
* Synchronously make the given models unsearchable.
117+
*
118+
* @param \Illuminate\Database\Eloquent\Collection $models
119+
* @return void
120+
*/
121+
public function syncRemoveFromSearch($models)
122+
{
123+
if ($models->isEmpty()) {
124+
return;
125+
}
126+
127+
return $models->first()->searchableUsing()->delete($models);
128+
}
129+
92130
/**
93131
* Determine if the model should be searchable.
94132
*
@@ -183,6 +221,16 @@ public function searchable()
183221
$this->newCollection([$this])->searchable();
184222
}
185223

224+
/**
225+
* Synchronously make the given model instance searchable.
226+
*
227+
* @return void
228+
*/
229+
public function searchableSync()
230+
{
231+
$this->newCollection([$this])->searchableSync();
232+
}
233+
186234
/**
187235
* Remove all instances of the model from the search index.
188236
*
@@ -205,6 +253,16 @@ public function unsearchable()
205253
$this->newCollection([$this])->unsearchable();
206254
}
207255

256+
/**
257+
* Synchronously remove the given model instance from the search index.
258+
*
259+
* @return void
260+
*/
261+
public function unsearchableSync()
262+
{
263+
$this->newCollection([$this])->unsearchableSync();
264+
}
265+
208266
/**
209267
* Determine if the model existed in the search index prior to an update.
210268
*

tests/Feature/SearchableTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,25 @@ class SearchableTest extends TestCase
1919
public function test_searchable_using_update_is_called_on_collection()
2020
{
2121
$collection = m::mock();
22-
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
22+
$collection->shouldReceive('isEmpty')->times(2)->andReturn(false);
2323
$collection->shouldReceive('first->makeSearchableUsing')->with($collection)->once()->andReturn($collection);
2424
$collection->shouldReceive('first->searchableUsing->update')->with($collection)->once();
2525

2626
$model = new SearchableModel;
2727
$model->queueMakeSearchable($collection);
2828
}
2929

30+
public function test_searchable_using_update_is_called_on_collection_sync()
31+
{
32+
$collection = m::mock();
33+
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
34+
$collection->shouldReceive('first->makeSearchableUsing')->with($collection)->once()->andReturn($collection);
35+
$collection->shouldReceive('first->searchableUsing->update')->with($collection)->once();
36+
37+
$model = new SearchableModel;
38+
$model->syncMakeSearchable($collection);
39+
}
40+
3041
public function test_searchable_using_update_is_not_called_on_empty_collection()
3142
{
3243
$collection = m::mock();
@@ -35,6 +46,13 @@ public function test_searchable_using_update_is_not_called_on_empty_collection()
3546

3647
$model = new SearchableModel;
3748
$model->queueMakeSearchable($collection);
49+
50+
$collection = m::mock();
51+
$collection->shouldReceive('isEmpty')->andReturn(true);
52+
$collection->shouldNotReceive('first->searchableUsing->update');
53+
54+
$model = new SearchableModel;
55+
$model->syncMakeSearchable($collection);
3856
}
3957

4058
public function test_overridden_make_searchable_is_dispatched()
@@ -58,13 +76,23 @@ public function test_overridden_make_searchable_is_dispatched()
5876
public function test_searchable_using_delete_is_called_on_collection()
5977
{
6078
$collection = m::mock();
61-
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
79+
$collection->shouldReceive('isEmpty')->times(2)->andReturn(false);
6280
$collection->shouldReceive('first->searchableUsing->delete')->with($collection);
6381

6482
$model = new SearchableModel;
6583
$model->queueRemoveFromSearch($collection);
6684
}
6785

86+
public function test_searchable_using_delete_is_called_on_collection_sycn()
87+
{
88+
$collection = m::mock();
89+
$collection->shouldReceive('isEmpty')->once()->andReturn(false);
90+
$collection->shouldReceive('first->searchableUsing->delete')->with($collection);
91+
92+
$model = new SearchableModel;
93+
$model->syncRemoveFromSearch($collection);
94+
}
95+
6896
public function test_searchable_using_delete_is_not_called_on_empty_collection()
6997
{
7098
$collection = m::mock();
@@ -73,6 +101,13 @@ public function test_searchable_using_delete_is_not_called_on_empty_collection()
73101

74102
$model = new SearchableModel;
75103
$model->queueRemoveFromSearch($collection);
104+
105+
$collection = m::mock();
106+
$collection->shouldReceive('isEmpty')->once()->andReturn(true);
107+
$collection->shouldNotReceive('first->searchableUsing->delete');
108+
109+
$model = new SearchableModel;
110+
$model->syncRemoveFromSearch($collection);
76111
}
77112

78113
public function test_overridden_remove_from_search_is_dispatched()

0 commit comments

Comments
 (0)