Skip to content

Commit

Permalink
Merge pull request #318 from mnightingale/skip-empty-updates
Browse files Browse the repository at this point in the history
[6.0] Skip empty updates
  • Loading branch information
taylorotwell authored Nov 10, 2018
2 parents fa82a4d + 7195b3e commit 31db9b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function update($models)
$models->each->pushSoftDeleteMetadata();
}

$index->addObjects($models->map(function ($model) {
$objects = $models->map(function ($model) {
$array = array_merge(
$model->toSearchableArray(), $model->scoutMetadata()
);
Expand All @@ -56,7 +56,11 @@ public function update($models)
}

return array_merge(['objectID' => $model->getScoutKey()], $array);
})->filter()->values()->all());
})->filter()->values()->all();

if (! empty($objects)) {
$index->addObjects($objects);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laravel\Scout\Engines\AlgoliaEngine;
use Tests\Fixtures\AlgoliaEngineTestCustomKeyModel;
use Tests\Fixtures\AlgoliaEngineTestModel;
use Tests\Fixtures\EmptyTestModel;
use Illuminate\Database\Eloquent\Collection;

class AlgoliaEngineTest extends AbstractTestCase
Expand Down Expand Up @@ -99,4 +100,14 @@ public function test_flush_a_model()
$engine = new AlgoliaEngine($client);
$engine->flush(new AlgoliaEngineTestCustomKeyModel());
}

public function test_update_empty_searchable_array_does_not_add_objects_to_index()
{
$client = Mockery::mock('AlgoliaSearch\Client');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = Mockery::mock('StdClass'));
$index->shouldNotReceive('addObjects');

$engine = new AlgoliaEngine($client);
$engine->update(Collection::make([new EmptyTestModel()]));
}
}
11 changes: 11 additions & 0 deletions tests/Fixtures/EmptyTestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tests\Fixtures;

class EmptyTestModel extends TestModel
{
public function toSearchableArray()
{
return [];
}
}

0 comments on commit 31db9b5

Please sign in to comment.