77use Laravel \Scout \Builder ;
88use Mockery ;
99use Tests \Fixtures \TestModel ;
10+ use Illuminate \Support \LazyCollection ;
1011
1112class ElasticsearchEngineTest extends AbstractTestCase
1213{
@@ -88,7 +89,7 @@ public function test_builder_callback_can_manipulate_search_parameters_to_elasti
8889 {
8990 /** @var \Elasticsearch\Client|\Mockery\MockInterface $client */
9091 $ client = Mockery::mock (\Elasticsearch \Client::class);
91- $ client ->shouldReceive ('search ' )->with ('modified_by_callback ' );
92+ $ client ->shouldReceive ('search ' )->with ([ 'modified_by_callback ' ] );
9293
9394 $ engine = new ElasticsearchEngine ($ client );
9495 $ builder = new \Laravel \Scout \Builder (
@@ -97,7 +98,7 @@ public function test_builder_callback_can_manipulate_search_parameters_to_elasti
9798 function (\Elasticsearch \Client $ client , $ query , $ params ) {
9899 $ this ->assertNotEmpty ($ params );
99100 $ this ->assertEquals ('huayra ' , $ query );
100- $ params = 'modified_by_callback ' ;
101+ $ params = [ 'modified_by_callback ' ] ;
101102
102103 return $ client ->search ($ params );
103104 }
@@ -131,4 +132,53 @@ public function test_map_correctly_maps_results_to_models()
131132
132133 $ this ->assertEquals (1 , count ($ results ));
133134 }
135+
136+ public function test_lazy_map_correctly_maps_results_to_models ()
137+ {
138+ $ client = Mockery::mock ('Elasticsearch\Client ' );
139+ $ engine = new ElasticsearchEngine ($ client );
140+
141+ $ builder = Mockery::mock (Builder::class);
142+
143+ $ model = Mockery::mock ('Illuminate\Database\Eloquent\Model ' );
144+ $ model ->shouldReceive ('getScoutKey ' )->andReturn ('1 ' );
145+ $ model ->shouldReceive ('queryScoutModelsByIds ' )->andReturn ($ model );
146+ $ model ->shouldReceive ('cursor ' )->andReturn ($ model );
147+ $ model ->shouldReceive ('filter ' )->andReturn ($ model );
148+ $ model ->shouldReceive ('sortBy ' )->andReturn ($ model );
149+ $ model ->shouldReceive ('values ' )->andReturn (LazyCollection::make ([1 ]));
150+
151+ $ results = $ engine ->lazyMap ($ builder , [
152+ 'hits ' => [
153+ 'total ' => 1 ,
154+ 'hits ' => [
155+ [
156+ '_id ' => '1 ' ,
157+ ],
158+ ],
159+ ],
160+ ], $ model );
161+
162+ $ this ->assertCount (1 , $ results );
163+ }
164+
165+ public function test_lazy_map_correctly_maps_empty ()
166+ {
167+ $ client = Mockery::mock ('Elasticsearch\Client ' );
168+ $ engine = new ElasticsearchEngine ($ client );
169+
170+ $ builder = Mockery::mock (Builder::class);
171+
172+ $ model = Mockery::mock ('Illuminate\Database\Eloquent\Model ' );
173+ $ model ->shouldReceive ('newCollection ' )->andReturn (collect ([]));
174+
175+ $ results = $ engine ->lazyMap ($ builder , [
176+ 'hits ' => [
177+ 'total ' => 0 ,
178+ 'hits ' => [],
179+ ],
180+ ], $ model );
181+
182+ $ this ->assertCount (0 , $ results );
183+ }
134184}
0 commit comments