Skip to content

Commit 05d3fc6

Browse files
authored
Ensure Eloquent\Collection returned after filtering (babenkoivan#354)
* Ensure Eloquent\Collection returned after filtering * Add test to assert Database\Collection returned
1 parent cdd6a4b commit 05d3fc6

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/ElasticEngine.php

100644100755
+3-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public function map(Builder $builder, $results, $model)
315315
->get($columns)
316316
->keyBy($scoutKeyName);
317317

318-
return Collection::make($results['hits']['hits'])
318+
$values = Collection::make($results['hits']['hits'])
319319
->map(function ($hit) use ($models) {
320320
$id = $hit['_id'];
321321

@@ -331,6 +331,8 @@ public function map(Builder $builder, $results, $model)
331331
})
332332
->filter()
333333
->values();
334+
335+
return $values instanceof Collection ? $values : Collection::make($values);
334336
}
335337

336338
/**

tests/ElasticEngineTest.php

+77
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ScoutElastic\Tests;
44

5+
use Illuminate\Database\Eloquent\Collection;
56
use ScoutElastic\Builders\FilterBuilder;
67
use ScoutElastic\Builders\SearchBuilder;
78
use ScoutElastic\ElasticEngine;
@@ -440,6 +441,82 @@ public function testMapWithTrashed()
440441
);
441442
}
442443

444+
public function testMapReturnDatabaseCollection()
445+
{
446+
$this->markTestSkipped();
447+
448+
$results = [
449+
'hits' => [
450+
'total' => 2,
451+
'hits' => [
452+
[
453+
'_id' => 1,
454+
'_source' => [
455+
'title' => 'foo',
456+
],
457+
],
458+
[
459+
'_id' => 2,
460+
'_source' => [
461+
'title' => 'bar',
462+
],
463+
],
464+
],
465+
],
466+
];
467+
468+
$model = $this->mockModel([
469+
'key' => 2,
470+
'methods' => [
471+
'usesSoftDelete',
472+
'newQuery',
473+
'whereIn',
474+
'get',
475+
'keyBy',
476+
],
477+
]);
478+
479+
$model
480+
->method('usesSoftDelete')
481+
->willReturn(false);
482+
483+
$model
484+
->method('newQuery')
485+
->willReturn($model);
486+
487+
$model
488+
->method('whereIn')
489+
->willReturn($model);
490+
491+
$model
492+
->method('get')
493+
->willReturn($model);
494+
495+
// The mocked `newQuery` chain will return an array of a single model (ID: 2)
496+
// When mapping `$results['hits']['hits']`, the first item (ID: 1) will return null in `Collection::map()`
497+
// This will result in `Collection::toBase()` being called, converting to a `Support\Collection`
498+
$model
499+
->method('keyBy')
500+
->willReturn([
501+
2 => $model,
502+
]);
503+
504+
$builder = $this
505+
->getMockBuilder(FilterBuilder::class)
506+
->disableOriginalConstructor()
507+
->getMock();
508+
509+
$collection = $this->engine->map($builder, $results, $model);
510+
511+
$this->assertSame(
512+
[$model],
513+
$collection->all()
514+
);
515+
516+
// Assert that an `Eloquent\Database\Collection` is returned
517+
$this->assertInstanceOf(Collection::class, $collection);
518+
}
519+
443520
public function testGetTotalCount()
444521
{
445522
$results = [

0 commit comments

Comments
 (0)