11<?php
22
3- namespace Shokme \ Meilisearch \Tests ;
3+ namespace Meilisearch \ Scout \Tests ;
44
55use MeiliSearch \Client ;
66use Illuminate \Database \Eloquent \Collection ;
77use Laravel \Scout \Builder ;
8- use Shokme \ Meilisearch \Engines \MeilisearchEngine ;
9- use Shokme \ Meilisearch \Tests \Fixtures \SearchableModel ;
8+ use Meilisearch \ Scout \Engines \MeilisearchEngine ;
9+ use Meilisearch \ Scout \Tests \Fixtures \SearchableModel ;
1010use Mockery as m ;
1111use stdClass ;
1212
@@ -22,9 +22,11 @@ public function update_adds_objects_to_index()
2222 {
2323 $ client = m::mock (Client::class);
2424 $ client ->shouldReceive ('getIndex ' )->with ('table ' )->andReturn ($ index = m::mock (stdClass::class));
25- $ index ->shouldReceive ('saveObjects ' )->with ([[
26- 'id ' => 1
27- ]]);
25+ $ index ->shouldReceive ('addDocuments ' )->with ([
26+ [
27+ 'id ' => 1
28+ ]
29+ ]);
2830
2931 $ engine = new MeilisearchEngine ($ client );
3032 $ engine ->update (Collection::make ([new SearchableModel ]));
@@ -47,44 +49,46 @@ public function search_sends_correct_parameters_to_meilisearch()
4749 $ client = m::mock (Client::class);
4850 $ client ->shouldReceive ('getIndex ' )->with ('table ' )->andReturn ($ index = m::mock (stdClass::class));
4951 $ index ->shouldReceive ('search ' )->with ('mustang ' , [
50- 'filters ' => [ 'foo=1 ' ] ,
52+ 'filters ' => 'foo=1 ' ,
5153 ]);
5254
5355 $ engine = new MeilisearchEngine ($ client );
54- $ builder = new Builder (new SearchableModel , 'mustang ' );
55- $ builder ->where ('filters ' , ['foo=1 ' ]);
56+ $ builder = new Builder (new SearchableModel , 'mustang ' , function ($ meilisearch , $ query , $ options ) {
57+ $ options ['filters ' ] = 'foo=1 ' ;
58+
59+ return $ meilisearch ->search ($ query , $ options );
60+ });
5661 $ engine ->search ($ builder );
5762 }
5863
5964 /** @test */
6065 public function map_correctly_maps_results_to_models ()
6166 {
62- $ this ->markTestSkipped ('TODO: try to prevent need of sql ' );
63-
6467 $ client = m::mock (Client::class);
6568 $ engine = new MeilisearchEngine ($ client );
6669
6770 $ model = m::mock (stdClass::class);
68- $ model ->shouldReceive (' getScoutModelsByIds ' )-> andReturn (Collection:: make ([ new SearchableModel ([ ' id ' => 1 ])]) );
69-
71+ $ model ->shouldReceive ([ ' getKeyName ' => ' id ' ] );
72+ $ model -> shouldReceive ( ' getScoutModelsByIds ' )-> andReturn ( $ models = Collection:: make ([ new SearchableModel ([ ' id ' => 1 ])]));
7073 $ builder = m::mock (Builder::class);
7174
72- $ results = $ engine ->map ($ builder , ['nbHits ' => 1 , 'hits ' => [
73- ['id ' => 1 ],
74- ]], new SearchableModel ());
75+ $ results = $ engine ->map ($ builder , [
76+ 'nbHits ' => 1 , 'hits ' => [
77+ ['id ' => 1 ],
78+ ]
79+ ], $ model );
7580
7681 $ this ->assertEquals (1 , count ($ results ));
7782 }
7883
7984 /** @test */
8085 public function map_method_respects_order ()
8186 {
82- $ this ->markTestSkipped ('TODO: try to prevent need of sql ' );
83-
8487 $ client = m::mock (Client::class);
8588 $ engine = new MeilisearchEngine ($ client );
8689
8790 $ model = m::mock (stdClass::class);
91+ $ model ->shouldReceive (['getKeyName ' => 'id ' ]);
8892 $ model ->shouldReceive ('getScoutModelsByIds ' )->andReturn ($ models = Collection::make ([
8993 new SearchableModel (['id ' => 1 ]),
9094 new SearchableModel (['id ' => 2 ]),
@@ -94,12 +98,14 @@ public function map_method_respects_order()
9498
9599 $ builder = m::mock (Builder::class);
96100
97- $ results = $ engine ->map ($ builder , ['nbHits ' => 4 , 'hits ' => [
98- ['id ' => 1 ],
99- ['id ' => 2 ],
100- ['id ' => 4 ],
101- ['id ' => 3 ],
102- ]], new SearchableModel ());
101+ $ results = $ engine ->map ($ builder , [
102+ 'nbHits ' => 4 , 'hits ' => [
103+ ['id ' => 1 ],
104+ ['id ' => 2 ],
105+ ['id ' => 4 ],
106+ ['id ' => 3 ],
107+ ]
108+ ], $ model );
103109
104110 $ this ->assertEquals (4 , count ($ results ));
105111 $ this ->assertEquals ([
0 commit comments