-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProfilerSubscriberTest.php
61 lines (50 loc) · 2.28 KB
/
ProfilerSubscriberTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace App\Tests;
use App\Kernel;
use App\ProfilerSubscriber;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
#[CoversClass(ProfilerSubscriber::class)]
final class ProfilerSubscriberTest extends TestCase
{
#[DataProvider('provideInvalidRequests')]
public function testAjaxReplaceHeaderNotSet(int $requestType, bool $debug, bool $isXmlHttpRequest): void
{
$request = new Request();
if ($isXmlHttpRequest) {
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
}
$response = new Response();
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), $requestType, $response);
$listener = new ProfilerSubscriber($debug);
$listener->onKernelResponse($event);
$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
}
/**
* @return iterable<array{int, bool, bool}>
*/
public static function provideInvalidRequests(): iterable
{
yield 'sub request, not debug, not XHR' => [HttpKernelInterface::SUB_REQUEST, false, false];
yield 'sub request, not debug, XHR' => [HttpKernelInterface::SUB_REQUEST, false, true];
yield 'sub request, debug, XHR' => [HttpKernelInterface::SUB_REQUEST, true, true];
yield 'main request, not debug, not XHR' => [HttpKernelInterface::MAIN_REQUEST, false, false];
yield 'main request, debug, not XHR' => [HttpKernelInterface::MAIN_REQUEST, true, false];
}
public function testAjaxReplaceHeaderOnEnabledAndXHR(): void
{
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$response = new Response();
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);
$listener = new ProfilerSubscriber(true);
$listener->onKernelResponse($event);
$this->assertEquals('1', $response->headers->get('Symfony-Debug-Toolbar-Replace'));
}
}