Skip to content

Instantly share code, notes, and snippets.

@Goutte
Forked from leek/PhpunitController.php
Created June 5, 2012 22:37
Show Gist options
  • Save Goutte/2878572 to your computer and use it in GitHub Desktop.
Save Goutte/2878572 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();
$argv[] = 'phpunit';
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;
}
}
}
@Goutte
Copy link
Author

Goutte commented Jun 5, 2012

It was missing 'phpunit' as argv[0] to work with the WebTestCase of Sf2.
See fork : https://gist.github.com/2878572/5bc1aa20e735728a16e03f508e08698670c42a9d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment