Version 1
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / ProfilerListenerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
13
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;
23
24 class ProfilerListenerTest extends TestCase
25 {
26     /**
27      * Test to ensure BC without RequestStack.
28      *
29      * @group legacy
30      */
31     public function testLegacyEventsWithoutRequestStack()
32     {
33         $profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')
34             ->disableOriginalConstructor()
35             ->getMock();
36
37         $profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
38             ->disableOriginalConstructor()
39             ->getMock();
40         $profiler->expects($this->once())
41             ->method('collect')
42             ->will($this->returnValue($profile));
43
44         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
45
46         $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
47             ->disableOriginalConstructor()
48             ->getMock();
49
50         $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
51             ->disableOriginalConstructor()
52             ->getMock();
53
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));
58     }
59
60     /**
61      * Test a master and sub request with an exception and `onlyException` profiler option enabled.
62      */
63     public function testKernelTerminate()
64     {
65         $profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')
66             ->disableOriginalConstructor()
67             ->getMock();
68
69         $profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
70             ->disableOriginalConstructor()
71             ->getMock();
72
73         $profiler->expects($this->once())
74             ->method('collect')
75             ->will($this->returnValue($profile));
76
77         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
78
79         $masterRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
80             ->disableOriginalConstructor()
81             ->getMock();
82
83         $subRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
84             ->disableOriginalConstructor()
85             ->getMock();
86
87         $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
88             ->disableOriginalConstructor()
89             ->getMock();
90
91         $requestStack = new RequestStack();
92         $requestStack->push($masterRequest);
93
94         $onlyException = true;
95         $listener = new ProfilerListener($profiler, $requestStack, null, $onlyException);
96
97         // master request
98         $listener->onKernelResponse(new FilterResponseEvent($kernel, $masterRequest, Kernel::MASTER_REQUEST, $response));
99
100         // sub request
101         $listener->onKernelException(new GetResponseForExceptionEvent($kernel, $subRequest, Kernel::SUB_REQUEST, new HttpException(404)));
102         $listener->onKernelResponse(new FilterResponseEvent($kernel, $subRequest, Kernel::SUB_REQUEST, $response));
103
104         $listener->onKernelTerminate(new PostResponseEvent($kernel, $masterRequest, $response));
105     }
106 }