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\HttpKernel\HttpKernelInterface ;
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
*/
2011-06-16 18:03:48 +01:00
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
2010-06-10 17:16:58 +01:00
{
2011-07-19 12:11:12 +01:00
static protected $class ;
2011-06-11 15:06:29 +01:00
static protected $kernel ;
2010-09-15 19:49:16 +01:00
/**
* Creates a Client .
*
* @ param array $options An array of options to pass to the createKernel class
* @ param array $server An array of server parameters
*
2011-08-13 18:27:36 +01:00
* @ return Client A Client instance
2010-09-15 19:49:16 +01:00
*/
2011-07-26 08:22:13 +01:00
static protected function createClient ( array $options = array (), array $server = array ())
2010-09-15 19:49:16 +01:00
{
2011-06-11 15:06:29 +01:00
static :: $kernel = static :: createKernel ( $options );
static :: $kernel -> boot ();
2010-09-15 19:49:16 +01:00
2011-06-11 15:06:29 +01:00
$client = static :: $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-06-11 15:06:29 +01:00
static protected function getPhpUnitXmlDir ()
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-06-11 15:06:29 +01:00
$dir = static :: getPhpUnitCliConfigArgument ();
2011-04-05 07:24:46 +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
*/
2011-06-11 15:06:29 +01:00
static private function getPhpUnitCliConfigArgument ()
2011-03-29 04:42:27 +01:00
{
$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 ;
2011-04-05 07:24:46 +01:00
} elseif ( strpos ( $testArg , '--configuration=' ) === 0 ) {
2011-03-29 04:42:27 +01:00
$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-06-11 15:06:29 +01:00
static protected function getKernelClass ()
2011-01-11 22:27:28 +00:00
{
2011-06-11 15:06:29 +01:00
$dir = isset ( $_SERVER [ 'KERNEL_DIR' ]) ? $_SERVER [ 'KERNEL_DIR' ] : static :: getPhpUnitXmlDir ();
2011-01-11 22:27:28 +00:00
$finder = new Finder ();
2011-07-19 11:51:57 +01:00
$finder -> name ( '*Kernel.php' ) -> depth ( 0 ) -> in ( $dir );
2011-11-04 17:29:15 +00:00
$results = iterator_to_array ( $finder );
if ( ! count ( $results )) {
throw new \RuntimeException ( 'Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.' );
2011-01-11 22:27:28 +00:00
}
2011-11-04 17:29:15 +00:00
$file = current ( $results );
2011-01-11 22:27:28 +00:00
$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
*/
2011-06-11 15:06:29 +01:00
static protected function createKernel ( array $options = array ())
2010-06-10 17:16:58 +01:00
{
2011-07-19 12:11:12 +01:00
if ( null === static :: $class ) {
static :: $class = static :: getKernelClass ();
}
2010-06-10 17:16:58 +01:00
2011-07-19 12:11:12 +01:00
return new static :: $class (
2010-06-16 09:19:24 +01:00
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
}
2011-05-11 09:45:23 +01:00
2011-05-03 05:17:33 +01:00
/**
2011-05-11 09:45:23 +01:00
* Shuts the kernel down if it was used in the test .
2011-05-03 05:17:33 +01:00
*/
protected function tearDown ()
{
2011-06-11 15:06:29 +01:00
if ( null !== static :: $kernel ) {
static :: $kernel -> shutdown ();
2011-05-03 05:17:33 +01:00
}
}
2010-06-10 17:16:58 +01:00
}