Skip to content

Commit 1ceb4e0

Browse files
committed
Fix mapIds method and the corresponding test
mapIds should return Collection mapIds needs to know the model's key name mapIds have to have a doc block
1 parent 96a00b8 commit 1ceb4e0

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

src/PostgresEngine.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class PostgresEngine extends Engine
3131
*/
3232
protected $config = [];
3333

34+
/**
35+
* @var \Illuminate\Database\Eloquent\Model
36+
*/
37+
protected $model;
38+
3439
/**
3540
* Create a new instance of PostgresEngine.
3641
*
@@ -195,6 +200,11 @@ public function getTotalCount($results)
195200
*/
196201
protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
197202
{
203+
// We have to preserve the model in order to allow for
204+
// correct behavior of mapIds() method which currently
205+
// does not revceive a model instance
206+
$this->preserveModel($builder->model);
207+
198208
$indexColumn = $this->getIndexColumn($builder->model);
199209

200210
// Build the query
@@ -224,9 +234,19 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
224234
->select($query->toSql(), $bindings->all());
225235
}
226236

237+
/**
238+
* Pluck and return the primary keys of the given results.
239+
*
240+
* @param mixed $results
241+
* @return \Illuminate\Support\Collection
242+
*/
227243
public function mapIds($results)
228244
{
229-
return collect($results)->pluck('id')->values()->all();
245+
$keyName = $this->model ? $this->model->getKeyName() : 'id';
246+
247+
return collect($results)
248+
->pluck($keyName)
249+
->values();
230250
}
231251

232252
/**
@@ -242,11 +262,11 @@ public function map($results, $model)
242262
return Collection::make();
243263
}
244264

245-
$results = collect($results);
246-
247265
$keys = $this->mapIds($results);
248266

249-
$models = $model->whereIn($model->getKeyName(), $keys)
267+
$results = collect($results);
268+
269+
$models = $model->whereIn($model->getKeyName(), $keys->all())
250270
->get()
251271
->keyBy($model->getKeyName());
252272

@@ -428,4 +448,12 @@ protected function config($key, $default = null)
428448
{
429449
return array_get($this->config, $key, $default);
430450
}
451+
452+
/**
453+
* @param \Illuminate\Database\Eloquent\Model $model
454+
*/
455+
protected function preserveModel(Model $model)
456+
{
457+
$this->model = $model;
458+
}
431459
}

tests/PostgresEngineTest.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,11 @@ public function test_search()
8484
{
8585
list($engine, $db) = $this->getEngine();
8686

87-
$db->shouldReceive('table')
88-
->andReturn($table = Mockery::mock('stdClass'));
89-
$db->shouldReceive('raw')
90-
->with('plainto_tsquery(?) query')
91-
->andReturn('plainto_tsquery(?) query');
87+
$table = $this->setDbExpectations($db);
9288

93-
$table->shouldReceive('crossJoin')->with('plainto_tsquery(?) query')->andReturnSelf()
94-
->shouldReceive('select')->with('id')->andReturnSelf()
95-
->shouldReceive('selectRaw')->with('ts_rank(searchable,query) AS rank')->andReturnSelf()
96-
->shouldReceive('selectRaw')->with('COUNT(*) OVER () AS total_count')->andReturnSelf()
97-
->shouldReceive('whereRaw')->andReturnSelf()
98-
->shouldReceive('orderBy')->with('rank', 'desc')->andReturnSelf()
99-
->shouldReceive('orderBy')->with('id')->andReturnSelf()
100-
->shouldReceive('skip')->with(0)->andReturnSelf()
89+
$table->shouldReceive('skip')->with(0)->andReturnSelf()
10190
->shouldReceive('limit')->with(5)->andReturnSelf()
102-
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
103-
->shouldReceive('toSql');
91+
->shouldReceive('where')->with('bar', 1);
10492

10593
$db->shouldReceive('select')->with(null, ['foo', 1]);
10694

@@ -138,9 +126,19 @@ public function test_it_returns_total_count()
138126

139127
public function test_map_ids_returns_right_key()
140128
{
141-
list($engine) = $this->getEngine();
142-
$results = [new TestModel()];
143-
$this->assertEquals([1], $engine->mapIds($results));
129+
list($engine, $db) = $this->getEngine();
130+
131+
$this->setDbExpectations($db);
132+
133+
$db->shouldReceive('select')
134+
->andReturn(json_decode('[{"id": 1}, {"id": 2}]'));
135+
136+
$builder = new Builder(new TestModel, 'foo');
137+
$results = $engine->search($builder);
138+
$ids = $engine->mapIds($results);
139+
140+
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $ids);
141+
$this->assertEquals([1, 2], $ids->all());
144142
}
145143

146144
protected function getEngine($config = [])
@@ -153,6 +151,26 @@ protected function getEngine($config = [])
153151

154152
return [new PostgresEngine($resolver, $config), $db];
155153
}
154+
155+
protected function setDbExpectations($db, $skip = 0, $limit = 5)
156+
{
157+
$db->shouldReceive('table')
158+
->andReturn($table = Mockery::mock('stdClass'));
159+
$db->shouldReceive('raw')
160+
->with('plainto_tsquery(?) query')
161+
->andReturn('plainto_tsquery(?) query');
162+
163+
$table->shouldReceive('crossJoin')->with('plainto_tsquery(?) query')->andReturnSelf()
164+
->shouldReceive('select')->with('id')->andReturnSelf()
165+
->shouldReceive('selectRaw')->with('ts_rank(searchable,query) AS rank')->andReturnSelf()
166+
->shouldReceive('selectRaw')->with('COUNT(*) OVER () AS total_count')->andReturnSelf()
167+
->shouldReceive('whereRaw')->andReturnSelf()
168+
->shouldReceive('orderBy')->with('rank', 'desc')->andReturnSelf()
169+
->shouldReceive('orderBy')->with('id')->andReturnSelf()
170+
->shouldReceive('toSql');
171+
172+
return $table;
173+
}
156174
}
157175

158176
class TestModel extends Model

0 commit comments

Comments
 (0)