@@ -26,7 +26,10 @@ public function test_update_adds_object_to_index()
26
26
$ db ->shouldReceive ('query ' )
27
27
->andReturn ($ query = Mockery::mock ('stdClass ' ));
28
28
$ query ->shouldReceive ('selectRaw ' )
29
- ->with ("to_tsvector( ?) || setweight(to_tsvector( ?), 'B') AS tsvector " , ['Foo ' , '' ])
29
+ ->with (
30
+ "to_tsvector(COALESCE(?, get_current_ts_config()), ?) || setweight(to_tsvector(COALESCE(?, get_current_ts_config()), ?), ?) AS tsvector " ,
31
+ [null , 'Foo ' , null , '' , 'B ' ]
32
+ )
30
33
->andReturnSelf ();
31
34
$ query ->shouldReceive ('value ' )
32
35
->with ('tsvector ' )
@@ -88,34 +91,62 @@ public function test_search()
88
91
$ limit = 5 ;
89
92
$ table = $ this ->setDbExpectations ($ db , $ skip , $ limit );
90
93
94
+ $ table ->shouldReceive ('skip ' )
95
+ ->with ($ skip )
96
+ ->andReturnSelf ()
97
+ ->shouldReceive ('limit ' )
98
+ ->with ($ limit )
99
+ ->andReturnSelf ()
100
+ ->shouldReceive ('where ' )
101
+ ->with ('bar ' , 1 );
102
+
103
+ $ db ->shouldReceive ('select ' )
104
+ ->with (null , [null , 'foo ' , 1 ]);
105
+
106
+ $ builder = new Builder (new TestModel (), 'foo ' );
107
+ $ builder ->where ('bar ' , 1 )->take (5 );
108
+
109
+ $ engine ->search ($ builder );
110
+ }
111
+
112
+ public function test_search_with_global_config ()
113
+ {
114
+ list ($ engine , $ db ) = $ this ->getEngine (['config ' => 'simple ' ]);
115
+
116
+ $ skip = 0 ;
117
+ $ limit = 5 ;
118
+ $ table = $ this ->setDbExpectations ($ db );
119
+
91
120
$ table ->shouldReceive ('skip ' )->with ($ skip )->andReturnSelf ()
92
121
->shouldReceive ('limit ' )->with ($ limit )->andReturnSelf ()
93
122
->shouldReceive ('where ' )->with ('bar ' , 1 );
94
123
95
- $ db ->shouldReceive ('select ' )->with (null , ['foo ' , 1 ]);
124
+ $ db ->shouldReceive ('select ' )->with (null , ['simple ' , ' foo ' , 1 ]);
96
125
97
126
$ builder = new Builder (new TestModel (), 'foo ' );
98
127
$ builder ->where ('bar ' , 1 )->take (5 );
99
128
100
129
$ engine ->search ($ builder );
101
130
}
102
131
103
- public function test_search_configuration ()
132
+ public function test_search_with_model_config ()
104
133
{
105
- $ searchConfig = 'simple ' ;
106
- list ($ engine , $ db ) = $ this ->getEngine (['search_configuration ' => $ searchConfig ]);
134
+ list ($ engine , $ db ) = $ this ->getEngine (['config ' => 'simple ' ]);
107
135
108
136
$ skip = 0 ;
109
137
$ limit = 5 ;
110
- $ table = $ this ->setDbExpectations ($ db, $ skip , $ limit , " ' " . $ searchConfig . " ', " );
138
+ $ table = $ this ->setDbExpectations ($ db );
111
139
112
140
$ table ->shouldReceive ('skip ' )->with ($ skip )->andReturnSelf ()
113
141
->shouldReceive ('limit ' )->with ($ limit )->andReturnSelf ()
114
142
->shouldReceive ('where ' )->with ('bar ' , 1 );
115
143
116
- $ db ->shouldReceive ('select ' )->with (null , ['foo ' , 1 ]);
144
+ $ db ->shouldReceive ('select ' )->with (null , ['english ' , ' foo ' , 1 ]);
117
145
118
- $ builder = new Builder (new TestModel (), 'foo ' );
146
+ $ model = new TestModel ();
147
+ $ model ->searchableOptions ['config ' ] = 'english ' ;
148
+
149
+ $ builder = new Builder ($ model , 'foo ' );
119
150
$ builder ->where ('bar ' , 1 )->take (5 );
120
151
121
152
$ engine ->search ($ builder );
@@ -175,21 +206,34 @@ protected function getEngine($config = [])
175
206
return [new PostgresEngine ($ resolver , $ config ), $ db ];
176
207
}
177
208
178
- protected function setDbExpectations ($ db, $ skip = 0 , $ limit = 5 , $ searchConfiguration = '' )
209
+ protected function setDbExpectations ($ db )
179
210
{
180
211
$ db ->shouldReceive ('table ' )
181
212
->andReturn ($ table = Mockery::mock ('stdClass ' ));
182
213
$ db ->shouldReceive ('raw ' )
183
- ->with ("plainto_tsquery( $ searchConfiguration ?) query " )
184
- ->andReturn ("plainto_tsquery( $ searchConfiguration ?) query " );
185
-
186
- $ table ->shouldReceive ('crossJoin ' )->with ("plainto_tsquery( $ searchConfiguration ?) query " )->andReturnSelf ()
187
- ->shouldReceive ('select ' )->with ('id ' )->andReturnSelf ()
188
- ->shouldReceive ('selectRaw ' )->with ('ts_rank(searchable,query) AS rank ' )->andReturnSelf ()
189
- ->shouldReceive ('selectRaw ' )->with ('COUNT(*) OVER () AS total_count ' )->andReturnSelf ()
190
- ->shouldReceive ('whereRaw ' )->andReturnSelf ()
191
- ->shouldReceive ('orderBy ' )->with ('rank ' , 'desc ' )->andReturnSelf ()
192
- ->shouldReceive ('orderBy ' )->with ('id ' )->andReturnSelf ()
214
+ ->with ("plainto_tsquery(COALESCE(?, get_current_ts_config()), ?) AS query " )
215
+ ->andReturn ("plainto_tsquery(COALESCE(?, get_current_ts_config()), ?) AS query " );
216
+
217
+ $ table ->shouldReceive ('crossJoin ' )
218
+ ->with ("plainto_tsquery(COALESCE(?, get_current_ts_config()), ?) AS query " )
219
+ ->andReturnSelf ()
220
+ ->shouldReceive ('select ' )
221
+ ->with ('id ' )
222
+ ->andReturnSelf ()
223
+ ->shouldReceive ('selectRaw ' )
224
+ ->with ('ts_rank(searchable,query) AS rank ' )
225
+ ->andReturnSelf ()
226
+ ->shouldReceive ('selectRaw ' )
227
+ ->with ('COUNT(*) OVER () AS total_count ' )
228
+ ->andReturnSelf ()
229
+ ->shouldReceive ('whereRaw ' )
230
+ ->andReturnSelf ()
231
+ ->shouldReceive ('orderBy ' )
232
+ ->with ('rank ' , 'desc ' )
233
+ ->andReturnSelf ()
234
+ ->shouldReceive ('orderBy ' )
235
+ ->with ('id ' )
236
+ ->andReturnSelf ()
193
237
->shouldReceive ('toSql ' );
194
238
195
239
return $ table ;
@@ -200,17 +244,22 @@ class TestModel extends Model
200
244
{
201
245
public $ id = 1 ;
202
246
203
- public $ text = 'Foo ' ;
247
+ // public $text = 'Foo';
204
248
205
- protected $ searchableOptions = [
249
+ public $ searchableOptions = [
206
250
'rank ' => [
207
251
'fields ' => [
208
252
'nullable ' => 'B ' ,
209
253
],
210
254
],
211
255
];
212
256
213
- protected $ searchableAdditionalArray = [];
257
+ public $ searchableArray = [
258
+ 'text ' => 'Foo ' ,
259
+ 'nullable ' => null ,
260
+ ];
261
+
262
+ public $ searchableAdditionalArray = [];
214
263
215
264
public function searchableAs ()
216
265
{
@@ -234,10 +283,7 @@ public function getTable()
234
283
235
284
public function toSearchableArray ()
236
285
{
237
- return [
238
- 'text ' => $ this ->text ,
239
- 'nullable ' => null ,
240
- ];
286
+ return $ this ->searchableArray ;
241
287
}
242
288
243
289
public function searchableOptions ()
0 commit comments