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
12namespace Symfony\Component\HttpKernel\Tests\HttpCache;
13
14use Symfony\Component\HttpKernel\HttpKernel;
15use Symfony\Component\HttpKernel\HttpKernelInterface;
16use Symfony\Component\HttpFoundation\Request;
17use Symfony\Component\HttpFoundation\Response;
18use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19use Symfony\Component\EventDispatcher\EventDispatcher;
20
21class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInterface
22{
23    protected $bodies = array();
24    protected $statuses = array();
25    protected $headers = array();
26    protected $called = false;
27    protected $backendRequest;
28
29    public function __construct($responses)
30    {
31        foreach ($responses as $response) {
32            $this->bodies[] = $response['body'];
33            $this->statuses[] = $response['status'];
34            $this->headers[] = $response['headers'];
35        }
36
37        parent::__construct(new EventDispatcher(), $this);
38    }
39
40    public function getBackendRequest()
41    {
42        return $this->backendRequest;
43    }
44
45    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
46    {
47        $this->backendRequest = $request;
48
49        return parent::handle($request, $type, $catch);
50    }
51
52    public function getController(Request $request)
53    {
54        return array($this, 'callController');
55    }
56
57    public function getArguments(Request $request, $controller)
58    {
59        return array($request);
60    }
61
62    public function callController(Request $request)
63    {
64        $this->called = true;
65
66        $response = new Response(array_shift($this->bodies), array_shift($this->statuses), array_shift($this->headers));
67
68        return $response;
69    }
70
71    public function hasBeenCalled()
72    {
73        return $this->called;
74    }
75
76    public function reset()
77    {
78        $this->called = false;
79    }
80}
81