1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jns\Bundle\XhprofBundle\Tests; |
4
|
|
|
|
5
|
|
|
use Jns\Bundle\XhprofBundle\EventListener\RequestListener; |
6
|
|
|
use Jns\Bundle\XhprofBundle\DataCollector\XhprofCollector; |
7
|
|
|
use Prophecy\Argument; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
14
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
15
|
|
|
|
16
|
|
|
class RequestListenerTest extends ProphecyTestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var XhprofCollector $collector |
20
|
|
|
*/ |
21
|
|
|
private $collector; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ContainerInterface $container |
25
|
|
|
*/ |
26
|
|
|
private $container; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var GetResponseEvent $getResponseEvent |
30
|
|
|
*/ |
31
|
|
|
private $getResponseEvent; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Request $request |
35
|
|
|
*/ |
36
|
|
|
private $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ParameterBag $query |
40
|
|
|
*/ |
41
|
|
|
private $query; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var FilterResponseEvent $filterResponseEvent |
45
|
|
|
*/ |
46
|
|
|
private $filterResponseEvent; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Response $response |
50
|
|
|
*/ |
51
|
|
|
private $response; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ParameterBag $headers |
55
|
|
|
*/ |
56
|
|
|
private $headers; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var RequestListener $requestListener |
60
|
|
|
*/ |
61
|
|
|
private $requestListener; |
62
|
|
|
|
63
|
|
|
public function setUp() |
64
|
|
|
{ |
65
|
|
|
parent::setUp(); |
66
|
|
|
|
67
|
|
|
// Init tested class |
68
|
|
|
$this->collector = $this->getProphecy('Jns\Bundle\XhprofBundle\DataCollector\XhprofCollector'); |
|
|
|
|
69
|
|
|
$this->container = $this->getProphecy('Symfony\Component\DependencyInjection\ContainerInterface'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->requestListener = new RequestListener($this->collector->reveal(), $this->container->reveal()); |
72
|
|
|
|
73
|
|
|
// Init some common objects |
74
|
|
|
$this->getResponseEvent = $this->getProphecy('Symfony\Component\HttpKernel\Event\GetResponseEvent'); |
|
|
|
|
75
|
|
|
$this->request = $this->getProphecy('Symfony\Component\HttpFoundation\Request'); |
|
|
|
|
76
|
|
|
$this->query = $this->getProphecy('Symfony\Component\HttpFoundation\ParameterBag'); |
|
|
|
|
77
|
|
|
$this->filterResponseEvent = $this->getProphecy('Symfony\Component\HttpKernel\Event\FilterResponseEvent'); |
|
|
|
|
78
|
|
|
$this->response = $this->getProphecy('Symfony\Component\HttpFoundation\Response'); |
|
|
|
|
79
|
|
|
$this->headers = $this->getProphecy('Symfony\Component\HttpFoundation\ParameterBag'); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$this->getResponseEvent->getRequest() |
82
|
|
|
->willReturn($this->request->reveal()); |
83
|
|
|
|
84
|
|
|
$this->filterResponseEvent->getRequest() |
85
|
|
|
->willReturn($this->request->reveal()); |
86
|
|
|
|
87
|
|
|
$this->filterResponseEvent->getResponse() |
88
|
|
|
->willReturn($this->response->reveal()); |
89
|
|
|
|
90
|
|
|
$this->request->query = $this->query->reveal(); |
91
|
|
|
$this->response->headers = $this->headers->reveal(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
############################ |
95
|
|
|
# onCoreRequest test suite # |
96
|
|
|
############################ |
97
|
|
|
|
98
|
|
|
public function test_on_core_request_without_master_request() |
99
|
|
|
{ |
100
|
|
|
$this->getResponseEvent->getRequestType() |
|
|
|
|
101
|
|
|
->willReturn('foo'); |
102
|
|
|
|
103
|
|
|
$this->collector->startProfiling() |
|
|
|
|
104
|
|
|
->shouldNotBeCalled(); |
105
|
|
|
|
106
|
|
|
$this->requestListener->onCoreRequest($this->getResponseEvent->reveal()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function test_on_core_request_with_request_query_argument_configured_but_not_provided() |
110
|
|
|
{ |
111
|
|
|
$this->getResponseEvent->getRequestType() |
|
|
|
|
112
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
113
|
|
|
|
114
|
|
|
$this->container->getParameter('jns_xhprof.request_query_argument') |
115
|
|
|
->willReturn('__xhprof'); |
116
|
|
|
|
117
|
|
|
$this->query->get('__xhprof') |
118
|
|
|
->willReturn(null); |
119
|
|
|
|
120
|
|
|
$this->collector->startProfiling() |
|
|
|
|
121
|
|
|
->shouldNotBeCalled(); |
122
|
|
|
|
123
|
|
|
$this->requestListener->onCoreRequest($this->getResponseEvent->reveal()); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function provider_on_core_request_with_hard_ignored_uri() |
127
|
|
|
{ |
128
|
|
|
return array( |
129
|
|
|
array('http://foo.com/_wdt'), |
130
|
|
|
array('http://foo.com/_wdt/bar'), |
131
|
|
|
array('http://foo.com/_profiler'), |
132
|
|
|
array('http://foo.com/_profiler/bar'), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @dataProvider provider_on_core_request_with_hard_ignored_uri |
138
|
|
|
*/ |
139
|
|
|
public function test_on_core_request_with_hard_ignored_uri($uri) |
140
|
|
|
{ |
141
|
|
|
$this->getResponseEvent->getRequestType() |
|
|
|
|
142
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
143
|
|
|
|
144
|
|
|
$this->container->getParameter('jns_xhprof.request_query_argument') |
145
|
|
|
->willReturn(false); |
146
|
|
|
|
147
|
|
|
$this->request->getRequestUri() |
|
|
|
|
148
|
|
|
->willReturn($uri); |
149
|
|
|
|
150
|
|
|
$this->collector->startProfiling() |
|
|
|
|
151
|
|
|
->shouldNotBeCalled(); |
152
|
|
|
|
153
|
|
|
$this->requestListener->onCoreRequest($this->getResponseEvent->reveal()); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function test_on_core_request_with_ignored_uri_pattern() |
157
|
|
|
{ |
158
|
|
|
$this->getResponseEvent->getRequestType() |
|
|
|
|
159
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
160
|
|
|
|
161
|
|
|
$this->container->getParameter('jns_xhprof.request_query_argument') |
162
|
|
|
->willReturn(false); |
163
|
|
|
|
164
|
|
|
$this->request->getRequestUri() |
|
|
|
|
165
|
|
|
->willReturn('http://foo.com/amazing/ignored/uri'); |
166
|
|
|
|
167
|
|
|
$this->container->getParameter('jns_xhprof.exclude_patterns') |
168
|
|
|
->willReturn(array( |
169
|
|
|
'/ignored/', |
170
|
|
|
)); |
171
|
|
|
|
172
|
|
|
$this->collector->startProfiling() |
|
|
|
|
173
|
|
|
->shouldNotBeCalled(); |
174
|
|
|
|
175
|
|
|
$this->requestListener->onCoreRequest($this->getResponseEvent->reveal()); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function test_on_core_request_with_request_query_argument_configured_and_provided() |
179
|
|
|
{ |
180
|
|
|
$this->getResponseEvent->getRequestType() |
|
|
|
|
181
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
182
|
|
|
$this->getResponseEvent->getRequest() |
|
|
|
|
183
|
|
|
->willReturn($this->request); |
184
|
|
|
|
185
|
|
|
$this->container->getParameter('jns_xhprof.request_query_argument') |
186
|
|
|
->willReturn('__xhprof'); |
187
|
|
|
|
188
|
|
|
$this->query->get('__xhprof') |
189
|
|
|
->willReturn(true); |
190
|
|
|
|
191
|
|
|
$this->query->remove('__xhprof') |
|
|
|
|
192
|
|
|
->shouldBeCalled(); |
193
|
|
|
|
194
|
|
|
$this->request->getRequestUri() |
|
|
|
|
195
|
|
|
->willReturn('http://foo.com/amazing/uri'); |
196
|
|
|
|
197
|
|
|
$this->container->getParameter('jns_xhprof.exclude_patterns') |
198
|
|
|
->willReturn(array( |
199
|
|
|
'/ignored/', |
200
|
|
|
)); |
201
|
|
|
|
202
|
|
|
$this->collector->startProfiling($this->request->reveal()) |
|
|
|
|
203
|
|
|
->shouldBeCalled(); |
204
|
|
|
|
205
|
|
|
$this->requestListener->onCoreRequest($this->getResponseEvent->reveal()); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
############################# |
209
|
|
|
# onCoreResponse test suite # |
210
|
|
|
############################# |
211
|
|
|
|
212
|
|
|
public function test_on_core_response_without_master_request() |
213
|
|
|
{ |
214
|
|
|
$this->filterResponseEvent->getRequestType() |
|
|
|
|
215
|
|
|
->willReturn('foo'); |
216
|
|
|
|
217
|
|
|
$this->collector->stopProfiling(Argument::any()) |
|
|
|
|
218
|
|
|
->shouldNotBeCalled(); |
219
|
|
|
|
220
|
|
|
$this->requestListener->onCoreResponse($this->filterResponseEvent->reveal()); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function test_on_core_response_without_link_returned() |
224
|
|
|
{ |
225
|
|
|
$this->filterResponseEvent->getRequestType() |
|
|
|
|
226
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
227
|
|
|
|
228
|
|
|
$this->request->getHost() |
|
|
|
|
229
|
|
|
->willReturn('foo.com'); |
230
|
|
|
|
231
|
|
|
$this->request->getUri() |
|
|
|
|
232
|
|
|
->willReturn('/amazing/uri'); |
233
|
|
|
|
234
|
|
|
$this->collector->stopProfiling('foo.com', '/amazing/uri') |
|
|
|
|
235
|
|
|
->willReturn(false); |
236
|
|
|
|
237
|
|
|
$this->requestListener->onCoreResponse($this->filterResponseEvent->reveal()); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function test_on_core_response_without_headerName() |
241
|
|
|
{ |
242
|
|
|
$this->filterResponseEvent->getRequestType() |
|
|
|
|
243
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
244
|
|
|
|
245
|
|
|
$this->request->getHost() |
|
|
|
|
246
|
|
|
->willReturn('foo.com'); |
247
|
|
|
|
248
|
|
|
$this->request->getUri() |
|
|
|
|
249
|
|
|
->willReturn('/amazing/uri'); |
250
|
|
|
|
251
|
|
|
$this->collector->stopProfiling('foo.com', '/amazing/uri') |
|
|
|
|
252
|
|
|
->willReturn('link'); |
253
|
|
|
|
254
|
|
|
$this->container->getParameter('jns_xhprof.response_header') |
255
|
|
|
->willReturn(null); |
256
|
|
|
|
257
|
|
|
$this->requestListener->onCoreResponse($this->filterResponseEvent->reveal()); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function test_on_core_response() |
261
|
|
|
{ |
262
|
|
|
$this->filterResponseEvent->getRequestType() |
|
|
|
|
263
|
|
|
->willReturn(HttpKernelInterface::MASTER_REQUEST); |
264
|
|
|
|
265
|
|
|
$this->request->getHost() |
|
|
|
|
266
|
|
|
->willReturn('foo.com'); |
267
|
|
|
|
268
|
|
|
$this->request->getUri() |
|
|
|
|
269
|
|
|
->willReturn('/amazing/uri'); |
270
|
|
|
|
271
|
|
|
$this->collector->stopProfiling('foo.com', '/amazing/uri') |
|
|
|
|
272
|
|
|
->willReturn('link'); |
273
|
|
|
|
274
|
|
|
$this->container->getParameter('jns_xhprof.response_header') |
275
|
|
|
->willReturn('header-name'); |
276
|
|
|
|
277
|
|
|
$this->collector->getXhprofUrl() |
|
|
|
|
278
|
|
|
->willReturn('http://xhprof.com/url'); |
279
|
|
|
|
280
|
|
|
$this->headers->set('header-name', 'http://xhprof.com/url') |
|
|
|
|
281
|
|
|
->shouldBeCalled(); |
282
|
|
|
|
283
|
|
|
$this->requestListener->onCoreResponse($this->filterResponseEvent->reveal()); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..