2010-06-10 17:16:58 +01:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Test;
|
2010-06-10 17:16:58 +01:00
|
|
|
|
2010-09-15 19:49:16 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Client;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
2010-09-17 23:34:56 +01:00
|
|
|
use Symfony\Component\HttpKernel\Test\WebTestCase as BaseWebTestCase;
|
2010-06-10 17:16:58 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebTestCase is the base class for functional tests.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-06-10 17:16:58 +01:00
|
|
|
*/
|
|
|
|
abstract class WebTestCase extends BaseWebTestCase
|
|
|
|
{
|
2010-09-15 19:49:16 +01:00
|
|
|
protected $kernel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Client.
|
|
|
|
*
|
|
|
|
* @param array $options An array of options to pass to the createKernel class
|
|
|
|
* @param array $server An array of server parameters
|
|
|
|
*
|
|
|
|
* @return Client A Client instance
|
|
|
|
*/
|
|
|
|
public function createClient(array $options = array(), array $server = array())
|
|
|
|
{
|
|
|
|
$this->kernel = $this->createKernel($options);
|
|
|
|
$this->kernel->boot();
|
|
|
|
|
2010-11-23 21:43:09 +00:00
|
|
|
$client = $this->kernel->getContainer()->get('test.client');
|
2010-09-15 19:49:16 +01:00
|
|
|
$client->setServerParameters($server);
|
|
|
|
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
2010-06-10 17:16:58 +01:00
|
|
|
/**
|
|
|
|
* Creates a Kernel.
|
|
|
|
*
|
|
|
|
* If you run tests with the PHPUnit CLI tool, everything will work as expected.
|
|
|
|
* If not, override this method in your test classes.
|
|
|
|
*
|
2010-06-16 09:19:24 +01:00
|
|
|
* Available options:
|
|
|
|
*
|
|
|
|
* * environment
|
|
|
|
* * debug
|
|
|
|
*
|
|
|
|
* @param array $options An array of options
|
2010-06-10 17:26:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return HttpKernelInterface A HttpKernelInterface instance
|
2010-06-10 17:16:58 +01:00
|
|
|
*/
|
2010-06-16 09:19:24 +01:00
|
|
|
protected function createKernel(array $options = array())
|
2010-06-10 17:16:58 +01:00
|
|
|
{
|
2011-01-07 13:51:52 +00:00
|
|
|
if (isset($_SERVER['KERNEL_DIR'])) {
|
|
|
|
$dir = $_SERVER['KERNEL_DIR'];
|
2010-08-11 14:30:17 +01:00
|
|
|
} else {
|
2011-01-07 13:51:52 +00:00
|
|
|
// black magic below, you have been warned!
|
|
|
|
$dir = getcwd();
|
|
|
|
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
|
|
|
|
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the --configuration flag from PHPUnit
|
|
|
|
$cli = implode(' ', $_SERVER['argv']);
|
|
|
|
if (preg_match('/\-\-configuration[= ]+([^ ]+)/', $cli, $matches)) {
|
|
|
|
$dir = $dir.'/'.$matches[1];
|
|
|
|
} elseif (preg_match('/\-c +([^ ]+)/', $cli, $matches)) {
|
|
|
|
$dir = $dir.'/'.$matches[1];
|
|
|
|
} elseif (file_exists(getcwd().'/phpunit.xml') || file_exists(getcwd().'/phpunit.xml.dist')) {
|
|
|
|
$dir = getcwd();
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException('Unable to guess the Kernel directory.');
|
|
|
|
}
|
2010-08-11 14:30:17 +01:00
|
|
|
|
2011-01-07 13:51:52 +00:00
|
|
|
if (!is_dir($dir)) {
|
|
|
|
$dir = dirname($dir);
|
|
|
|
}
|
2010-06-10 17:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->name('*Kernel.php')->in($dir);
|
|
|
|
if (!count($finder)) {
|
|
|
|
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = current(iterator_to_array($finder));
|
|
|
|
$class = $file->getBasename('.php');
|
|
|
|
unset($finder);
|
|
|
|
|
|
|
|
require_once $file;
|
|
|
|
|
2010-06-16 09:19:24 +01:00
|
|
|
return new $class(
|
|
|
|
isset($options['environment']) ? $options['environment'] : 'test',
|
2010-09-23 07:51:42 +01:00
|
|
|
isset($options['debug']) ? $options['debug'] : true
|
2010-06-16 09:19:24 +01:00
|
|
|
);
|
2010-06-10 17:16:58 +01:00
|
|
|
}
|
|
|
|
}
|