66use PhpPlatform \SearchQueryParser \FindParams ;
77use PhpPlatform \SearchQueryParser \Parser ;
88use PhpPlatform \Errors \Exceptions \Http \_4XX \BadRequest ;
9+ use PhpPlatform \Persist \RelationalMappingUtil ;
10+ use PhpPlatform \Persist \TransactionManager ;
911
1012class TestParser extends \PHPUnit_Framework_TestCase{
1113
@@ -28,7 +30,14 @@ function testParse($request,$modelClassName, $excludeFromFullTextSearch, $expect
2830 $ this ->assertEquals ($ expectedFindParams ['filters ' ], $ findParams ->filters );
2931 $ this ->assertEquals ($ expectedFindParams ['sort ' ], $ findParams ->sort );
3032 $ this ->assertEquals ($ expectedFindParams ['pagination ' ], $ findParams ->pagination );
31- $ this ->assertEquals ($ expectedFindParams ['where ' ], $ findParams ->where );
33+ if ($ findParams ->where != null ){
34+ $ that = $ this ;
35+ TransactionManager::executeInTransaction (function () use ($ that ,$ expectedFindParams ,$ findParams ){
36+ $ that ->assertEquals ($ expectedFindParams ['where ' ], $ findParams ->where ->asString ($ that ->getColumnNameMappingForTestModels ()));
37+ });
38+ }else {
39+ $ this ->assertNull ($ expectedFindParams ['where ' ]);
40+ }
3241
3342 }catch (BadRequest $ e ){
3443 $ this ->assertEquals ($ expectedException , $ e ->getBody ());
@@ -182,10 +191,82 @@ function parseDataProvider(){
182191 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
183192 null ,
184193 ['filters ' =>[],'sort ' =>['id ' =>'ASC ' ,'m1Id ' =>'DESC ' ],'pagination ' =>null ,'where ' =>null ]
194+ ],
195+
196+ "with pagination " =>[
197+ $ this ->getHttpRequestWithQueryParameters ([
198+ 'p ' =>'2-100 '
199+ ]),
200+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
201+ null ,
202+ ['filters ' =>[],'sort ' =>[],'pagination ' =>['pageNumber ' =>2 ,'pageSize ' =>100 ],'where ' =>null ]
203+ ],
204+ "with pagination wrong format 1 " =>[
205+ $ this ->getHttpRequestWithQueryParameters ([
206+ 'p ' =>'two-100 '
207+ ]),
208+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
209+ null ,
210+ [],
211+ ['p ' =>'invalid ' ]
212+ ],
213+ "with pagination wrong format 2 " =>[
214+ $ this ->getHttpRequestWithQueryParameters ([
215+ 'p ' =>'1- '
216+ ]),
217+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
218+ null ,
219+ [],
220+ ['p ' =>'invalid ' ]
221+ ],
222+ "with pagination wrong format 3 " =>[
223+ $ this ->getHttpRequestWithQueryParameters ([
224+ 'p ' =>'-10 '
225+ ]),
226+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
227+ null ,
228+ [],
229+ ['p ' =>'invalid ' ]
230+ ],
231+ "with pagination wrong format 4 " =>[
232+ $ this ->getHttpRequestWithQueryParameters ([
233+ 'p ' =>'10 '
234+ ]),
235+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
236+ null ,
237+ [],
238+ ['p ' =>'invalid ' ]
239+ ],
240+ "with pagination wrong format 5 " =>[
241+ $ this ->getHttpRequestWithQueryParameters ([
242+ 'p ' =>'10-10-10 '
243+ ]),
244+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
245+ null ,
246+ [],
247+ ['p ' =>'invalid ' ]
248+ ],
249+ "with full text search " =>[
250+ $ this ->getHttpRequestWithQueryParameters ([
251+ 'q ' =>'abcd '
252+ ]),
253+ 'PhpPlatform\Tests\SearchQueryParser\Models\M1 ' ,
254+ null ,
255+ ['filters ' =>[],'sort ' =>[],'pagination ' =>null ,'where ' =>"(m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%') " ]
256+ ],
257+ "with full text search for child class " =>[
258+ $ this ->getHttpRequestWithQueryParameters ([
259+ 'q ' =>'abcd '
260+ ]),
261+ 'PhpPlatform\Tests\SearchQueryParser\Models\M2 ' ,
262+ null ,
263+ ['filters ' =>[],'sort ' =>[],'pagination ' =>null ,'where ' =>"(m2.ADDRESS LIKE '%abcd%') OR (m1.NAME LIKE '%abcd%') OR (m1.USER_NAME LIKE '%abcd%') " ]
185264 ]
186265
266+
267+
187268 ];
188- //return [$cases['with filters with other operators ']];
269+ //return [$cases['with pagination wrong format 3 ']];
189270 return $ cases ;
190271 }
191272
@@ -204,4 +285,17 @@ private function getHttpRequestWithQueryParameters($queryParams){
204285 return HTTPRequest::getInstance ();
205286 }
206287
288+ private function getColumnNameMappingForTestModels (){
289+ $ mapping = array ();
290+
291+ $ classList = RelationalMappingUtil::getClassConfiguration ('PhpPlatform\Tests\SearchQueryParser\Models\M2 ' );
292+
293+ foreach ($ classList as $ className =>$ class ){
294+ $ prefix = $ class ['prefix ' ];
295+ foreach ($ class ['fields ' ] as $ fieldName =>$ field ){
296+ $ mapping ["$ className:: $ fieldName " ] = $ prefix .'. ' .$ field ['columnName ' ];
297+ }
298+ }
299+ return $ mapping ;
300+ }
207301}
0 commit comments