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;
14 use Symfony\Component\BrowserKit\Client as BaseClient;
15 use Symfony\Component\BrowserKit\Request as DomRequest;
16 use Symfony\Component\BrowserKit\Response as DomResponse;
17 use Symfony\Component\BrowserKit\Cookie as DomCookie;
18 use Symfony\Component\BrowserKit\History;
19 use Symfony\Component\BrowserKit\CookieJar;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
25 * Client simulates a browser and makes requests to a Kernel object.
27 * @author Fabien Potencier <fabien@symfony.com>
29 * @method Request|null getRequest() A Request instance
30 * @method Response|null getResponse() A Response instance
32 class Client extends BaseClient
39 * @param HttpKernelInterface $kernel An HttpKernel instance
40 * @param array $server The server parameters (equivalent of $_SERVER)
41 * @param History $history A History instance to store the browser history
42 * @param CookieJar $cookieJar A CookieJar instance to store the cookies
44 public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
46 // These class properties must be set before calling the parent constructor, as it may depend on it.
47 $this->kernel = $kernel;
48 $this->followRedirects = false;
50 parent::__construct($server, $history, $cookieJar);
56 * @param Request $request A Request instance
58 * @return Response A Response instance
60 protected function doRequest($request)
62 $response = $this->kernel->handle($request);
64 if ($this->kernel instanceof TerminableInterface) {
65 $this->kernel->terminate($request, $response);
72 * Returns the script to execute when the request must be insulated.
74 * @param Request $request A Request instance
78 protected function getScript($request)
80 $kernel = str_replace("'", "\\'", serialize($this->kernel));
81 $request = str_replace("'", "\\'", serialize($request));
83 $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\ClassLoader');
84 $requirePath = str_replace("'", "\\'", $r->getFileName());
85 $symfonyPath = str_replace("'", "\\'", dirname(dirname(dirname(__DIR__))));
86 $errorReporting = error_reporting();
91 error_reporting($errorReporting);
93 require_once '$requirePath';
95 \$loader = new Symfony\Component\ClassLoader\ClassLoader();
96 \$loader->addPrefix('Symfony', '$symfonyPath');
99 \$kernel = unserialize('$kernel');
100 \$request = unserialize('$request');
103 return $code.$this->getHandleScript();
106 protected function getHandleScript()
109 $response = $kernel->handle($request);
111 if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
112 $kernel->terminate($request, $response);
115 echo serialize($response);
120 * Converts the BrowserKit request to a HttpKernel request.
122 * @param DomRequest $request A DomRequest instance
124 * @return Request A Request instance
126 protected function filterRequest(DomRequest $request)
128 $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
130 foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
131 $httpRequest->files->set($key, $value);
138 * Filters an array of files.
140 * This method created test instances of UploadedFile so that the move()
141 * method can be called on those instances.
143 * If the size of a file is greater than the allowed size (from php.ini) then
144 * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
148 * @param array $files An array of files
150 * @return array An array with all uploaded files marked as already moved
152 protected function filterFiles(array $files)
155 foreach ($files as $key => $value) {
156 if (is_array($value)) {
157 $filtered[$key] = $this->filterFiles($value);
158 } elseif ($value instanceof UploadedFile) {
159 if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
160 $filtered[$key] = new UploadedFile(
162 $value->getClientOriginalName(),
163 $value->getClientMimeType(),
169 $filtered[$key] = new UploadedFile(
170 $value->getPathname(),
171 $value->getClientOriginalName(),
172 $value->getClientMimeType(),
173 $value->getClientSize(),
185 * Converts the HttpKernel response to a BrowserKit response.
187 * @param Response $response A Response instance
189 * @return DomResponse A DomResponse instance
191 protected function filterResponse($response)
193 $headers = $response->headers->all();
194 if ($response->headers->getCookies()) {
196 foreach ($response->headers->getCookies() as $cookie) {
197 $cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
199 $headers['Set-Cookie'] = $cookies;
202 // this is needed to support StreamedResponse
204 $response->sendContent();
205 $content = ob_get_clean();
207 return new DomResponse($content, $response->getStatusCode(), $headers);