1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Bundle\VMProApiBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use MovingImage\Bundle\VMProApiBundle\EventListener\StopwatchListener; |
6
|
|
|
use MovingImage\Bundle\VMProApiBundle\Service\Stopwatch; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
12
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
13
|
|
|
use Symfony\Component\Stopwatch\Stopwatch as SymfonyStopwatch; |
14
|
|
|
|
15
|
|
|
class StopwatchListenerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param bool $enabled |
19
|
|
|
* @dataProvider dataProviderForTestOnKernelResponse |
20
|
|
|
*/ |
21
|
|
|
public function testOnKernelResponse($enabled) |
22
|
|
|
{ |
23
|
|
|
$stopwatch = new Stopwatch(new SymfonyStopwatch()); |
24
|
|
|
$stopwatch->start('test'); |
25
|
|
|
$stopwatch->stop('test'); |
26
|
|
|
$listener = new StopwatchListener($stopwatch, $enabled); |
27
|
|
|
$request = $this->createMock(Request::class); |
28
|
|
|
$response = Response::create(); |
29
|
|
|
$kernel = $this->createMock(HttpKernel::class); |
30
|
|
|
$event = new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response); |
31
|
|
|
$listener->onKernelResponse($event); |
32
|
|
|
|
33
|
|
|
if ($enabled) { |
34
|
|
|
$this->assertTrue($response->headers->has('X-API-RESPONSE-TEST')); |
35
|
|
|
} else { |
36
|
|
|
$this->assertFalse($response->headers->has('X-API-RESPONSE-TEST')); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Data provider for testOnKernelResponse. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function dataProviderForTestOnKernelResponse() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
[true], |
49
|
|
|
[false], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|