22
22
use MongoDB \Model \BSONDocument ;
23
23
use PHPUnit \Framework \Attributes \DataProvider ;
24
24
25
+ use function array_merge_recursive ;
25
26
use function serialize ;
26
27
use function unserialize ;
27
28
@@ -52,37 +53,40 @@ public function testSearch(Closure $builder, array $expectedPipeline): void
52
53
53
54
public function provideSearchPipelines (): iterable
54
55
{
55
- yield 'simple string ' => [
56
- fn () => new Builder (new SearchableModel (), '' ),
56
+ $ defaultPipeline = [
57
57
[
58
- [
59
- '$search ' => [
60
- 'index ' => 'scout ' ,
61
- 'text ' => [
62
- 'query ' => '' ,
63
- 'path ' => [
64
- 'wildcard ' => '* ' ,
65
- ],
66
- 'fuzzy ' => [
67
- 'maxEdits ' => 2 ,
68
- ],
58
+ '$search ' => [
59
+ 'index ' => 'scout ' ,
60
+ 'text ' => [
61
+ 'query ' => 'lar ' ,
62
+ 'path ' => [
63
+ 'wildcard ' => '* ' ,
69
64
],
70
- 'count ' => [
71
- 'type ' => ' lowerBound ' ,
65
+ 'fuzzy ' => [
66
+ 'maxEdits ' => 2 ,
72
67
],
73
68
],
74
- ],
75
- [
76
- '$addFields ' => [
77
- 'search_meta ' => '$$SEARCH_META ' ,
69
+ 'count ' => [
70
+ 'type ' => 'lowerBound ' ,
78
71
],
79
72
],
80
73
],
74
+ [
75
+ '$addFields ' => [
76
+ 'search_meta ' => '$$SEARCH_META ' ,
77
+ ],
78
+ ],
79
+ ];
80
+
81
+ yield 'simple string ' => [
82
+ fn
83
+ () => new Builder (new SearchableModel (), 'lar ' ),
84
+ $ defaultPipeline ,
81
85
];
82
86
83
87
yield 'where conditions ' => [
84
88
function () {
85
- $ builder = new Builder (new SearchableModel (), '' );
89
+ $ builder = new Builder (new SearchableModel (), 'lar ' );
86
90
$ builder ->where ('foo ' , 'bar ' );
87
91
$ builder ->where ('key ' , 'value ' );
88
92
@@ -93,20 +97,86 @@ function () {
93
97
94
98
yield 'where in conditions ' => [
95
99
function () {
96
- $ builder = new Builder (new SearchableModel (), '' );
100
+ $ builder = new Builder (new SearchableModel (), 'lar ' );
97
101
$ builder ->where ('foo ' , 'bar ' );
98
102
$ builder ->where ('bar ' , 'baz ' );
99
103
$ builder ->whereIn ('qux ' , [1 , 2 ]);
100
104
$ builder ->whereIn ('quux ' , [1 , 2 ]);
101
105
102
106
return $ builder ;
103
107
},
104
- [],
108
+ array_merge_recursive ($ defaultPipeline , [
109
+ [
110
+ '$search ' => [
111
+ 'compound ' => [
112
+ 'must ' => [
113
+ [
114
+ 'text ' => [
115
+ 'query ' => 'lar ' ,
116
+ 'path ' => [
117
+ 'wildcard ' => '* ' ,
118
+ ],
119
+ 'fuzzy ' => [
120
+ 'maxEdits ' => 2 ,
121
+ ],
122
+ ],
123
+ ],
124
+ [
125
+ 'text ' => [
126
+ 'query ' => 'bar ' ,
127
+ 'path ' => [
128
+ 'wildcard ' => '* ' ,
129
+ ],
130
+ 'fuzzy ' => [
131
+ 'maxEdits ' => 2 ,
132
+ ],
133
+ ],
134
+ ],
135
+ [
136
+ 'text ' => [
137
+ 'query ' => 'baz ' ,
138
+ 'path ' => [
139
+ 'wildcard ' => '* ' ,
140
+ ],
141
+ 'fuzzy ' => [
142
+ 'maxEdits ' => 2 ,
143
+ ],
144
+ ],
145
+ ],
146
+ ],
147
+ 'should ' => [
148
+ [
149
+ 'text ' => [
150
+ 'query ' => '1 ' ,
151
+ 'path ' => [
152
+ 'wildcard ' => '* ' ,
153
+ ],
154
+ 'fuzzy ' => [
155
+ 'maxEdits ' => 2 ,
156
+ ],
157
+ ],
158
+ ],
159
+ [
160
+ 'text ' => [
161
+ 'query ' => '2 ' ,
162
+ 'path ' => [
163
+ 'wildcard ' => '* ' ,
164
+ ],
165
+ 'fuzzy ' => [
166
+ 'maxEdits ' => 2 ,
167
+ ],
168
+ ],
169
+ ],
170
+ ],
171
+ ],
172
+ ],
173
+ ],
174
+ ]),
105
175
];
106
176
107
177
yield 'where not in conditions ' => [
108
178
function () {
109
- $ builder = new Builder (new SearchableModel (), '' );
179
+ $ builder = new Builder (new SearchableModel (), 'lar ' );
110
180
$ builder ->where ('foo ' , 'bar ' );
111
181
$ builder ->where ('bar ' , 'baz ' );
112
182
$ builder ->whereIn ('qux ' , [1 , 2 ]);
@@ -115,49 +185,63 @@ function () {
115
185
116
186
return $ builder ;
117
187
},
118
- [] ,
188
+ $ defaultPipeline ,
119
189
];
120
190
121
191
yield 'where in conditions without other conditions ' => [
122
192
function () {
123
- $ builder = new Builder (new SearchableModel (), '' );
193
+ $ builder = new Builder (new SearchableModel (), 'lar ' );
124
194
$ builder ->whereIn ('qux ' , [1 , 2 ]);
125
195
$ builder ->whereIn ('quux ' , [1 , 2 ]);
126
196
127
197
return $ builder ;
128
198
},
129
- [] ,
199
+ $ defaultPipeline ,
130
200
];
131
201
132
202
yield 'where not in conditions without other conditions ' => [
133
203
function () {
134
- $ builder = new Builder (new SearchableModel (), '' );
204
+ $ builder = new Builder (new SearchableModel (), 'lar ' );
135
205
$ builder ->whereIn ('qux ' , [1 , 2 ]);
136
206
$ builder ->whereIn ('quux ' , [1 , 2 ]);
137
207
$ builder ->whereNotIn ('eaea ' , [3 ]);
138
208
139
209
return $ builder ;
140
210
},
141
- [] ,
211
+ $ defaultPipeline ,
142
212
];
143
213
144
214
yield 'empty where in conditions ' => [
145
215
function () {
146
- $ builder = new Builder (new SearchableModel (), '' );
216
+ $ builder = new Builder (new SearchableModel (), 'lar ' );
147
217
$ builder ->whereIn ('qux ' , [1 , 2 ]);
148
218
$ builder ->whereIn ('quux ' , [1 , 2 ]);
149
219
$ builder ->whereNotIn ('eaea ' , [3 ]);
150
220
151
221
return $ builder ;
152
222
},
153
- [] ,
223
+ $ defaultPipeline ,
154
224
];
155
225
156
226
yield 'exclude soft-deleted ' => [
157
- function () {
158
- return new Builder (new SearchableModel (), '' , softDelete: true );
159
- },
160
- [],
227
+ fn
228
+ () => new Builder (new SearchableModel (), '' , softDelete: true ),
229
+ array_merge_recursive (
230
+ $ defaultPipeline ,
231
+ ['$search ' => ['compound ' => ['filter ' => [['equals ' => ['path ' => '__soft_deleted ' , 'value ' => 1 ]]]]]],
232
+ ),
233
+ ];
234
+
235
+ yield 'with callback ' => [
236
+ fn () => new Builder (new SearchableModel (), 'query ' , callback: function (...$ args ) {
237
+ $ this ->assertCount (3 , $ args );
238
+ $ this ->assertInstanceOf (Collection::class, $ args [0 ]);
239
+ $ this ->assertSame ('query ' , $ args [1 ]);
240
+ $ this ->assertNull ($ args [2 ]);
241
+
242
+ return $ args [0 ]->aggregate (['pipeline ' ], self ::EXPECTED_SEARCH_OPTIONS );
243
+ }),
244
+ ['pipeline ' ],
161
245
];
162
246
}
163
247
0 commit comments