2222use MongoDB \Model \BSONDocument ;
2323use PHPUnit \Framework \Attributes \DataProvider ;
2424
25+ use function array_merge_recursive ;
2526use function serialize ;
2627use function unserialize ;
2728
@@ -52,37 +53,40 @@ public function testSearch(Closure $builder, array $expectedPipeline): void
5253
5354 public function provideSearchPipelines (): iterable
5455 {
55- yield 'simple string ' => [
56- fn () => new Builder (new SearchableModel (), '' ),
56+ $ defaultPipeline = [
5757 [
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 ' => '* ' ,
6964 ],
70- 'count ' => [
71- 'type ' => ' lowerBound ' ,
65+ 'fuzzy ' => [
66+ 'maxEdits ' => 2 ,
7267 ],
7368 ],
74- ],
75- [
76- '$addFields ' => [
77- 'search_meta ' => '$$SEARCH_META ' ,
69+ 'count ' => [
70+ 'type ' => 'lowerBound ' ,
7871 ],
7972 ],
8073 ],
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 ,
8185 ];
8286
8387 yield 'where conditions ' => [
8488 function () {
85- $ builder = new Builder (new SearchableModel (), '' );
89+ $ builder = new Builder (new SearchableModel (), 'lar ' );
8690 $ builder ->where ('foo ' , 'bar ' );
8791 $ builder ->where ('key ' , 'value ' );
8892
@@ -93,20 +97,86 @@ function () {
9397
9498 yield 'where in conditions ' => [
9599 function () {
96- $ builder = new Builder (new SearchableModel (), '' );
100+ $ builder = new Builder (new SearchableModel (), 'lar ' );
97101 $ builder ->where ('foo ' , 'bar ' );
98102 $ builder ->where ('bar ' , 'baz ' );
99103 $ builder ->whereIn ('qux ' , [1 , 2 ]);
100104 $ builder ->whereIn ('quux ' , [1 , 2 ]);
101105
102106 return $ builder ;
103107 },
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+ ]),
105175 ];
106176
107177 yield 'where not in conditions ' => [
108178 function () {
109- $ builder = new Builder (new SearchableModel (), '' );
179+ $ builder = new Builder (new SearchableModel (), 'lar ' );
110180 $ builder ->where ('foo ' , 'bar ' );
111181 $ builder ->where ('bar ' , 'baz ' );
112182 $ builder ->whereIn ('qux ' , [1 , 2 ]);
@@ -115,49 +185,63 @@ function () {
115185
116186 return $ builder ;
117187 },
118- [] ,
188+ $ defaultPipeline ,
119189 ];
120190
121191 yield 'where in conditions without other conditions ' => [
122192 function () {
123- $ builder = new Builder (new SearchableModel (), '' );
193+ $ builder = new Builder (new SearchableModel (), 'lar ' );
124194 $ builder ->whereIn ('qux ' , [1 , 2 ]);
125195 $ builder ->whereIn ('quux ' , [1 , 2 ]);
126196
127197 return $ builder ;
128198 },
129- [] ,
199+ $ defaultPipeline ,
130200 ];
131201
132202 yield 'where not in conditions without other conditions ' => [
133203 function () {
134- $ builder = new Builder (new SearchableModel (), '' );
204+ $ builder = new Builder (new SearchableModel (), 'lar ' );
135205 $ builder ->whereIn ('qux ' , [1 , 2 ]);
136206 $ builder ->whereIn ('quux ' , [1 , 2 ]);
137207 $ builder ->whereNotIn ('eaea ' , [3 ]);
138208
139209 return $ builder ;
140210 },
141- [] ,
211+ $ defaultPipeline ,
142212 ];
143213
144214 yield 'empty where in conditions ' => [
145215 function () {
146- $ builder = new Builder (new SearchableModel (), '' );
216+ $ builder = new Builder (new SearchableModel (), 'lar ' );
147217 $ builder ->whereIn ('qux ' , [1 , 2 ]);
148218 $ builder ->whereIn ('quux ' , [1 , 2 ]);
149219 $ builder ->whereNotIn ('eaea ' , [3 ]);
150220
151221 return $ builder ;
152222 },
153- [] ,
223+ $ defaultPipeline ,
154224 ];
155225
156226 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 ' ],
161245 ];
162246 }
163247
0 commit comments