Skip to content

Commit 70a2399

Browse files
committed
Add syncingDisabledFor functionality to Aggregator
1 parent 423c167 commit 70a2399

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;
@@ -687,6 +688,115 @@ public function testSkipSavedWhenDisableSyncingFor(): void
687688
ModelObserver::enableSyncingFor(User::class);
688689
}
689690
}
691+
692+
public function testSkipSavedWhenDisableSyncingForAggregator(): void
693+
{
694+
$wallIndexMock = $this->mockIndex('wall');
695+
$usersIndexMock = $this->mockIndex('users');
696+
697+
$usersIndexMock->shouldReceive('saveObjects')->once();
698+
699+
$user = factory(User::class)->create();
700+
701+
AggregatorObserver::disableSyncingFor(Wall::class);
702+
703+
Wall::bootSearchable();
704+
try {
705+
// Ensure saveObjects is NOT called for the wall index (aggregator)
706+
$wallIndexMock->shouldNotReceive('saveObjects')
707+
->with(Mockery::on(function ($argument) {
708+
return count($argument) === 1 && array_key_exists('email', $argument[0]) &&
709+
$argument[0]['objectID'] === 'App\User::1';
710+
}));
711+
712+
// Expect saveObjects to be called once for the user index
713+
$usersIndexMock->shouldReceive('saveObjects')
714+
->once()
715+
->with(Mockery::on(function ($argument) {
716+
return count($argument) === 1 && array_key_exists('email', $argument[0]) &&
717+
$argument[0]['objectID'] === 'App\User::1';
718+
}));
719+
720+
Wall::bootSearchable();
721+
722+
$user->save();
723+
} finally {
724+
AggregatorObserver::enableSyncingFor(User::class);
725+
}
726+
}
727+
728+
public function testSkipDeletedWhenDisableSyncingForAggregator(): void
729+
{
730+
$this->app['config']->set('scout.algolia.use_deprecated_delete_by', false);
731+
732+
$wallIndexMock = $this->mockIndex('wall');
733+
$usersIndexMock = $this->mockIndex('users');
734+
735+
$usersIndexMock->shouldReceive('saveObjects')->once();
736+
737+
$user = factory(User::class)->create();
738+
739+
AggregatorObserver::disableSyncingFor(Wall::class);
740+
741+
Wall::bootSearchable();
742+
743+
try {
744+
// Ensure saveObjects is NOT called for the wall index (aggregator)
745+
$wallIndexMock->shouldNotReceive('browseObjects')->with([
746+
'attributesToRetrieve' => ['objectID'],
747+
'tagFilters' => [['App\User::1']],
748+
])->andReturn([['objectID' => 'App\User::1']]);
749+
$wallIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']);
750+
751+
// Expect browseObjects and deleteObjects to be called on the users index
752+
$usersIndexMock->shouldReceive('browseObjects')->once()->with([
753+
'attributesToRetrieve' => ['objectID'],
754+
'tagFilters' => [['App\User::1']],
755+
])->andReturn([['objectID' => 'App\User::1']]);
756+
$usersIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']);
757+
758+
$user->delete();
759+
} finally {
760+
AggregatorObserver::enableSyncingFor(Wall::class);
761+
}
762+
}
763+
764+
public function testSkipForceDeletedWhenDisableSyncingForAggregator(): void
765+
{
766+
$this->app['config']->set('scout.algolia.use_deprecated_delete_by', false);
767+
768+
$wallIndexMock = $this->mockIndex('wall');
769+
$usersIndexMock = $this->mockIndex('users');
770+
771+
$usersIndexMock->shouldReceive('saveObjects')->once();
772+
773+
$user = factory(User::class)->create();
774+
775+
AggregatorObserver::disableSyncingFor(Wall::class);
776+
777+
Wall::bootSearchable();
778+
779+
try {
780+
// Ensure saveObjects is NOT called for the wall index (aggregator)
781+
$wallIndexMock->shouldNotReceive('browseObjects')->with([
782+
'attributesToRetrieve' => ['objectID'],
783+
'tagFilters' => [['App\User::1']],
784+
])->andReturn([['objectID' => 'App\User::1']]);
785+
$wallIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']);
786+
787+
// Expect browseObjects and deleteObjects to be called on the users index
788+
$usersIndexMock->shouldReceive('browseObjects')->once()->with([
789+
'attributesToRetrieve' => ['objectID'],
790+
'tagFilters' => [['App\User::1']],
791+
])->andReturn([['objectID' => 'App\User::1']]);
792+
$usersIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']);
793+
794+
$user->forceDelete();
795+
} finally {
796+
AggregatorObserver::enableSyncingFor(Wall::class);
797+
}
798+
}
799+
690800
}
691801

692802
class DummyRemoveFromSearch {

0 commit comments

Comments
 (0)