2010-06-10 17:16:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-06-10 17:16:58 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Test;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Client;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
use Symfony\Component\HttpKernel\Test\WebTestCase as BaseWebTestCase;
|
|
|
|
|
2010-06-10 17:16:58 +01:00
|
|
|
/**
|
|
|
|
* WebTestCase is the base class for functional tests.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.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;
|
|
|
|
}
|
|
|
|
|
2011-01-11 22:27:28 +00:00
|
|
|
/**
|
2011-01-13 07:53:44 +00:00
|
|
|
* Finds the directory where the phpunit.xml(.dist) is stored.
|
2011-01-11 22:27:28 +00:00
|
|
|
*
|
2011-01-13 07:53:44 +00:00
|
|
|
* If you run tests with the PHPUnit CLI tool, everything will work as expected.
|
|
|
|
* If not, override this method in your test classes.
|
|
|
|
*
|
|
|
|
* @return string The directory where phpunit.xml(.dist) is stored
|
2011-01-11 22:27:28 +00:00
|
|
|
*/
|
2011-01-13 07:53:44 +00:00
|
|
|
protected function getPhpUnitXmlDir()
|
2011-01-11 22:27:28 +00:00
|
|
|
{
|
2011-03-29 03:41:58 +01:00
|
|
|
$dir = null;
|
2011-01-11 22:27:28 +00:00
|
|
|
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
|
|
|
|
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
|
|
|
|
}
|
|
|
|
|
2011-03-29 04:42:27 +01:00
|
|
|
$dir = $this->getPhpUnitCliConfigArgument();
|
2011-03-29 11:54:32 +01:00
|
|
|
if ($dir === null &&
|
|
|
|
(file_exists(getcwd() . DIRECTORY_SEPARATOR . 'phpunit.xml') ||
|
|
|
|
file_exists(getcwd() . DIRECTORY_SEPARATOR . 'phpunit.xml.dist'))) {
|
2011-03-29 04:42:27 +01:00
|
|
|
$dir = getcwd();
|
2011-03-29 03:41:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can't continue
|
|
|
|
if ($dir === null) {
|
2011-01-11 22:27:28 +00:00
|
|
|
throw new \RuntimeException('Unable to guess the Kernel directory.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dir($dir)) {
|
|
|
|
$dir = dirname($dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
2011-03-29 04:42:27 +01:00
|
|
|
/**
|
|
|
|
* Finds the value of configuration flag from cli
|
|
|
|
*
|
|
|
|
* PHPUnit will use the last configuration argument on the command line, so this only returns
|
|
|
|
* the last configuration argument
|
|
|
|
*
|
|
|
|
* @return string The value of the phpunit cli configuration option
|
|
|
|
*/
|
|
|
|
private function getPhpUnitCliConfigArgument()
|
|
|
|
{
|
|
|
|
$dir = null;
|
2011-03-29 04:58:43 +01:00
|
|
|
$reversedArgs = array_reverse($_SERVER['argv']);
|
|
|
|
foreach ($reversedArgs as $argIndex=>$testArg) {
|
2011-03-29 04:42:27 +01:00
|
|
|
if ($testArg === '-c' || $testArg === '--configuration') {
|
2011-03-29 04:58:43 +01:00
|
|
|
$dir = realpath($reversedArgs[$argIndex - 1]);
|
2011-03-29 04:42:27 +01:00
|
|
|
break;
|
|
|
|
} else if (strpos($testArg, '--configuration=') === 0) {
|
|
|
|
$argPath = substr($testArg, strlen('--configuration='));
|
|
|
|
$dir = realpath($argPath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
2011-01-11 22:27:28 +00:00
|
|
|
/**
|
2011-01-13 07:53:44 +00:00
|
|
|
* Attempts to guess the kernel location.
|
2011-01-11 22:27:28 +00:00
|
|
|
*
|
2011-01-13 07:53:44 +00:00
|
|
|
* When the Kernel is located, the file is required.
|
|
|
|
*
|
|
|
|
* @return string The Kernel class name
|
2011-01-11 22:27:28 +00:00
|
|
|
*/
|
2011-01-13 07:53:44 +00:00
|
|
|
protected function getKernelClass()
|
2011-01-11 22:27:28 +00:00
|
|
|
{
|
2011-01-13 07:53:44 +00:00
|
|
|
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : $this->getPhpUnitXmlDir();
|
2011-01-11 22:27:28 +00: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');
|
|
|
|
|
2011-01-13 07:53:44 +00:00
|
|
|
require_once $file;
|
|
|
|
|
|
|
|
return $class;
|
2011-01-11 22:27:28 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 17:16:58 +01:00
|
|
|
/**
|
|
|
|
* Creates a Kernel.
|
|
|
|
*
|
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-13 07:53:44 +00:00
|
|
|
$class = $this->getKernelClass();
|
2010-06-10 17:16:58 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|