PHPUnit Symfony Test Suite

Recently I was looking at tests at the symfony/symfony repository and found some interesting things. Because there were so many skipped tests I wanted to know the reason why so many tests on symfony were skipped. So I ran `phpunit –tap` on under `vendor/symfony/symfony` folder. I then realized there were some skipped tests mainly due to the mismatching intl and icu extension and versions, some other reasons and among them that we were not plugging the memcached, memcache, and mongodb php extensions. The option `tap` was very useful in doing this and I submitted a PR here.

Screenshot 2013-12-27 08.36.11

Besides this I wondered how can I run phpunit so that it will give me a note on which was the bottleneck in terms of tests performance. Which test is taking too long on the symfony suite?

I needed to do a custom logger or listener for phpunit. Here is the code output:

...
14.584405 = ok 2985 14.584405 - Symfony\Component\Finder\Tests\FinderTest :: testNonSeekableStream
 
Time: 2.54 minutes, Memory: 749.50Mb

So now we know which is the slowest test and the reason is because of the network dependency:

public function testNonSeekableStream()
{
    try {
        $i = Finder::create()->in('ftp://ftp.mozilla.org/')->depth(0)->getIterator();
    } catch (\UnexpectedValueException $e) {
        $this->markTestSkipped(sprintf('Unsupported stream "%s".', 'ftp'));
    }
 
    $contains = array(
        'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README',
        'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html',
        'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub',
    );
 
    $this->assertIteratorInForeach($contains, $i);
}

You have to add it to the phpunit.xml.dist like this:

<listeners>
    <listener class="CustomLogger" file="./CustomLogger.php" />
</listeners>

And finally what makes it possible:

<?php
 
class CustomLogger extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
{
    protected $testNumber = 0;
    protected $testSuiteLevel = 0;
    protected $testSuccessful = TRUE;
    protected $largestTime;
    protected $largestTest;
 
    public function __construct($out = NULL)
    {
        parent::__construct($out);
        $this->largestTest = '';
        $this->largestTime = 0.0;
    }
 
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
    }
 
    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
    {
    }
 
    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
    }
 
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
        $this->testSuccessful = FALSE;
    }
 
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        $this->testSuiteLevel++;
    }
 
    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        $this->testSuiteLevel--;
 
        if ($this->testSuiteLevel == 0) {
            $this->write(sprintf(
                "\n\n%f = %s\n",
                $this->largestTime,
                $this->largestTest
            ));
        }
    }
 
    public function startTest(PHPUnit_Framework_Test $test)
    {
        $this->testNumber++;
        $this->testSuccessful = TRUE;
    }
 
    public function endTest(PHPUnit_Framework_Test $test, $time)
    {
        if ($time > $this->largestTime) {
            $this->largestTime = $time;
            $this->largestTest = sprintf(
                "ok %d %f - %s\n",
                $this->testNumber,
                $time,
                get_class($test).' :: '.$test->getName()
            );
        }
        if ($this->testSuccessful === TRUE) {
            $this->write(
                sprintf(
                    "ok %d %f - %s\n",
                    $this->testNumber,
                    $time,
                    get_class($test).' :: '.$test->getName()
                )
            );
        }
    }
 
    protected function writeNotOk(PHPUnit_Framework_Test $test, $prefix = '', $directive = '')
    {
        $this->testSuccessful = FALSE;
    }
}

Thanks for reading and hope you find this very useful at work!

Encouragements in all good,

Luis

One thought on “PHPUnit Symfony Test Suite

  1. Typically the drone takes off, flying a few feet off the ground.
    I guide it up to about 12 feet with the accelerator around the remote control.
    After that my three year older son takes over. This individual taps the screen on an iPad running our piloting app.
    The drone, a new DJI Phantom 4, commences to ascend, heading in the direction of an abandoned grainery.
    My son happily taps apart at the screen, shifting the drone here plus
    there. You wouldn’t discover how frantically he’s going from the footage he or
    she captured, which is buttery smooth.

Leave a Reply to www Cancel reply

Your email address will not be published. Required fields are marked *