1+ <?php
2+ namespace PhpPlatform \Tests \SearchQueryParser ;
3+
4+
5+ use PhpPlatform \RESTFul \HTTPRequest ;
6+ use PhpPlatform \SearchQueryParser \FindParams ;
7+ use PhpPlatform \SearchQueryParser \Parser ;
8+
9+ class TestParser extends \PHPUnit_Framework_TestCase{
10+
11+ /**
12+ * @dataProvider parseDataProvider
13+ *
14+ * @param HTTPRequest|callable $request
15+ * @param string $modelClassName
16+ * @param string[] $excludeFromFullTextSearch
17+ * @param array $expectedFindParams
18+ * @param string $expectedException
19+ */
20+ function testParse ($ request ,$ modelClassName , $ excludeFromFullTextSearch , $ expectedFindParams ,$ expectedException = null ){
21+ if (is_callable ($ request )){
22+ $ request = call_user_func ($ request );
23+ }
24+ try {
25+ $ findParams = Parser::parse ($ request , $ modelClassName , $ excludeFromFullTextSearch );
26+
27+ $ this ->assertEquals ($ expectedFindParams ['filters ' ], $ findParams ->filters );
28+ $ this ->assertEquals ($ expectedFindParams ['sort ' ], $ findParams ->sort );
29+ $ this ->assertEquals ($ expectedFindParams ['pagination ' ], $ findParams ->pagination );
30+ $ this ->assertEquals ($ expectedFindParams ['where ' ], $ findParams ->where );
31+
32+ }catch (\Exception $ e ){
33+ $ this ->assertEquals ($ expectedException , $ e ->getMessage ());
34+ }
35+ }
36+
37+ function parseDataProvider (){
38+ return [
39+ "without any search params " =>[
40+ $ this ->getHttpRequestWithQueryParameters ([
41+
42+ ]),
43+ 'PhpPlatform\Tests\SearchQueryParser\Models\M1 ' ,
44+ null ,
45+ ['filters ' =>[],'sort ' =>[],'pagination ' =>null ,'where ' =>null ]
46+ ],
47+ "with filters " =>[
48+ $ this ->getHttpRequestWithQueryParameters ([
49+ 'f ' =>base64_encode (json_encode (['name ' =>'myName ' ]))
50+ ]),
51+ 'PhpPlatform\Tests\SearchQueryParser\Models\M1 ' ,
52+ null ,
53+ ['filters ' =>['name ' =>'myName ' ],'sort ' =>[],'pagination ' =>null ,'where ' =>null ]
54+ ]
55+ ];
56+ }
57+
58+ /**
59+ * @param string[][] $queryParams
60+ *
61+ * @return HTTPRequest
62+ */
63+ private function getHttpRequestWithQueryParameters ($ queryParams ){
64+
65+ $ httpRequestStaticInstance = new \ReflectionProperty ('PhpPlatform\RESTFul\HTTPRequest ' , 'instance ' );
66+ $ httpRequestStaticInstance ->setAccessible (true );
67+ $ httpRequestStaticInstance ->setValue (null , null );
68+
69+ $ _GET = $ queryParams ;
70+ return HTTPRequest::getInstance ();
71+ }
72+
73+ }
0 commit comments