Skip to content

Commit caeddb8

Browse files
committed
Add syncingDisabledFor functionality to Aggregator
1 parent a5895a2 commit caeddb8

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/Searchable/AggregatorObserver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public function saved($model): void
7676
}
7777

7878
foreach ($this->aggregators[$class] as $aggregator) {
79+
if (static::syncingDisabledFor($aggregator)) {
80+
continue;
81+
}
7982
parent::saved($aggregator::create($model));
8083
}
8184
}
@@ -95,6 +98,9 @@ public function deleted($model): void
9598
}
9699

97100
foreach ($this->aggregators[$class] as $aggregator) {
101+
if (static::syncingDisabledFor($aggregator)) {
102+
continue;
103+
}
98104
$aggregator::create($model)->unsearchable();
99105
}
100106
}
@@ -115,6 +121,9 @@ public function forceDeleted($model): void
115121
}
116122

117123
foreach ($this->aggregators[$class] as $aggregator) {
124+
if (static::syncingDisabledFor($aggregator)) {
125+
continue;
126+
}
118127
$aggregator::create($model)->unsearchable();
119128
}
120129
}

tests/Features/AggregatorTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Algolia\AlgoliaSearch\Iterators\ObjectIterator;
99
use Algolia\ScoutExtended\Searchable\Aggregator;
1010
use Algolia\ScoutExtended\Searchable\AggregatorCollection;
11+
use Algolia\ScoutExtended\Searchable\AggregatorObserver;
1112
use Algolia\ScoutExtended\Searchable\Aggregators;
1213
use App\All;
1314
use App\News;
@@ -692,6 +693,115 @@ public function testSkipSavedWhenDisableSyncingFor(): void
692693
ModelObserver::enableSyncingFor(User::class);
693694
}
694695
}
696+
697+
public function testSkipSavedWhenDisableSyncingForAggregator(): void
698+
{
699+
$wallIndexMock = $this->mockIndex('wall');
700+
$usersIndexMock = $this->mockIndex('users');
701+
702+
$usersIndexMock->shouldReceive('saveObjects')->once();
703+
704+
$user = factory(User::class)->create();
705+
706+
AggregatorObserver::disableSyncingFor(Wall::class);
707+
708+
Wall::bootSearchable();
709+
try {
710+
// Ensure saveObjects is NOT called for the wall index (aggregator)
711+
$wallIndexMock->shouldNotReceive('saveObjects')
712+
->with(Mockery::on(function ($argument) {
713+
return count($argument) === 1 && array_key_exists('email', $argument[0]) &&
714+
$argument[0]['objectID'] === 'App\User::1';
715+
}));
716+
717+
// Expect saveObjects to be called once for the user index
718+
$usersIndexMock->shouldReceive('saveObjects')
719+
->once()
720+
->with(Mockery::on(function ($argument) {
721+
return count($argument) === 1 && array_key_exists('email', $argument[0]) &&
722+
$argument[0]['objectID'] === 'App\User::1';
723+
}));
724+
725+
Wall::bootSearchable();
726+
727+
$user->save();
728+
} finally {
729+
AggregatorObserver::enableSyncingFor(User::class);
730+
}
731+
}
732+
733+
public function testSkipDeletedWhenDisableSyncingForAggregator(): void
734+
{
735+
$this->app['config']->set('scout.algolia.use_deprecated_delete_by', false);
736+
737+
$wallIndexMock = $this->mockIndex('wall');
738+
$usersIndexMock = $this->mockIndex('users');
739+
740+
$usersIndexMock->shouldReceive('saveObjects')->once();
741+
742+
$user = factory(User::class)->create();
743+
744+
AggregatorObserver::disableSyncingFor(Wall::class);
745+
746+
Wall::bootSearchable();
747+
748+
try {
749+
// Ensure saveObjects is NOT called for the wall index (aggregator)
750+
$wallIndexMock->shouldNotReceive('browseObjects')->with([
751+
'attributesToRetrieve' => ['objectID'],
752+
'tagFilters' => [['App\User::1']],
753+
])->andReturn([['objectID' => 'App\User::1']]);
754+
$wallIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']);
755+
756+
// Expect browseObjects and deleteObjects to be called on the users index
757+
$usersIndexMock->shouldReceive('browseObjects')->once()->with([
758+
'attributesToRetrieve' => ['objectID'],
759+
'tagFilters' => [['App\User::1']],
760+
])->andReturn([['objectID' => 'App\User::1']]);
761+
$usersIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']);
762+
763+
$user->delete();
764+
} finally {
765+
AggregatorObserver::enableSyncingFor(Wall::class);
766+
}
767+
}
768+
769+
public function testSkipForceDeletedWhenDisableSyncingForAggregator(): void
770+
{
771+
$this->app['config']->set('scout.algolia.use_deprecated_delete_by', false);
772+
773+
$wallIndexMock = $this->mockIndex('wall');
774+
$usersIndexMock = $this->mockIndex('users');
775+
776+
$usersIndexMock->shouldReceive('saveObjects')->once();
777+
778+
$user = factory(User::class)->create();
779+
780+
AggregatorObserver::disableSyncingFor(Wall::class);
781+
782+
Wall::bootSearchable();
783+
784+
try {
785+
// Ensure saveObjects is NOT called for the wall index (aggregator)
786+
$wallIndexMock->shouldNotReceive('browseObjects')->with([
787+
'attributesToRetrieve' => ['objectID'],
788+
'tagFilters' => [['App\User::1']],
789+
])->andReturn([['objectID' => 'App\User::1']]);
790+
$wallIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']);
791+
792+
// Expect browseObjects and deleteObjects to be called on the users index
793+
$usersIndexMock->shouldReceive('browseObjects')->once()->with([
794+
'attributesToRetrieve' => ['objectID'],
795+
'tagFilters' => [['App\User::1']],
796+
])->andReturn([['objectID' => 'App\User::1']]);
797+
$usersIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']);
798+
799+
$user->forceDelete();
800+
} finally {
801+
AggregatorObserver::enableSyncingFor(Wall::class);
802+
}
803+
}
804+
695805
}
696806

697807
class DummyRemoveFromSearch {

0 commit comments

Comments
 (0)