@@ -84,23 +84,11 @@ public function test_search()
84
84
{
85
85
list ($ engine , $ db ) = $ this ->getEngine ();
86
86
87
- $ db ->shouldReceive ('table ' )
88
- ->andReturn ($ table = Mockery::mock ('stdClass ' ));
89
- $ db ->shouldReceive ('raw ' )
90
- ->with ('plainto_tsquery(?) query ' )
91
- ->andReturn ('plainto_tsquery(?) query ' );
87
+ $ table = $ this ->setDbExpectations ($ db );
92
88
93
- $ table ->shouldReceive ('crossJoin ' )->with ('plainto_tsquery(?) query ' )->andReturnSelf ()
94
- ->shouldReceive ('select ' )->with ('id ' )->andReturnSelf ()
95
- ->shouldReceive ('selectRaw ' )->with ('ts_rank(searchable,query) AS rank ' )->andReturnSelf ()
96
- ->shouldReceive ('selectRaw ' )->with ('COUNT(*) OVER () AS total_count ' )->andReturnSelf ()
97
- ->shouldReceive ('whereRaw ' )->andReturnSelf ()
98
- ->shouldReceive ('orderBy ' )->with ('rank ' , 'desc ' )->andReturnSelf ()
99
- ->shouldReceive ('orderBy ' )->with ('id ' )->andReturnSelf ()
100
- ->shouldReceive ('skip ' )->with (0 )->andReturnSelf ()
89
+ $ table ->shouldReceive ('skip ' )->with (0 )->andReturnSelf ()
101
90
->shouldReceive ('limit ' )->with (5 )->andReturnSelf ()
102
- ->shouldReceive ('where ' )->with ('bar ' , 1 )->andReturnSelf ()
103
- ->shouldReceive ('toSql ' );
91
+ ->shouldReceive ('where ' )->with ('bar ' , 1 );
104
92
105
93
$ db ->shouldReceive ('select ' )->with (null , ['foo ' , 1 ]);
106
94
@@ -138,9 +126,19 @@ public function test_it_returns_total_count()
138
126
139
127
public function test_map_ids_returns_right_key ()
140
128
{
141
- list ($ engine ) = $ this ->getEngine ();
142
- $ results = [new TestModel ()];
143
- $ this ->assertEquals ([1 ], $ engine ->mapIds ($ results ));
129
+ list ($ engine , $ db ) = $ this ->getEngine ();
130
+
131
+ $ this ->setDbExpectations ($ db );
132
+
133
+ $ db ->shouldReceive ('select ' )
134
+ ->andReturn (json_decode ('[{"id": 1}, {"id": 2}] ' ));
135
+
136
+ $ builder = new Builder (new TestModel , 'foo ' );
137
+ $ results = $ engine ->search ($ builder );
138
+ $ ids = $ engine ->mapIds ($ results );
139
+
140
+ $ this ->assertInstanceOf (\Illuminate \Support \Collection::class, $ ids );
141
+ $ this ->assertEquals ([1 , 2 ], $ ids ->all ());
144
142
}
145
143
146
144
protected function getEngine ($ config = [])
@@ -153,6 +151,26 @@ protected function getEngine($config = [])
153
151
154
152
return [new PostgresEngine ($ resolver , $ config ), $ db ];
155
153
}
154
+
155
+ protected function setDbExpectations ($ db , $ skip = 0 , $ limit = 5 )
156
+ {
157
+ $ db ->shouldReceive ('table ' )
158
+ ->andReturn ($ table = Mockery::mock ('stdClass ' ));
159
+ $ db ->shouldReceive ('raw ' )
160
+ ->with ('plainto_tsquery(?) query ' )
161
+ ->andReturn ('plainto_tsquery(?) query ' );
162
+
163
+ $ table ->shouldReceive ('crossJoin ' )->with ('plainto_tsquery(?) query ' )->andReturnSelf ()
164
+ ->shouldReceive ('select ' )->with ('id ' )->andReturnSelf ()
165
+ ->shouldReceive ('selectRaw ' )->with ('ts_rank(searchable,query) AS rank ' )->andReturnSelf ()
166
+ ->shouldReceive ('selectRaw ' )->with ('COUNT(*) OVER () AS total_count ' )->andReturnSelf ()
167
+ ->shouldReceive ('whereRaw ' )->andReturnSelf ()
168
+ ->shouldReceive ('orderBy ' )->with ('rank ' , 'desc ' )->andReturnSelf ()
169
+ ->shouldReceive ('orderBy ' )->with ('id ' )->andReturnSelf ()
170
+ ->shouldReceive ('toSql ' );
171
+
172
+ return $ table ;
173
+ }
156
174
}
157
175
158
176
class TestModel extends Model
0 commit comments