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

[6.0] Skip empty updates #318

Merged
merged 3 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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 [];
}
}