2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Foundation\Test;
|
|
|
|
|
|
|
|
use Symfony\Foundation\Kernel;
|
2010-05-06 11:04:50 +01:00
|
|
|
use Symfony\Components\HttpKernel\Test\Client as BaseClient;
|
2010-04-21 11:05:34 +01:00
|
|
|
use Symfony\Components\BrowserKit\History;
|
|
|
|
use Symfony\Components\BrowserKit\CookieJar;
|
|
|
|
|
|
|
|
/*
|
2010-04-24 00:22:16 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-21 11:05:34 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Client simulates a browser and makes requests to a Kernel object.
|
|
|
|
*
|
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Foundation
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Client extends BaseClient
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $kernel;
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param Symfony\Foundation\Kernel $kernel A Kernel instance
|
|
|
|
* @param array $server The server parameters (equivalent of $_SERVER)
|
|
|
|
* @param Symfony\Components\BrowserKit\History $history A History instance to store the browser history
|
|
|
|
* @param Symfony\Components\BrowserKit\CookieJar $cookieJar A CookieJar instance to store the cookies
|
|
|
|
*/
|
|
|
|
public function __construct(Kernel $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
|
2010-04-21 11:05:34 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->kernel = $kernel;
|
|
|
|
$this->container = $kernel->getContainer();
|
2010-04-21 11:05:34 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
parent::__construct($kernel->getContainer()->getHttpKernelService(), $server, $history, $cookieJar);
|
|
|
|
|
|
|
|
$this->addTestersFromContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the container.
|
|
|
|
*
|
|
|
|
* @return Symfony\Components\DependencyInjection\Container
|
|
|
|
*/
|
|
|
|
public function getContainer()
|
|
|
|
{
|
|
|
|
return $this->container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the kernel.
|
|
|
|
*
|
|
|
|
* @return Symfony\Foundation\Kernel
|
|
|
|
*/
|
|
|
|
public function getKernel()
|
|
|
|
{
|
|
|
|
return $this->kernel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an tester by name.
|
|
|
|
*
|
|
|
|
* @param string $name The tester alias
|
|
|
|
*
|
|
|
|
* @return Symfony\Foundation\Test\TesterInterface A Tester instance
|
|
|
|
*/
|
|
|
|
public function getTester($name)
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($this->testers[$name]) && !is_object($this->testers[$name])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->container->setService('test.response', $this->getResponse());
|
|
|
|
|
|
|
|
return $this->container->getService($this->testers[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getTester($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a request.
|
|
|
|
*
|
|
|
|
* @param Symfony\Components\HttpKernel\Request $request A Request instance
|
|
|
|
*
|
|
|
|
* @param Symfony\Components\HttpKernel\Response $response A Response instance
|
|
|
|
*/
|
|
|
|
protected function doRequest($request)
|
|
|
|
{
|
|
|
|
$this->kernel->reboot();
|
|
|
|
|
|
|
|
return $this->kernel->handle($request);
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the script to execute when the request must be insulated.
|
|
|
|
*
|
|
|
|
* @param Symfony\Components\HttpKernel\Request $request A Request instance
|
|
|
|
*/
|
|
|
|
protected function getScript($request)
|
|
|
|
{
|
|
|
|
$kernel = serialize($this->kernel);
|
|
|
|
$request = serialize($request);
|
|
|
|
|
|
|
|
$r = new \ReflectionObject($this->kernel);
|
|
|
|
$path = $r->getFileName();
|
|
|
|
|
|
|
|
return <<<EOF
|
2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once '$path';
|
|
|
|
|
|
|
|
\$kernel = unserialize('$kernel');
|
|
|
|
\$kernel->boot();
|
|
|
|
echo serialize(\$kernel->handle(unserialize('$request')));
|
|
|
|
EOF;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-21 11:05:34 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds tester objects from the container.
|
|
|
|
*
|
|
|
|
* This methods adds services with the test.tester annotation as tester objects.
|
|
|
|
*/
|
|
|
|
protected function addTestersFromContainer()
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->container->findAnnotatedServiceIds('test.tester') as $id => $config) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!isset($config[0]['alias'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->testers[$config[0]['alias']] = $id;
|
|
|
|
}
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
}
|