Skip to content

Instantly share code, notes, and snippets.

@leek
Created January 6, 2012 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leek/1571131 to your computer and use it in GitHub Desktop.
Save leek/1571131 to your computer and use it in GitHub Desktop.
PhpunitController for Symfony2
class PhpunitController extends Controller
{
public function runTestsAction($filterClass = null)
{
// Make sure PHPUnit is autoloaded
require_once('PHPUnit/Autoload.php');
set_time_limit(0);
$version = \PHPUnit_Runner_Version::id();
$kernel_dir = $this->container->getParameter('kernel.root_dir');
chdir($kernel_dir);
// This will force the printer class to be autoloaded by Symfony, before PHPUnit tries to (and does not) find it
$printerClass = 'PW\CoreBundle\PHPUnit\HtmlResultPrinter';
if (!class_exists($printerClass)) {
$printerClass = false;
}
$argv = array();
if ($filterClass) {
$argv[] = '--filter';
$argv[] = $filterClass;
}
if (version_compare($version, "3.6.0") >= 0) {
if ($printerClass) {
$argv[] = '--printer';
$argv[] = $printerClass;
}
$_SERVER['argv'] = $argv;
\PHPUnit_TextUI_Command::main(true);
} else {
ob_end_clean();
echo '<pre>';
$_SERVER['argv'] = $argv;
\PHPUnit_TextUI_Command::main(false);
echo '</pre>';
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment