When your testsuite starts to grow you’ll soon find yourself wishing you had spent more time creating simple unittests with mockups and created less of those fancy Symfony2 functional tests.
Here are some tips that you can use to speed up any PHPUnit testssuite - with a Symfony2 bonus tip.
I start off with this Xdebug configuration:
zend_extension=/usr/lib/php5/20090626/xdebug.so
xdebug.show_mem_delta = 1
xdebug.profiler_enable_trigger=1
xdebug.max_nesting_level=600
xdebug.profiler_enable=0
xdebug.coverage_enable=0
Now, my research found tips on how to disable xdebug by adding and removing a config file from PHPs INI_SCAN_DIR, but I found that that was way too much hassle for me. Instead I enable xdebug by default and disable all options.( http://www.boxuk.com/blog/fast-phpunit-and-xdebug-code-coverage/)
Tip 1: Disable as much as possible of Xdebug.
For your normal tests, disable xdebug as much as you can:
<php>
<ini name="xdebug.default_enable" value="0" />
<ini name="xdebug.remote_autostart" value="0" />
<ini name="xdebug.remote_enable" value="0" />
<ini name="xdebug.overload_var_dump" value="0" />
<ini name="xdebug.show_mem_delta" value="0" />
</php>
Tip 2: Only enable coverage when you need it.
Keep a separate phpunit file for coverage tests where you enable coverage:
<php>
<ini name="xdebug.default_enable" value="1" />
<ini name="xdebug.enable_coverage" value="1" />
<ini name="xdebug.remote_autostart" value="0" />
<ini name="xdebug.remote_enable" value="0" />
<ini name="xdebug.overload_var_dump" value="0" />
<ini name="xdebug.show_mem_delta" value="0" />
</php>
Bonus tip:
Originally this tip comes from Kris Wallsmith.
Add this to your Symfony2 kernel:
class AppKernel extends Kernel
{
// ...
protected function initializeContainer()
{
static $first = true;
if ('test' !== $this->getEnvironment()) {
parent::initializeContainer();
return;
}
$debug = $this->debug;
if (!$first) {
// disable debug mode on all but the first initialization
$this->debug = false;
}
// will not work with --process-isolation
$first = false;
try {
parent::initializeContainer();
} catch (\Exception $e) {
$this->debug = $debug;
throw $e;
}
$this->debug = $debug;
}
}
And your tests will run much faster because Symfony2 doesn’t check if any resources have been changed between each time it starts a new kernel.