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;
|
|
|
|
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>
|
2017-02-05 17:30:23 +00:00
|
|
|
*
|
|
|
|
* @method Request|null getRequest() A Request instance
|
|
|
|
* @method Response|null getResponse() A Response instance
|
2010-04-21 11:05:34 +01:00
|
|
|
*/
|
|
|
|
class Client extends BaseClient
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $kernel;
|
2017-05-24 16:00:21 +01:00
|
|
|
private $catchExceptions = true;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
|
2017-05-24 16:00:21 +01:00
|
|
|
/**
|
|
|
|
* Sets whether to catch exceptions when the kernel is handling a request.
|
|
|
|
*
|
|
|
|
* @param bool $catchExceptions Whether to catch exceptions
|
|
|
|
*/
|
|
|
|
public function catchExceptions($catchExceptions)
|
|
|
|
{
|
|
|
|
$this->catchExceptions = $catchExceptions;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Makes a request.
|
|
|
|
*
|
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)
|
|
|
|
{
|
2017-05-24 16:00:21 +01:00
|
|
|
$response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $this->catchExceptions);
|
2011-12-15 18:17:27 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
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));
|
2014-12-23 06:43:53 +00:00
|
|
|
$errorReporting = error_reporting();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2017-01-20 09:53:58 +00:00
|
|
|
$requires = '';
|
|
|
|
foreach (get_declared_classes() as $class) {
|
|
|
|
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
|
|
|
|
$r = new \ReflectionClass($class);
|
|
|
|
$file = dirname(dirname($r->getFileName())).'/autoload.php';
|
|
|
|
if (file_exists($file)) {
|
|
|
|
$requires .= "require_once '".str_replace("'", "\\'", $file)."';\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$requires) {
|
|
|
|
throw new \RuntimeException('Composer autoloader not found.');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-01-20 09:53:58 +00:00
|
|
|
$requires
|
2010-04-21 11:05:34 +01:00
|
|
|
|
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.
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
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
|
|
|
{
|
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();
|
|
|
|
|
2016-11-19 14:10:17 +00:00
|
|
|
return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
}
|