Skip to content

Commit 8abcebb

Browse files
committed
more tests
1 parent 58d350b commit 8abcebb

File tree

4 files changed

+195
-25
lines changed

4 files changed

+195
-25
lines changed

src/SearchQueryParser/Parser.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,10 @@ class Parser {
2323
*/
2424
static public function parse($request,$modelClassName,$excludeFromFullTextSearch = array()){
2525
$findParams = new FindParams();
26-
try{
27-
// full text search query
28-
$findParams->where = self::parseFullTextSearch($request, $modelClassName, $excludeFromFullTextSearch);
29-
$findParams->filters = self::parseFilters($request, $modelClassName);
30-
$findParams->sort = self::parseSort($request, $modelClassName);
31-
$findParams->pagination = self::parsePagination($request);
32-
}catch (\Exception $e){
33-
throw new BadRequest("Bad Search Params");
34-
}
26+
$findParams->where = self::parseFullTextSearch($request, $modelClassName, $excludeFromFullTextSearch);
27+
$findParams->filters = self::parseFilters($request, $modelClassName);
28+
$findParams->sort = self::parseSort($request, $modelClassName);
29+
$findParams->pagination = self::parsePagination($request);
3530
return $findParams;
3631
}
3732

@@ -46,7 +41,10 @@ private static function parseFullTextSearch($request,$modelClassName,$excludeFro
4641
$classList = RelationalMappingUtil::getClassConfiguration($modelClassName);
4742
foreach ($classList as $className => $class){
4843
foreach ($class['fields'] as $fieldName => $field){
49-
if(RelationalMappingUtil::_isGet($field) && !RelationalMappingUtil::_isAutoIncrement($field) && !in_array($fieldName, $excludeFromFullTextSearch)){
44+
if(RelationalMappingUtil::_isGet($field) &&
45+
!RelationalMappingUtil::_isAutoIncrement($field) &&
46+
!RelationalMappingUtil::_isReference($field) &&
47+
!in_array($fieldName, $excludeFromFullTextSearch)){
5048
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($className, $field), $fullTextSearchQuery]);
5149
}
5250
}
@@ -63,7 +61,7 @@ private static function parseFilters($request,$modelClassName){
6361
$fieldSpecificFilters = base64_decode($fieldSpecificFilters);
6462
$fieldSpecificFilters = json_decode($fieldSpecificFilters,true);
6563
if(!is_array($fieldSpecificFilters)){
66-
throw new BadInputException("query parameter f is invalid");
64+
throw new BadRequest(['f'=>'invalid']);
6765
}
6866

6967
$classList = RelationalMappingUtil::getClassConfiguration($modelClassName);
@@ -78,22 +76,22 @@ private static function parseFilters($request,$modelClassName){
7876
foreach ($filterValue as $operator=>$operands){
7977
// expression validates for the valid expression syntax
8078
try{
81-
new Expression($operator, [new Field($className, $field), $operands]);
79+
new Expression($operator, [new Field($className, $fieldName), $operands]);
8280
}catch (\Exception $e){
83-
throw new BadRequest("query parameter f is invalid");
81+
throw new BadRequest(['f'=>'invalid']);
8482
}
8583
}
8684
$filters[$fieldName] = $filterValue;
8785
}else{
88-
throw new BadRequest("query parameter f is invalid");
86+
throw new BadRequest(['f'=>'invalid']);
8987
}
9088
unset($fieldSpecificFilters[$fieldName]);
9189
}
9290
}
9391
}
9492

