5dbf02c9922dae6c4ba2b811f4e295e8374c1eb8
[yaffs-website] / vendor / symfony / http-kernel / Tests / HttpCache / TestHttpKernel.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\HttpCache;
13
14 use Symfony\Component\EventDispatcher\EventDispatcher;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
18 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19 use Symfony\Component\HttpKernel\HttpKernel;
20 use Symfony\Component\HttpKernel\HttpKernelInterface;
21
22 class TestHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
23 {
24     protected $body;
25     protected $status;
26     protected $headers;
27     protected $called = false;
28     protected $customizer;
29     protected $catch = false;
30     protected $backendRequest;
31
32     public function __construct($body, $status, $headers, \Closure $customizer = null)
33     {
34         $this->body = $body;
35         $this->status = $status;
36         $this->headers = $headers;
37         $this->customizer = $customizer;
38
39         parent::__construct(new EventDispatcher(), $this, null, $this);
40     }
41
42     public function assert(\Closure $callback)
43     {
44         $trustedConfig = array(Request::getTrustedProxies(), Request::getTrustedHeaderSet());
45
46         list($trustedProxies, $trustedHeaderSet, $backendRequest) = $this->backendRequest;
47         Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
48
49         try {
50             $callback($backendRequest);
51         } finally {
52             list($trustedProxies, $trustedHeaderSet) = $trustedConfig;
53             Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
54         }
55     }
56
57     public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
58     {
59         $this->catch = $catch;
60         $this->backendRequest = array(Request::getTrustedProxies(), Request::getTrustedHeaderSet(), $request);
61
62         return parent::handle($request, $type, $catch);
63     }
64
65     public function isCatchingExceptions()
66     {
67         return $this->catch;
68     }
69
70     public function getController(Request $request)
71     {
72         return array($this, 'callController');
73     }
74
75     public function getArguments(Request $request, $controller)
76     {
77         return array($request);
78     }
79
80     public function callController(Request $request)
81     {
82         $this->called = true;
83
84         $response = new Response($this->body, $this->status, $this->headers);
85
86         if (null !== $customizer = $this->customizer) {
87             $customizer($request, $response);
88         }
89
90         return $response;
91     }
92
93     public function hasBeenCalled()
94     {
95         return $this->called;
96     }
97
98     public function reset()
99     {
100         $this->called = false;
101     }
102 }