2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\HttpKernel;
|
2010-04-21 11:05:34 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\BrowserKit\Client as BaseClient;
|
|
|
|
use Symfony\Component\BrowserKit\Request as DomRequest;
|
|
|
|
use Symfony\Component\BrowserKit\Response as DomResponse;
|
2011-04-13 19:25:35 +01:00
|
|
|
use Symfony\Component\BrowserKit\Cookie as DomCookie;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\BrowserKit\History;
|
|
|
|
use Symfony\Component\BrowserKit\CookieJar;
|
2013-09-19 11:49:28 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-09-18 13:41:53 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2010-04-21 11:05:34 +01:00
|
|
|
|
|
|
|
/**
|
2010-05-06 11:04:50 +01:00
|
|
|
* Client simulates a browser and makes requests to a Kernel object.
|
2010-04-21 11:05:34 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-04-21 11:05:34 +01:00
|
|
|
*/
|
|
|
|
class Client extends BaseClient
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $kernel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param HttpKernelInterface $kernel An HttpKernel instance
|
|
|
|
* @param array $server The server parameters (equivalent of $_SERVER)
|
|
|
|
* @param History $history A History instance to store the browser history
|
|
|
|
* @param CookieJar $cookieJar A CookieJar instance to store the cookies
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-10 16:26:10 +01:00
|
|
|
public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
|
2010-04-21 11:05:34 +01:00
|
|
|
{
|
2013-11-25 12:34:16 +00:00
|
|
|
// These class properties must be set before calling the parent constructor, as it may depend on it.
|
2013-11-10 17:06:47 +00:00
|
|
|
$this->kernel = $kernel;
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->followRedirects = false;
|
2013-11-25 12:34:16 +00:00
|
|
|
|
|
|
|
parent::__construct($server, $history, $cookieJar);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2013-04-24 17:19:42 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2013-04-25 06:52:12 +01:00
|
|
|
* @return Request|null A Request instance
|
2013-04-24 17:19:42 +01:00
|
|
|
*/
|
2013-04-24 20:08:47 +01:00
|
|
|
public function getRequest()
|
2013-04-24 17:19:42 +01:00
|
|
|
{
|
2013-04-24 20:08:47 +01:00
|
|
|
return parent::getRequest();
|
2013-04-24 17:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2013-04-25 06:52:12 +01:00
|
|
|
* @return Response|null A Response instance
|
2013-04-24 17:19:42 +01:00
|
|
|
*/
|
2013-04-24 20:08:47 +01:00
|
|
|
public function getResponse()
|
2013-04-24 17:19:42 +01:00
|
|
|
{
|
2013-04-24 20:08:47 +01:00
|
|
|
return parent::getResponse();
|
2013-04-24 17:19:42 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Makes a request.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param Request $request A Request instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
protected function doRequest($request)
|
|
|
|
{
|
2011-12-15 18:17:27 +00:00
|
|
|
$response = $this->kernel->handle($request);
|
|
|
|
|
|
|
|
if ($this->kernel instanceof TerminableInterface) {
|
|
|
|
$this->kernel->terminate($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the script to execute when the request must be insulated.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param Request $request A Request instance
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
protected function getScript($request)
|
|
|
|
{
|
2011-07-22 12:37:23 +01:00
|
|
|
$kernel = str_replace("'", "\\'", serialize($this->kernel));
|
|
|
|
$request = str_replace("'", "\\'", serialize($request));
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2013-02-08 13:02:12 +00:00
|
|
|
$r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\ClassLoader');
|
2011-07-22 12:37:23 +01:00
|
|
|
$requirePath = str_replace("'", "\\'", $r->getFileName());
|
2015-07-20 22:51:01 +01:00
|
|
|
$symfonyPath = str_replace("'", "\\'", dirname(dirname(dirname(__DIR__))));
|
2014-12-23 06:43:53 +00:00
|
|
|
$errorReporting = error_reporting();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2013-09-29 20:03:21 +01:00
|
|
|
$code = <<<EOF
|
2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
2015-12-10 14:12:08 +00:00
|
|
|
error_reporting($errorReporting);
|
2014-12-23 06:43:53 +00:00
|
|
|
|
2010-04-21 11:05:34 +01:00
|
|
|
require_once '$requirePath';
|
|
|
|
|
2013-02-08 13:02:12 +00:00
|
|
|
\$loader = new Symfony\Component\ClassLoader\ClassLoader();
|
|
|
|
\$loader->addPrefix('Symfony', '$symfonyPath');
|
2010-04-21 11:05:34 +01:00
|
|
|
\$loader->register();
|
|
|
|
|
2010-05-06 11:04:50 +01:00
|
|
|
\$kernel = unserialize('$kernel');
|
2013-09-29 20:03:21 +01:00
|
|
|
\$request = unserialize('$request');
|
|
|
|
EOF;
|
|
|
|
|
|
|
|
return $code.$this->getHandleScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getHandleScript()
|
|
|
|
{
|
|
|
|
return <<<'EOF'
|
|
|
|
$response = $kernel->handle($request);
|
|
|
|
|
|
|
|
if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
|
|
|
|
$kernel->terminate($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo serialize($response);
|
2010-04-21 11:05:34 +01:00
|
|
|
EOF;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Converts the BrowserKit request to a HttpKernel request.
|
|
|
|
*
|
2013-04-25 06:52:12 +01:00
|
|
|
* @param DomRequest $request A DomRequest instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Request A Request instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
protected function filterRequest(DomRequest $request)
|
2010-04-21 11:05:34 +01:00
|
|
|
{
|
2011-03-17 23:09:31 +00:00
|
|
|
$httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
|
|
|
|
|
2014-05-20 21:16:09 +01:00
|
|
|
foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
|
|
|
|
$httpRequest->files->set($key, $value);
|
|
|
|
}
|
2011-03-17 23:09:31 +00:00
|
|
|
|
|
|
|
return $httpRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters an array of files.
|
|
|
|
*
|
2011-07-05 21:01:06 +01:00
|
|
|
* This method created test instances of UploadedFile so that the move()
|
|
|
|
* method can be called on those instances.
|
|
|
|
*
|
|
|
|
* If the size of a file is greater than the allowed size (from php.ini) then
|
|
|
|
* an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
|
|
|
|
*
|
2014-12-21 16:36:15 +00:00
|
|
|
* @see UploadedFile
|
2011-03-17 23:09:31 +00:00
|
|
|
*
|
|
|
|
* @param array $files An array of files
|
|
|
|
*
|
|
|
|
* @return array An array with all uploaded files marked as already moved
|
|
|
|
*/
|
|
|
|
protected function filterFiles(array $files)
|
|
|
|
{
|
|
|
|
$filtered = array();
|
|
|
|
foreach ($files as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$filtered[$key] = $this->filterFiles($value);
|
|
|
|
} elseif ($value instanceof UploadedFile) {
|
2011-07-07 09:47:10 +01:00
|
|
|
if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
|
2011-07-05 21:01:06 +01:00
|
|
|
$filtered[$key] = new UploadedFile(
|
|
|
|
'',
|
|
|
|
$value->getClientOriginalName(),
|
|
|
|
$value->getClientMimeType(),
|
|
|
|
0,
|
|
|
|
UPLOAD_ERR_INI_SIZE,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$filtered[$key] = new UploadedFile(
|
|
|
|
$value->getPathname(),
|
|
|
|
$value->getClientOriginalName(),
|
|
|
|
$value->getClientMimeType(),
|
|
|
|
$value->getClientSize(),
|
|
|
|
$value->getError(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2011-03-17 23:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filtered;
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Converts the HttpKernel response to a BrowserKit response.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param Response $response A Response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2013-04-25 06:52:12 +01:00
|
|
|
* @return DomResponse A DomResponse instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
protected function filterResponse($response)
|
2010-04-21 11:05:34 +01:00
|
|
|
{
|
2011-04-13 19:25:35 +01:00
|
|
|
$headers = $response->headers->all();
|
|
|
|
if ($response->headers->getCookies()) {
|
|
|
|
$cookies = array();
|
|
|
|
foreach ($response->headers->getCookies() as $cookie) {
|
|
|
|
$cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
|
|
|
|
}
|
2011-12-15 09:36:36 +00:00
|
|
|
$headers['Set-Cookie'] = $cookies;
|
2011-04-13 19:25:35 +01:00
|
|
|
}
|
|
|
|
|
2012-11-10 10:47:17 +00:00
|
|
|
// this is needed to support StreamedResponse
|
2012-11-09 15:49:48 +00:00
|
|
|
ob_start();
|
|
|
|
$response->sendContent();
|
|
|
|
$content = ob_get_clean();
|
|
|
|
|
|
|
|
return new DomResponse($content, $response->getStatusCode(), $headers);
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
}
|