9593
if(count($fieldSpecificFilters) != 0){
96-
throw new BadRequest("query parameter f is invalid");
94+
throw new BadRequest(['f'=>'invalid']);
9795
}
9896
}
9997
return $filters;
@@ -106,25 +104,25 @@ private static function parseSort($request,$modelClassName){
106104
$sortParams= base64_decode($sortParams);
107105
$sortParams= json_decode($sortParams,true);
108106
if(!is_array($sortParams)){
109-
throw new BadInputException("query parameter s is invalid");
107+
throw new BadRequest(['s'=>'invalid']);
110108
}
111109

112110
$classList = RelationalMappingUtil::getClassConfiguration($modelClassName);
113111

114112
foreach ($classList as $class){
115113
foreach ($class['fields'] as $fieldName=>$field){
116-
if(RelationalMappingUtil::_isGet($field) && in_array($fieldName, $sortParams)){
114+
if(RelationalMappingUtil::_isGet($field) && array_key_exists($fieldName, $sortParams)){
117115
$sortValue = $sortParams[$fieldName];
118116
if($sortValue != Model::SORTBY_ASC && $sortValue != Model::SORTBY_DESC){
119-
throw new BadRequest("query parameter s is invalid");
117+
throw new BadRequest(['s'=>'invalid']);
120118
}
121-
$sort[$field] = $sortValue;
119+
$sort[$fieldName] = $sortValue;
122120
unset($sortParams[$fieldName]);
123121
}
124122
}
125123
}
126124
if(count($sortParams) != 0){
127-
throw new BadRequest("query parameter s is invalid");
125+
throw new BadRequest(['s'=>'invalid']);
128126
}
129127
}
130128
return $sort;
@@ -138,7 +136,7 @@ private static function parsePagination($request){
138136
if(count($paginationParam) != 2 ||
139137
(!is_numeric($paginationParam[0]) || !is_int($paginationParam[0]+0)) ||
140138
(!is_numeric($paginationParam[0]) || !is_int($paginationParam[1]+0)) ){
141-
throw new BadRequest('query parameter p is invalid');
139+
throw new BadRequest(['p'=>'invalid']);
142140
}
143141
$pagination = array('pageNumber'=>$paginationParam[0],'pageSize'=>$paginationParam[1]);
144142
}

tests/SearchQueryParser/Models/M1.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22
namespace PhpPlatform\Tests\SearchQueryParser\Models;
33

4+
use PhpPlatform\Persist\Model;
5+
46
/**
57
* @tableName M1
68
* @prefix m1
79
*/
8-
class M1 {
10+
class M1 extends Model{
911

1012
/**
1113
* @columnName ID
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace PhpPlatform\Tests\SearchQueryParser\Models;
3+
4+
/**
5+
* @tableName M2
6+
* @prefix m2
7+
*/
8+
class M2 extends M1{
9+
10+
/**
11+
* @columnName ID
12+
* @type bigint
13+
* @primary
14+
* @autoIncrement
15+
* @get
16+
*/
17+
private $id = null;
18+
19+
/**
20+
* @columnName M1_ID
21+
* @type bigint
22+
* @reference
23+
* @get
24+
*/
25+
private $m1Id = null;
26+
27+
/**
28+
* @columnName ADDRESS
29+
* @type varchar
30+
* @set
31+
* @get
32+
*/
33+
private $address = null;
34+
35+
36+
}

tests/SearchQueryParser/TestParser.php

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpPlatform\RESTFul\HTTPRequest;
66
use PhpPlatform\SearchQueryParser\FindParams;
77
use PhpPlatform\SearchQueryParser\Parser;
8+
use PhpPlatform\Errors\Exceptions\Http\_4XX\BadRequest;
89

910
class TestParser extends \PHPUnit_Framework_TestCase{
1011

@@ -29,13 +30,13 @@ function testParse($request,$modelClassName, $excludeFromFullTextSearch, $expect
2930
$this->assertEquals($expectedFindParams['pagination'], $findParams->pagination);
3031
$this->assertEquals($expectedFindParams['where'], $findParams->where);
3132

32-
}catch (\Exception $e){
33-
$this->assertEquals($expectedException, $e->getMessage());
33+
}catch (BadRequest $e){
34+
$this->assertEquals($expectedException, $e->getBody());
3435
}
3536
}
3637

3738
function parseDataProvider(){
38-
return [
39+
$cases = [
3940
"without any search params"=>[
4041
$this->getHttpRequestWithQueryParameters([
4142

@@ -51,8 +52,141 @@ function parseDataProvider(){
5152
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
5253
null,
5354
['filters'=>['name'=>'myName'],'sort'=>[],'pagination'=>null,'where'=>null]
55+
],
56+
"with filters with invalid field"=>[
57+
$this->getHttpRequestWithQueryParameters([
58+
'f'=>base64_encode(json_encode(['address'=>'myAddress']))
59+
]),
60+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
61+
null,
62+
[],
63+
['f'=>'invalid']
64+
],
65+
"with filters without base64 encoded"=>[
66+
$this->getHttpRequestWithQueryParameters([
67+
'f'=>json_encode(['name'=>'myName'])
68+
]),
69+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
70+
null,
71+
[],
72+
['f'=>'invalid']
73+
],
74+
"with filters with invalid filter value"=>[
75+
$this->getHttpRequestWithQueryParameters([
76+
'f'=>base64_encode(json_encode(['name'=>['and'=>'myName']]))
77+
]),
78+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
79+
null,
80+
[],
81+
['f'=>'invalid']
82+
],
83+
"with filters for non get fields"=>[
84+
$this->getHttpRequestWithQueryParameters([
85+
'f'=>base64_encode(json_encode(['password'=>'myName']))
86+
]),
87+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
88+
null,
89+
[],
90+
['f'=>'invalid']
91+
],
92+
"with filters with other operators"=>[
93+
$this->getHttpRequestWithQueryParameters([
94+
'f'=>base64_encode(json_encode(['name'=>['LIKE'=>'myName']]))
95+
]),
96+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
97+
null,
98+
['filters'=>['name'=>['LIKE'=>'myName']],'sort'=>[],'pagination'=>null,'where'=>null]
99+
],
100+
"with filters for child model"=>[
101+
$this->getHttpRequestWithQueryParameters([
102+
'f'=>base64_encode(json_encode(['name'=>['LIKE'=>'myName']]))
103+
]),
104+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
105+
null,
106+
['filters'=>['name'=>['LIKE'=>'myName']],'sort'=>[],'pagination'=>null,'where'=>null]
107+
],
108+
"with filters for reference and autoIncrement fields"=>[
109+
$this->getHttpRequestWithQueryParameters([
110+
'f'=>base64_encode(json_encode(['id'=>'1','m1Id'=>'2']))
111+
]),
112+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
113+
null,
114+
['filters'=>['id'=>'1','m1Id'=>'2'],'sort'=>[],'pagination'=>null,'where'=>null]
115+
],
116+
117+
"with sort"=>[
118+
$this->getHttpRequestWithQueryParameters([
119+
's'=>base64_encode(json_encode(['name'=>'ASC']))
120+
]),
121+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
122+
null,
123+
['filters'=>[],'sort'=>['name'=>'ASC'],'pagination'=>null,'where'=>null]
124+
],
125+
"with sort with invalid field"=>[
126+
$this->getHttpRequestWithQueryParameters([
127+
's'=>base64_encode(json_encode(['address'=>'ASC']))
128+
]),
129+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
130+
null,
131+
[],
132+
['s'=>'invalid']
133+
],
134+
"with sort without base64 encoded"=>[
135+
$this->getHttpRequestWithQueryParameters([
136+
's'=>json_encode(['name'=>'myName'])
137+
]),
138+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
139+
null,
140+
[],
141+
['s'=>'invalid']
142+
],
143+
"with sort with invalid sort value"=>[
144+
$this->getHttpRequestWithQueryParameters([
145+
's'=>base64_encode(json_encode(['name'=>['and'=>'myName']]))
146+
]),
147+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
148+
null,
149+
[],
150+
['s'=>'invalid']
151+
],
152+
"with sort with invalid sort option"=>[
153+
$this->getHttpRequestWithQueryParameters([
154+
's'=>base64_encode(json_encode(['name'=>'increasing']))
155+
]),
156+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
157+
null,
158+
[],
159+
['s'=>'invalid']
160+
],
161+
"with sort for non-get fields"=>[
162+
$this->getHttpRequestWithQueryParameters([
163+
's'=>base64_encode(json_encode(['password'=>'ASC']))
164+
]),
165+
'PhpPlatform\Tests\SearchQueryParser\Models\M1',
166+
null,
167+
[],
168+
['s'=>'invalid']
169+
],
170+
"with sort for child model"=>[
171+
$this->getHttpRequestWithQueryParameters([
172+
's'=>base64_encode(json_encode(['name'=>'DESC']))
173+
]),
174+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
175+
null,
176+
['filters'=>[],'sort'=>['name'=>'DESC'],'pagination'=>null,'where'=>null]
177+
],
178+
"with sort for reference and autoIncrement fields"=>[
179+
$this->getHttpRequestWithQueryParameters([
180+
's'=>base64_encode(json_encode(['id'=>'ASC','m1Id'=>'DESC']))
181+
]),
182+
'PhpPlatform\Tests\SearchQueryParser\Models\M2',
183+
null,
184+
['filters'=>[],'sort'=>['id'=>'ASC','m1Id'=>'DESC'],'pagination'=>null,'where'=>null]
54185
]
186+
55187
];
188+
//return [$cases['with filters with other operators']];
189+
return $cases;
56190
}
57191

58192
/**

0 commit comments

Comments
 (0)