1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* validate listener |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\RqlParserBundle\Tests\Listener; |
7
|
|
|
|
8
|
|
|
use Graviton\RqlParserBundle\Listener\RequestListener; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author List of contributors <https://github.com/libgraviton/GravitonRqlParserBundle/graphs/contributors> |
13
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
14
|
|
|
* @link http://swisscom.ch |
15
|
|
|
*/ |
16
|
|
|
class RequestListenerTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* check if nothing gets done when query is empty |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function testWillBehaveOnEmptyQuery() |
24
|
|
|
{ |
25
|
|
|
$lexerDouble = $this->getMockBuilder('Graviton\RqlParser\Lexer') |
26
|
|
|
->disableOriginalConstructor() |
27
|
|
|
->getMock(); |
28
|
|
|
$parserDouble = $this->getMockBuilder('Graviton\RqlParser\Parser') |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->getMock(); |
31
|
|
|
|
32
|
|
|
$eventDouble = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent') |
33
|
|
|
->disableOriginalConstructor() |
34
|
|
|
->getMock(); |
35
|
|
|
|
36
|
|
|
$requestDouble = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock(); |
37
|
|
|
|
38
|
|
|
$serverDouble = $this->getMockBuilder('Symfony\Component\HttpFoundation\ServerBag')->getMock(); |
39
|
|
|
$serverDouble->expects($this->once()) |
40
|
|
|
->method('get') |
41
|
|
|
->with('QUERY_STRING') |
42
|
|
|
->willReturn(null); |
43
|
|
|
$requestDouble->server = $serverDouble; |
44
|
|
|
|
45
|
|
|
$attributesDouble = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock(); |
46
|
|
|
$attributesDouble->expects($this->never()) |
47
|
|
|
->method('set'); |
48
|
|
|
$requestDouble->attributes = $attributesDouble; |
49
|
|
|
|
50
|
|
|
$eventDouble->expects($this->once()) |
51
|
|
|
->method('getRequest') |
52
|
|
|
->willReturn($requestDouble); |
53
|
|
|
|
54
|
|
|
$sut = new RequestListener( |
55
|
|
|
$lexerDouble, |
56
|
|
|
$parserDouble |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$sut->onKernelRequest($eventDouble); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* test if queries get handled properly |
64
|
|
|
* |
65
|
|
|
* @dataProvider willParseQueryData |
66
|
|
|
* |
67
|
|
|
* @param string $query that we should be extracting |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function testWillParseQuery($query) |
72
|
|
|
{ |
73
|
|
|
$lexerDouble = $this->getMockBuilder('Graviton\RqlParser\Lexer') |
74
|
|
|
->disableOriginalConstructor() |
75
|
|
|
->getMock(); |
76
|
|
|
$lexerDouble->expects($this->any()) |
77
|
|
|
->method('tokenize') |
78
|
|
|
->willReturn( |
79
|
|
|
$this->getMockBuilder('Graviton\RqlParser\TokenStream') |
80
|
|
|
->disableOriginalConstructor() |
81
|
|
|
->getMock() |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$parserDouble = $this->getMockBuilder('Graviton\RqlParser\Parser') |
85
|
|
|
->disableOriginalConstructor() |
86
|
|
|
->getMock(); |
87
|
|
|
$parserDouble->expects($this->once()) |
88
|
|
|
->method('parse') |
89
|
|
|
->willReturn($this->getMockBuilder('Graviton\RqlParser\Query')->getMock()); |
90
|
|
|
|
91
|
|
|
$eventDouble = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent') |
92
|
|
|
->disableOriginalConstructor() |
93
|
|
|
->getMock(); |
94
|
|
|
|
95
|
|
|
$requestDouble = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock(); |
96
|
|
|
|
97
|
|
|
$serverDouble = $this->getMockBuilder('Symfony\Component\HttpFoundation\ServerBag')->getMock(); |
98
|
|
|
$serverDouble->expects($this->once()) |
99
|
|
|
->method('get') |
100
|
|
|
->with('QUERY_STRING') |
101
|
|
|
->willReturn($query); |
102
|
|
|
$requestDouble->server = $serverDouble; |
103
|
|
|
|
104
|
|
|
$attributesDouble = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock(); |
105
|
|
|
$attributesDouble->expects($this->at(0)) |
106
|
|
|
->method('set') |
107
|
|
|
->with('hasRql', true); |
108
|
|
|
$attributesDouble->expects($this->at(1)) |
109
|
|
|
->method('set') |
110
|
|
|
->with('rawRql', $query); |
111
|
|
|
$attributesDouble->expects($this->at(2)) |
112
|
|
|
->method('set') |
113
|
|
|
->with('rqlQuery', $this->isInstanceOf('Graviton\RqlParser\Query')); |
114
|
|
|
$requestDouble->attributes = $attributesDouble; |
115
|
|
|
|
116
|
|
|
$eventDouble->expects($this->once()) |
117
|
|
|
->method('getRequest') |
118
|
|
|
->willReturn($requestDouble); |
119
|
|
|
|
120
|
|
|
$sut = new RequestListener( |
121
|
|
|
$lexerDouble, |
122
|
|
|
$parserDouble |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$sut->onKernelRequest($eventDouble); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array[] |
130
|
|
|
*/ |
131
|
|
|
public function willParseQueryData() |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
'simple query string' => [ |
135
|
|
|
'eq(foo,b%20a%20r)' |
136
|
|
|
], |
137
|
|
|
'test with $ref in name' => [ |
138
|
|
|
'eq(name.%24ref,http%3A%2F%2Fexmaple.com)' |
139
|
|
|
], |
140
|
|
|
'multiple rql statements' => [ |
141
|
|
|
'(a=2&(b<3|c>4)&like(e,123))&select(a,b)&sort(+a,-b)&limit(1,2)' |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|