Skip to content
Closed
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
6 changes: 4 additions & 2 deletions src/Jobs/RemoveFromSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RemoveFromSearch implements ShouldQueue
*/
public function __construct($models)
{
$this->models = RemoveableScoutCollection::make($models);
$this->models = $models;
}

/**
Expand All @@ -38,7 +38,9 @@ public function __construct($models)
public function handle()
{
if ($this->models->isNotEmpty()) {
$this->models->first()->searchableUsing()->delete($this->models);
$this->models->first()->searchableUsing()->delete(
RemoveableScoutCollection::make($this->models)
);
Copy link
Contributor

@stevebauman stevebauman Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't make this change. The RemoveableScoutCollection instance must be present in the constructor for the getQueueableIds() method to return the proper scout keys. This collection method will be called when the job is serialized and placed onto the queue:

public function getQueueableIds()

The test passes because the Eloquent\Collection instance will (by default) utilize the model's primary key (which is being populated in the test model) in it's getQueuableIds() method, so it will be available inside of the faux model, and thus the getScoutKey() will return the correct key:

https://github.com/laravel/framework/blob/b89f0d95275baf8a2b5adcc265203d7d365950be/src/Illuminate/Database/Eloquent/Collection.php#L697-L706

}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/RemoveFromSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ public function test_models_are_deserialized_without_the_database()
$this->assertEquals(1234, $job->models->first()->getScoutKey());
}

public function test_models_are_deserialized_without_the_database_using_custom_scout_key()
public function test_models_are_deserialized_without_the_database_using_custom_scout_key_values()
{
$job = new RemoveFromSearch(Collection::make([
$model = new SearchableModelWithCustomKey(['other_id' => 1234]),
$model = new MeiliSearchCustomKeySearchableModel(['id' => 1234]),
]));

$job = unserialize(serialize($job));

$this->assertInstanceOf(Collection::class, $job->models);
$this->assertCount(1, $job->models);
$this->assertInstanceOf(SearchableModelWithCustomKey::class, $job->models->first());
$this->assertInstanceOf(MeiliSearchCustomKeySearchableModel::class, $job->models->first());
$this->assertTrue($model->is($job->models->first()));
$this->assertEquals(1234, $job->models->first()->getScoutKey());
$this->assertEquals('searchable_model_with_custom_keys.other_id', $job->models->first()->getScoutKeyName());
$this->assertEquals('my-meilisearch-key.1234', $job->models->first()->getScoutKey());
$this->assertEquals('meili_search_custom_key_searchable_models.id', $job->models->first()->getScoutKeyName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will pass because the MeiliSearchCustomKeySearchableModel fixture is being instantiated with primary key that is being used in the getScoutKey() method:

public function getScoutKey()
{
    return 'my-meilisearch-key.'.$this->getKey();
}

https://github.com/DarronEngelbrechtEdge/scout/blob/246bf96e1042f1b1adc586f69f6260e389a1dccb/tests/Unit/MeiliSearchEngineTest.php#L475-L478

If another model attribute is used to construct the scout key, then this will fail, as it cannot be retrieved from the faux model that is created after the job is unserialized. All other model properties will not exist.

}

public function test_removeable_scout_collection_returns_scout_keys()
Expand Down