|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tests\Artprima\QueryFilterBundle\Request; |
|
6
|
|
|
|
|
7
|
|
|
use Artprima\QueryFilterBundle\QueryFilter\Config\BaseConfig; |
|
8
|
|
|
use Artprima\QueryFilterBundle\QueryFilter\QueryFilter; |
|
9
|
|
|
use Artprima\QueryFilterBundle\QueryFilter\QueryFilterArgs; |
|
10
|
|
|
use Artprima\QueryFilterBundle\QueryFilter\QueryResult; |
|
11
|
|
|
use Artprima\QueryFilterBundle\Request\Request; |
|
12
|
|
|
use Artprima\QueryFilterBundle\Response\Response; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Tests\Artprima\QueryFilterBundle\Fixtures\Response\ResponseConstructorWithRequiredArguments; |
|
15
|
|
|
use Tests\Artprima\QueryFilterBundle\Fixtures\Response\ResponseNotImplementingResponseInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
|
17
|
|
|
|
|
18
|
|
|
class QueryFilterTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
* @doesNotPerformAssertions |
|
23
|
|
|
*/ |
|
24
|
|
|
public function constructor_should_throw_no_exceptions_with_proper_argument() |
|
25
|
|
|
{ |
|
26
|
|
|
new QueryFilter(Response::class); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
* @expectedException \Artprima\QueryFilterBundle\Exception\InvalidArgumentException |
|
32
|
|
|
* @expectedExceptionMessage Response class "Tests\Artprima\QueryFilterBundle\Fixtures\Response\ResponseNotImplementingResponseInterface" must implement "Artprima\QueryFilterBundle\Response\ResponseInterface" |
|
33
|
|
|
*/ |
|
34
|
|
|
public function constructor_should_throw_exception_for_response_not_implementing_ResponseInterface() |
|
35
|
|
|
{ |
|
36
|
|
|
new QueryFilter(ResponseNotImplementingResponseInterface::class); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
* @expectedException \Artprima\QueryFilterBundle\Exception\InvalidArgumentException |
|
42
|
|
|
* @expectedExceptionMessage Response class "Tests\Artprima\QueryFilterBundle\Fixtures\Response\ResponseConstructorWithRequiredArguments" must have a constructor without required parameters |
|
43
|
|
|
*/ |
|
44
|
|
|
public function constructor_should_throw_exception_for_response_constructor_having_required_arguments() |
|
45
|
|
|
{ |
|
46
|
|
|
new QueryFilter(ResponseConstructorWithRequiredArguments::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testGetDataSimpleBaseCase() |
|
50
|
|
|
{ |
|
51
|
|
|
$queryFilter = new QueryFilter(Response::class); |
|
52
|
|
|
|
|
53
|
|
|
$config = new BaseConfig(); |
|
54
|
|
|
$request = new Request(new HttpRequest([ |
|
55
|
|
|
'limit' => 100, |
|
56
|
|
|
'page'=> 3, |
|
57
|
|
|
'filter' => [ |
|
58
|
|
|
'c.dummy' => 'the road to hell', |
|
59
|
|
|
], |
|
60
|
|
|
'sortby' => 'c.id', |
|
61
|
|
|
'sortdir' => 'asc', |
|
62
|
|
|
])); |
|
63
|
|
|
$config->setRequest($request); |
|
64
|
|
|
$config->setSearchAllowedCols(['c.dummy']); |
|
65
|
|
|
$config->setSortCols(['c.id']); |
|
66
|
|
|
|
|
67
|
|
|
$config->setRepositoryCallback(function(QueryFilterArgs $args) { |
|
68
|
|
|
self::assertSame(100, $args->getLimit()); |
|
69
|
|
|
self::assertSame(200, $args->getOffset()); |
|
70
|
|
|
self::assertEquals([ |
|
71
|
|
|
'c.dummy' => [ |
|
72
|
|
|
'type' => 'like', |
|
73
|
|
|
'val' => 'the%road%to%hell' |
|
74
|
|
|
], |
|
75
|
|
|
], $args->getSearchBy()); |
|
76
|
|
|
self::assertEquals([ |
|
77
|
|
|
'c.id' => 'asc', |
|
78
|
|
|
], $args->getSortBy()); |
|
79
|
|
|
|
|
80
|
|
|
return new QueryResult([ |
|
81
|
|
|
["dummy"], |
|
82
|
|
|
["wammy"], |
|
83
|
|
|
], 1000); |
|
84
|
|
|
}); |
|
85
|
|
|
$response = $queryFilter->getData($config); |
|
86
|
|
|
self::assertEquals([ |
|
87
|
|
|
["dummy"], |
|
88
|
|
|
["wammy"], |
|
89
|
|
|
], $response->getData()); |
|
90
|
|
|
self::assertSame(1000, $response->getMeta()['total_records']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testGetDataAdvancedBaseCase() |
|
94
|
|
|
{ |
|
95
|
|
|
$queryFilter = new QueryFilter(Response::class); |
|
96
|
|
|
|
|
97
|
|
|
$config = new BaseConfig(); |
|
98
|
|
|
$request = new Request(new HttpRequest([ |
|
99
|
|
|
'limit' => 100, |
|
100
|
|
|
'page'=> 3, |
|
101
|
|
|
'filter' => [ |
|
102
|
|
|
[ |
|
103
|
|
|
'field' => 'c.hell', |
|
104
|
|
|
'type' => 'eq', |
|
105
|
|
|
'val' => 'the road to hell', |
|
106
|
|
|
], |
|
107
|
|
|
[ |
|
108
|
|
|
'field' => 'c.heaven', |
|
109
|
|
|
'type' => 'like', |
|
110
|
|
|
'val' => 'the road to heaven', |
|
111
|
|
|
], |
|
112
|
|
|
], |
|
113
|
|
|
'sortby' => 'c.id', |
|
114
|
|
|
'sortdir' => 'asc', |
|
115
|
|
|
'simple' => '0', |
|
116
|
|
|
])); |
|
117
|
|
|
$config->setRequest($request); |
|
118
|
|
|
$config->setSearchAllowedCols(['c.hell', 'c.heaven']); |
|
119
|
|
|
$config->setSortCols(['c.id']); |
|
120
|
|
|
|
|
121
|
|
|
$config->setRepositoryCallback(function(QueryFilterArgs $args) { |
|
122
|
|
|
self::assertSame(100, $args->getLimit()); |
|
123
|
|
|
self::assertSame(200, $args->getOffset()); |
|
124
|
|
|
self::assertEquals([ |
|
125
|
|
|
'c.hell' => [ |
|
126
|
|
|
'type' => 'eq', |
|
127
|
|
|
'val' => 'the road to hell' |
|
128
|
|
|
], |
|
129
|
|
|
'c.heaven' => [ |
|
130
|
|
|
'type' => 'like', |
|
131
|
|
|
'val' => 'the%road%to%heaven' |
|
132
|
|
|
], |
|
133
|
|
|
], $args->getSearchBy()); |
|
134
|
|
|
self::assertEquals([ |
|
135
|
|
|
'c.id' => 'asc', |
|
136
|
|
|
], $args->getSortBy()); |
|
137
|
|
|
|
|
138
|
|
|
return new QueryResult([ |
|
139
|
|
|
["dummy"], |
|
140
|
|
|
["wammy"], |
|
141
|
|
|
], 1000); |
|
142
|
|
|
}); |
|
143
|
|
|
$response = $queryFilter->getData($config); |
|
144
|
|
|
self::assertEquals([ |
|
145
|
|
|
["dummy"], |
|
146
|
|
|
["wammy"], |
|
147
|
|
|
], $response->getData()); |
|
148
|
|
|
self::assertSame(1000, $response->getMeta()['total_records']); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|