4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\RequestStack;
16 use Symfony\Component\HttpKernel\EventListener\ProfilerListener;
17 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
18 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
21 use Symfony\Component\HttpKernel\Exception\HttpException;
22 use Symfony\Component\HttpKernel\Kernel;
24 class ProfilerListenerTest extends TestCase
27 * Test to ensure BC without RequestStack.
31 public function testLegacyEventsWithoutRequestStack()
33 $profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')
34 ->disableOriginalConstructor()
37 $profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
38 ->disableOriginalConstructor()
40 $profiler->expects($this->once())
42 ->will($this->returnValue($profile));
44 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
46 $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
47 ->disableOriginalConstructor()
50 $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
51 ->disableOriginalConstructor()
54 $listener = new ProfilerListener($profiler);
55 $listener->onKernelRequest(new GetResponseEvent($kernel, $request, Kernel::MASTER_REQUEST));
56 $listener->onKernelResponse(new FilterResponseEvent($kernel, $request, Kernel::MASTER_REQUEST, $response));
57 $listener->onKernelTerminate(new PostResponseEvent($kernel, $request, $response));
61 * Test a master and sub request with an exception and `onlyException` profiler option enabled.
63 public function testKernelTerminate()
65 $profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')
66 ->disableOriginalConstructor()
69 $profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
70 ->disableOriginalConstructor()
73 $profiler->expects($this->once())
75 ->will($this->returnValue($profile));
77 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
79 $masterRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
80 ->disableOriginalConstructor()
83 $subRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
84 ->disableOriginalConstructor()
87 $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
88 ->disableOriginalConstructor()
91 $requestStack = new RequestStack();
92 $requestStack->push($masterRequest);
94 $onlyException = true;
95 $listener = new ProfilerListener($profiler, $requestStack, null, $onlyException);
98 $listener->onKernelResponse(new FilterResponseEvent($kernel, $masterRequest, Kernel::MASTER_REQUEST, $response));
101 $listener->onKernelException(new GetResponseForExceptionEvent($kernel, $subRequest, Kernel::SUB_REQUEST, new HttpException(404)));
102 $listener->onKernelResponse(new FilterResponseEvent($kernel, $subRequest, Kernel::SUB_REQUEST, $response));
104 $listener->onKernelTerminate(new PostResponseEvent($kernel, $masterRequest, $response));