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-07-09 09:12:56 +01:00
|
|
|
use Symfony\Framework\Test\WebTestCase as BaseWebTestCase;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
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.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
abstract class WebTestCase extends BaseWebTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
// 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)) {
|
2010-08-11 14:30:17 +01:00
|
|
|
$dir = $dir.'/'.$matches[1];
|
|
|
|
} elseif (preg_match('/\-c +([^ ]+)/', $cli, $matches)) {
|
|
|
|
$dir = $dir.'/'.$matches[1];
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException('Unable to guess the Kernel directory.');
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
isset($options['debug']) ? $debug : true
|
|
|
|
);
|
2010-06-10 17:16:58 +01:00
|
|
|
}
|
|
|
|
}
|