Skip to content

Commit d92d5f5

Browse files
Merge pull request #8 from willvincent/master
Fix tests, add whereIn, whereNotIn and queryCallback support
2 parents c677cde + 2c7fec0 commit d92d5f5

File tree

4 files changed

+126
-24
lines changed

4 files changed

+126
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.phpunit.result.cache
33
composer.lock
44
.DS_Store
5+
.idea

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
],
3030
"require": {
3131
"php": "^8.1|^8.2|^8.3|^8.4",
32-
"illuminate/contracts": "^10|^11",
33-
"illuminate/database": "^10|^11",
34-
"illuminate/support": "^10|^11",
35-
"laravel/scout": "^10|^11"
32+
"illuminate/contracts": "^10|^11|^12",
33+
"illuminate/database": "^10|^11|^12",
34+
"illuminate/support": "^10|^11|^12",
35+
"laravel/scout": "^10|^11|^12"
3636
},
3737
"require-dev": {
3838
"laravel/pint": "^1.18",

src/PostgresEngine.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,34 @@ protected function performSearch(Builder $builder, ?int $perPage = 0, int $page
365365
->selectRaw('COUNT(*) OVER () AS total_count')
366366
->whereRaw("{$indexColumn} @@ \"tsquery\"");
367367

368+
// Apply query callback if set
369+
if ($builder->queryCallback) {
370+
call_user_func($builder->queryCallback, $builder);
371+
}
372+
368373
// Apply where clauses that were set on the builder instance if any
369374
foreach ($builder->wheres as $key => $value) {
375+
if ($key == '__soft_deleted') {
376+
if ($this->usesSoftDeletes($builder->model)) {
377+
if ($value == 1) {
378+
$query->whereNotNull($builder->model->getDeletedAtColumn());
379+
} else {
380+
$query->whereNull($builder->model->getDeletedAtColumn());
381+
}
382+
}
383+
continue;
384+
}
370385
$query->where($key, $value);
371386
}
372387

373-
// If parsed documents are being stored in the model's table
374-
if (! $this->isExternalIndex($builder->model)) {
375-
// and the model uses soft deletes we need to exclude trashed rows
376-
if ($this->usesSoftDeletes($builder->model)) {
377-
$query->whereNull($builder->model->getDeletedAtColumn());
378-
}
388+
// Apply whereIn clauses that were set on the builder instance if any
389+
foreach ($builder->whereIns as $key => $value) {
390+
$query->whereIn($key, $value);
391+
}
392+
393+
// Apply whereNoIn clauses that were set on the builder instance if any
394+
foreach ($builder->whereNotIns as $key => $value) {
395+
$query->whereNotIn($key, $value);
379396
}
380397

381398
// Apply order by clauses that were set on the builder instance if any

tests/PostgresEngineTest.php

Lines changed: 98 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ public function update_adds_object_to_index()
3232
[$engine, $db] = $this->getEngine();
3333

3434
$db->shouldReceive('query')
35-
->andReturn($query = Mockery::mock('stdClass'));
35+
->andReturn($query = Mockery::mock('stdClass'))->once();
3636
$query->shouldReceive('selectRaw')
3737
->with(
3838
'to_tsvector(COALESCE(?, get_current_ts_config()), ?) || setweight(to_tsvector(COALESCE(?, get_current_ts_config()), ?), ?) AS tsvector',
3939
[null, 'Foo', null, '', 'B']
4040
)
41-
->andReturnSelf();
41+
->andReturnSelf()->once();
4242
$query->shouldReceive('value')
4343
->with('tsvector')
44-
->andReturn('foo');
44+
->andReturn('foo')->once();
4545

4646
$db->shouldReceive('table')
4747
->andReturn($table = Mockery::mock('stdClass'));
4848
$table->shouldReceive('where')
4949
->with('id', '=', 1)
50-
->andReturnSelf();
50+
->andReturnSelf()->once();
5151

5252
$table->shouldReceive('update')
53-
->with(['searchable' => 'foo']);
53+
->with(['searchable' => 'foo'])->once();
5454

5555
$engine->update(Collection::make([new TestModel]));
5656
}
@@ -62,7 +62,7 @@ public function update_do_nothing_if_index_maintenance_turned_off_globally()
6262
{
6363
[$engine] = $this->getEngine(['maintain_index' => false]);
6464

65-
$engine->update(Collection::make([new TestModel]));
65+
$this->assertNull($engine->update(Collection::make([new TestModel])));
6666
}
6767

6868
/**
@@ -73,12 +73,15 @@ public function delete_removes_object_from_index()
7373
[$engine, $db] = $this->getEngine();
7474

7575
$db->shouldReceive('table')
76-
->andReturn($table = Mockery::mock('stdClass'));
76+
->andReturn($table = Mockery::mock('stdClass'))
77+
->once();
7778
$table->shouldReceive('whereIn')
7879
->with('id', [1])
79-
->andReturnSelf();
80+
->andReturnSelf()
81+
->once();
8082
$table->shouldReceive('update')
81-
->with(['searchable' => null]);
83+
->with(['searchable' => null])
84+
->once();
8285

8386
$engine->delete(Collection::make([new TestModel]));
8487
}
@@ -142,7 +145,8 @@ public function search()
142145
->shouldReceive('getBindings')->andReturn([null, 'foo', 1, 'qux']);
143146

144147
$db->shouldReceive('select')
145-
->with(null, $table->getBindings());
148+
->with(null, $table->getBindings())
149+
->once();
146150

147151
$builder = new Builder(new TestModel, 'foo');
148152
$builder->where('bar', 1)
@@ -166,7 +170,8 @@ public function search_with_order_by()
166170
->shouldReceive('getBindings')->andReturn([null, 'foo']);
167171

168172
$db->shouldReceive('select')
169-
->with(null, $table->getBindings());
173+
->with(null, $table->getBindings())
174+
->once();
170175

171176
$builder = new Builder(new TestModel, 'foo');
172177
$builder->orderBy('bar', 'desc')
@@ -175,6 +180,85 @@ public function search_with_order_by()
175180
$engine->search($builder);
176181
}
177182

183+
/**
184+
* @test
185+
*/
186+
public function search_with_queryCallback()
187+
{
188+
[$engine, $db] = $this->getEngine();
189+
190+
$skip = 0;
191+
$limit = 5;
192+
$table = $this->setDbExpectations($db);
193+
194+
$table->shouldReceive('skip')->with($skip)->andReturnSelf()
195+
->shouldReceive('limit')->with($limit)->andReturnSelf()
196+
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
197+
->shouldReceive('where')->with('baz', 'qux')
198+
->shouldReceive('getBindings')->andReturn([null, 'foo', 1, 'qux']);
199+
200+
$db->shouldReceive('select')
201+
->with(null, $table->getBindings())
202+
->once();
203+
204+
$builder = new Builder(new TestModel, 'foo');
205+
$builder->query(function ($q) {
206+
$q->where('bar', 1)
207+
->where('baz', 'qux')
208+
->take(5);
209+
});
210+
211+
$engine->search($builder);
212+
}
213+
214+
/**
215+
* @test
216+
*/
217+
public function search_with_whereIn()
218+
{
219+
[$engine, $db] = $this->getEngine();
220+
221+
$skip = 0;
222+
$limit = 5;
223+
$table = $this->setDbExpectations($db);
224+
225+
$table->shouldReceive('whereIn')->with('bar', [1])->andReturnSelf()
226+
->shouldReceive('getBindings')->andReturn([null, 'foo', [1]]);
227+
228+
$db->shouldReceive('select')
229+
->with(null, $table->getBindings())
230+
->once();
231+
232+
$builder = new Builder(new TestModel, 'foo');
233+
$builder->whereIn('bar', [1]);
234+
235+
$engine->search($builder);
236+
}
237+
238+
/**
239+
* @test
240+
*/
241+
public function search_with_whereNotIn()
242+
{
243+
[$engine, $db] = $this->getEngine();
244+
245+
$skip = 0;
246+
$limit = 5;
247+
$table = $this->setDbExpectations($db);
248+
249+
$table->shouldReceive('whereNotIn')->with('bar', [1])->andReturnSelf()
250+
->shouldReceive('getBindings')->andReturn([null, 'foo', [1]]);
251+
252+
$db->shouldReceive('select')
253+
->with(null, $table->getBindings())
254+
->once();
255+
256+
$builder = new Builder(new TestModel, 'foo');
257+
$builder->whereNotIn('bar', [1]);
258+
259+
$engine->search($builder);
260+
}
261+
178262
/**
179263
* @test
180264
*/
@@ -191,7 +275,7 @@ public function search_with_global_config()
191275
->shouldReceive('where')->with('bar', 1)
192276
->shouldReceive('getBindings')->andReturn(['simple', 'foo', 1]);
193277

194-
$db->shouldReceive('select')->with(null, $table->getBindings());
278+
$db->shouldReceive('select')->with(null, $table->getBindings())->once();
195279

196280
$builder = new Builder(new TestModel, 'foo');
197281
$builder->where('bar', 1)->take(5);
@@ -215,7 +299,7 @@ public function search_with_model_config()
215299
->shouldReceive('where')->with('bar', 1)
216300
->shouldReceive('getBindings')->andReturn(['english', 'foo', 1]);
217301

218-
$db->shouldReceive('select')->with(null, $table->getBindings());
302+
$db->shouldReceive('select')->with(null, $table->getBindings())->once();
219303

220304
$model = new TestModel;
221305
$model->searchableOptions['config'] = 'english';
@@ -241,7 +325,7 @@ public function search_with_soft_deletes()
241325
->shouldReceive('whereNull')->with('deleted_at')
242326
->shouldReceive('getBindings')->andReturn([null, 'foo', 1]);
243327

244-
$db->shouldReceive('select')->with(null, $table->getBindings());
328+
$db->shouldReceive('select')->with(null, $table->getBindings())->once();
245329

246330
$builder = new Builder(new SoftDeletableTestModel, 'foo');
247331
$builder->where('bar', 1)->take(5);

0 commit comments

Comments
 (0)