Skip to content

Commit 772c3f0

Browse files
committed
added tests for pagination and full text search
1 parent 6caf1c9 commit 772c3f0

File tree

7 files changed

+217
-10
lines changed

7 files changed

+217
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php-platform/persist" : "~0.1"
1717
},
1818
"require-dev" : {
19-
"phpunit/phpunit" : "~4.8"
19+
"phpunit/phpunit" : "~4.8",
20+
"php-platform/mock-config" : "~0.1"
2021
},
2122
"autoload" : {
2223
"psr-4" : {

composer.lock

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SearchQueryParser/FindParams.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace PhpPlatform\SearchQueryParser;
44

5+
use PhpPlatform\Persist\Expression;
6+
57
/**
68
* This is a class representing the arguments required by PhpPlatform\Persist\Model::find($filters,$sort,$pagination,$where)
79
*/
810
class FindParams {
911
public $filters = array();
1012
public $sort = array();
11-
public $pagination = array();
13+
public $pagination = null;
14+
15+
/**
16+
* @var Expression
17+
*/
1218
public $where = null;
1319
}

src/SearchQueryParser/Parser.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpPlatform\Persist\Expression;
88
use PhpPlatform\Persist\Model;
99
use PhpPlatform\Persist\Field;
10-
use PhpPlatform\Errors\Exceptions\Application\BadInputException;
1110
use PhpPlatform\Persist\RelationalMappingUtil;
1211

1312
class Parser {
@@ -45,7 +44,7 @@ private static function parseFullTextSearch($request,$modelClassName,$excludeFro
4544
!RelationalMappingUtil::_isAutoIncrement($field) &&
4645
!RelationalMappingUtil::_isReference($field) &&
4746
!in_array($fieldName, $excludeFromFullTextSearch)){
48-
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($className, $field), $fullTextSearchQuery]);
47+
$fullTextSearchExpressions[] = new Expression(Model::OPERATOR_LIKE, [new Field($className, $fieldName), $fullTextSearchQuery]);
4948
}
5049
}
5150
}
@@ -135,7 +134,7 @@ private static function parsePagination($request){
135134
$paginationParam = preg_split('/-/', $paginationParam);
136135
if(count($paginationParam) != 2 ||
137136
(!is_numeric($paginationParam[0]) || !is_int($paginationParam[0]+0)) ||
138-
(!is_numeric($paginationParam[0]) || !is_int($paginationParam[1]+0)) ){
137+
(!is_numeric($paginationParam[1]) || !is_int($paginationParam[1]+0)) ){
139138
throw new BadRequest(['p'=>'invalid']);
140139
}
141140
$pagination = array('pageNumber'=>$paginationParam[0],'pageSize'=>$paginationParam[1]);

tests/SearchQueryParser/MockDataBase.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace PhpPlatform\Tests\SearchQueryParser;
3+
4+
use PhpPlatform\Persist\Connection\Connection;
5+
6+
class MockDataBase implements Connection{
7+
public function query($queryString) {
8+
}
9+
10+
public function autocommit($mode) {
11+
}
12+
13+
public function lastError() {
14+
}
15+
16+
public function lastInsertedId() {
17+
}
18+
19+
public function close() {
20+
}
21+
22+
public function startTransaction() {
23+
}
24+
25+
public function commitTransaction() {
26+
}
27+
28+
public function abortTransaction() {
29+
}
30+
31+
public function encodeForSQLInjection($value) {
32+
return $value;
33+
}
34+
35+
public function formatDate($dateStr = null, $includeTime = null) {
36+
}
37+
38+
public function formatTime($hh = 0, $mm = 0, $ss = 0, $ampm = "AM") {
39+
}
40+
41+
public function formatBoolean($value) {
42+
}
43+
44+
public function outputDateFormat() {
45+
}
46+
47+
public function outputTimeFormat() {
48+
}
49+
50+
public function outputDateTimeFormat() {
51+
}
52+
53+
public function setTimeZone($timeZone) {
54+
}
55+
56+
}

tests/SearchQueryParser/TestParser.php

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use PhpPlatform\SearchQueryParser\FindParams;
77
use PhpPlatform\SearchQueryParser\Parser;
88
use PhpPlatform\Errors\Exceptions\Http\_4XX\BadRequest;
9+
use PhpPlatform\Persist\RelationalMappingUtil;
10+
use PhpPlatform\Persist\TransactionManager;
911

1012
class 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
}

tests/autoload.php

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

3+
use PhpPlatform\Mock\Config\MockSettings;
4+
35
include_once dirname(__FILE__).'/../vendor/autoload.php';
46

57
// getallheaders method will not be present when running php from shell
@@ -13,6 +15,10 @@ function getallheaders(){
1315
// following $_SERVER paramaeters are prefilled to avoid notices in CI Environment
1416
$_SERVER['REMOTE_ADDR'] = 'localhost';
1517
$_SERVER['REQUEST_URI'] = '/tests';
16-
$_SERVER['PLATFORM_APPLICATION_PATH '] = '/tests';
18+
$_SERVER['PLATFORM_APPLICATION_PATH'] = '/tests';
19+
$_SERVER['PLATFORM_SERVICE_CONSUMES'] = 'application/json';
1720
$_SERVER['HTTP_HOST'] = 'localhost';
18-
$_SERVER['REQUEST_METHOD'] = 'GET';
21+
$_SERVER['REQUEST_METHOD'] = 'GET';
22+
23+
// mock database
24+
MockSettings::setSettings('php-platform/persist', 'connection-class', 'PhpPlatform\Tests\SearchQueryParser\MockDataBase');

0 commit comments

Comments
 (0)