Search code examples
symfonyphpunitphpstorm

PHPUnit TestListener does not work in PhpStorm


I'm using Symfony 2, PHPUnit 3.7.10 and PhpStorm 5.0.4.

I have a class MyTestListener (implements PHPUnit_Framework_TestListener) in the namespace Acme\DemoBundle\Tests.

When I call phpunit from CLI with vendor/bin/phpunit.bat -c app/ The listener work, the test is successful.

I found this: PHPUnit, PHPUnit_Framework_TestListener, Netbeans and the PHPUnit xml config but it seems to be another problem.

But when I start the test in PhpStorm the, the listener does not load, some objects should be injected into an abstract test class, which are null in this case. I also notices, that when I debug with a breakpoint in the MyTestListener class the debugger doesn't stop in this class, others breakpoints work fine.

the phpunit.xml entry:

    <listeners>
       <listener class="Acme\DemoBundle\Tests\MyTestListener" file="../src/Acme/DemoBundle/Tests/MyTestListener.php">
       </listener>
    </listeners>

I also try to add a empty element, it doesn't help.

In my Run/Debug Configuration of PhpStorm I specify phpunit.xml and the bootstrap file. I also add this option for the Command Line/Interpreter: -d auto_prepend_file=vendor/autoload.php

The listener configuration in phpunit.xml seems to be parsed, but not work. When I change the class name to an invalid file/class, I get an error, that the file can't open the stream. I also try to use a absolute file path or change the slashes into backslashes, nothing works.


Solution

  • What a shame... I found my own error

    I do all tests from CLI, but in PhpStorm only one test class, so the suite name was different and in my listener class I do the stuff only if the suite name match:

    if ($suite->getName() == "db-tests") {...}
    

    So the solution was to extends the expression to

    $name = $suite->getName();
    if ($name == "db-tests" || 
       strpos($name, 'Acme\DemoBundle\Tests\Database') !== false) {...}