Skip to content

Commit 58d350b

Browse files
committed
added tests for parser
1 parent 039b4b7 commit 58d350b

File tree

6 files changed

+138
-6
lines changed

6 files changed

+138
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ php:
55
- '5.6'
66
- '7.0'
77
- '7.1'
8+
- '7.2'
89
- 'nightly'
910

1011
before_script:

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

3-
<phpunit colors="true" bootstrap="vendor/autoload.php" >
3+
<phpunit colors="true" bootstrap="tests/autoload.php" >
44
<testsuites>
55
<testsuite name="php-platform/search-query-params">
66
<directory suffix=".php">./tests</directory>

src/SearchQueryParser/Parser.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace ICircle\Accounts\Services\Utils;
3+
namespace PhpPlatform\SearchQueryParser;
44

55
use PhpPlatform\RESTFul\HTTPRequest;
66
use PhpPlatform\Errors\Exceptions\Http\_4XX\BadRequest;
@@ -9,7 +9,6 @@
99
use PhpPlatform\Persist\Field;
1010
use PhpPlatform\Errors\Exceptions\Application\BadInputException;
1111
use PhpPlatform\Persist\RelationalMappingUtil;
12-
use PhpPlatform\SearchQueryParser\FindParams;
1312

1413
class Parser {
1514

@@ -19,8 +18,10 @@ class Parser {
1918
* @param HTTPRequest $request is the HTTPRequest object received in the rest service
2019
* @param string $modelClassName is the full name of the Model which is served as REST Resource
2120
* @param string[] $excludeFromFullTextSearch is the array of fields which needs to be excluded from performing full text search
21+
*
22+
* @return FindParams
2223
*/
23-
static public function parse($request,$modelClassName,$excludeFromFullTextSearch){
24+
static public function parse($request,$modelClassName,$excludeFromFullTextSearch = array()){
2425
$findParams = new FindParams();
2526
try{
2627
// full text search query
@@ -31,10 +32,13 @@ static public function parse($request,$modelClassName,$excludeFromFullTextSearch
3132
}catch (\Exception $e){
3233
throw new BadRequest("Bad Search Params");
3334
}
34-
$findParams;
35+
return $findParams;
3536
}
3637

3738
private static function parseFullTextSearch($request,$modelClassName,$excludeFromFullTextSearch){
39+
if(!is_array($excludeFromFullTextSearch)){
40+
$excludeFromFullTextSearch = array();
41+
}
3842
$whereExpression = null;
3943
$fullTextSearchQuery = $request->getQueryParam('q');
4044
if($fullTextSearchQuery != null){
@@ -66,7 +70,7 @@ private static function parseFilters($request,$modelClassName){
6670

6771
foreach ($classList as $className=>$class){
6872
foreach ($class['fields'] as $fieldName=>$field){
69-
if(RelationalMappingUtil::_isGet($field) && in_array($fieldName, $fieldSpecificFilters)){
73+
if(RelationalMappingUtil::_isGet($field) && array_key_exists($fieldName, $fieldSpecificFilters)){
7074
$filterValue = $fieldSpecificFilters[$fieldName];
7175
if(is_scalar($filterValue)){
7276
$filters[$fieldName]=$filterValue;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace PhpPlatform\Tests\SearchQueryParser\Models;
3+
4+
/**
5+
* @tableName M1
6+
* @prefix m1
7+
*/
8+
class M1 {
9+
10+
/**
11+
* @columnName ID
12+
* @type bigint
13+
* @primary
14+
* @autoIncrement
15+
* @get
16+
*/
17+
private $id = null;
18+
19+
/**
20+
* @columnName NAME
21+
* @type varchar
22+
* @set
23+
* @get
24+
*/
25+
private $name = null;
26+
27+
/**
28+
* @columnName USER_NAME
29+
* @type varchar
30+
* @set
31+
* @get
32+
*/
33+
private $userName = null;
34+
35+
/**
36+
* @columnName PASSWORD
37+
* @type varchar
38+
* @set
39+
*/
40+
private $password = null;
41+
42+
43+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

tests/autoload.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
include_once dirname(__FILE__).'/../vendor/autoload.php';
4+
5+
// getallheaders method will not be present when running php from shell
6+
// so mocking this function
7+
if(!function_exists('getallheaders')){
8+
function getallheaders(){
9+
return [];
10+
}
11+
}

0 commit comments

Comments
 (0)