0

When I activate Xdebug and run a PHPUnit unittest this error occurs: ❌

Warning: assert(): assert($iterator instanceof FilterIterator) failed in [...]/vendor/phpunit/phpunit/src/Runner/Filter/Factory.php on line 57
[... some stack trace information ...]
Return value of PHPUnit\Runner\Filter\Factory::factory() must be an instance of FilterIterator, instance of PHPUnit\Framework\TestSuiteIterator returned

I'm using

PHPUnit 9.5.6 by Sebastian Bergmann and contributors.
Runtime: PHP 7.4.21 with Xdebug 2.8.0

The PHPUnit execution call (clued together with PhpStorm) looks like:

/usr/bin/php74.bin.cli 
-dzend_extension=/usr/local/php74/lib/php/extensions/xdebug.so 
-dxdebug.collect_params=5 
-dxdebug.profiler_enable=on 
-dxdebug.auto_trace=1 -dxdebug.trace_format=1 
-dxdebug.collect_return=1 -ddisplay_errors=1 
-ddisplay_startup_errors=1 -derror_reporting=E_ALL 
-dmemory_limit=512M 
[...]/vendor/phpunit/phpunit/phpunit 
--coverage-filter [...]/src/ 
--bootstrap [...]/utils/unittest/bootstrap.php 
--configuration [...]/utils/unittest/phpunit.xml 
--filter "/(BasicsTest::testReturnBytes)( .*)?$/" 
--test-suffix BasicsTest.php [...]/utils/unittest/basics --teamcity

Calling a simple php script with Xdebug activated works well. ✔️

/usr/bin/php74.bin.cli 
-dzend_extension=/usr/local/php74/lib/php/extensions/xdebug.so 
-dxdebug.collect_params=5 
-dxdebug.profiler_enable=on -dxdebug.auto_trace=1 
-dxdebug.trace_format=1 -dxdebug.collect_return=1 
-ddisplay_errors=1 -ddisplay_startup_errors=1 
-derror_reporting=E_ALL -dmemory_limit=512M ./test.php

Calling the unittests without Xdebug activated works well. ✔️

/usr/bin/php74.bin.cli 
-dallow_url_fopen=1 
[...]/vendor/phpunit/phpunit/phpunit 
--no-coverage 
--bootstrap [...]/utils/unittest/bootstrap.php 
--configuration [...]/utils/unittest/phpunit.xml 
--filter "/(BasicsTest::testReturnBytes)( .*)?$/" 
--test-suffix BasicsTest.php [...]/utils/unittest/basics --teamcity

My phpunit.xml

<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
         bootstrap="bootstrap.php"
         colors="true"
         verbose="true">
  <coverage>
    <include>
      <directory suffix=".php">../../../src/*</directory>
      <directory suffix=".php">../../../some other dirs/*</directory>
    </include>
    <exclude>
        <directory suffix=".php">../../../some other dirs/*</directory>
    </exclude>
    <report>
        <html outputDirectory="./x_testresults" lowUpperBound="35" highLowerBound="70"/>
    </report>
  </coverage>
  <php>
    <ini name="allow_url_fopen" value="On"/>
    <ini name="memory_limit" value="5G"/>
    <ini name="include_path" value="."/>
  </php>
</phpunit>

The unittest

<?php
declare(strict_types = 1);

class BasicsTest extends PHPUnit\Framework\TestCase
{
    function testReturnBytes(): void {
        $this->assertSame(1, 1);
    }
}

The docs say in https://phpunit.readthedocs.io/en/9.5/code-coverage-analysis.html#including-files that

It is mandatory to configure a filter for telling PHPUnit which source code files to include in the code coverage report. This can either be done using the --coverage-filter command line option or via the configuration file (see The Element).

This is what I did, see above. Also without setting a --coverage-filter parameter PHPUnit complains about missing filter information, so I added it.

For me it feels like the issue is related to the --coverage-filter parameter. The error message (see first code paragraph) says FilterIterator should be instance of PHPUnit\Framework\TestSuiteIterator but I don't know where to adjust this.

8
  • this looks puzzling. on first glance I would perhaps re-init phpunit (move your config away and let phpunit init create a new one), just for quick testing purposes. Then I would swap the configuration files again and run with xdebug 3. Commented Jul 15, 2021 at 17:26
  • and do you really have * in the XML? It looks like it breaks the syntax highlighting here on SO but I also think its not needed. Commented Jul 15, 2021 at 17:29
  • @hakre I did already use different configs, no positive result. The * is there. Will try without it. Commented Jul 15, 2021 at 18:29
  • @hakre no change without the * in the XML. Commented Jul 15, 2021 at 18:36
  • Can you double-check xdebug 2 (which is EOL) has still support for code-coverage in and is fully compatible with phpunit 9.5? Not that at the end it is just an incompatibility. Commented Jul 15, 2021 at 18:42

1 Answer 1

0

I found some mistakes in my setup and I found a kind of workaround to get the tests to be running with Xdebug.

The mistakes:

  • the includes and excludes tags in the phpunit.xml had wrong relative paths
  • the settings "-dxdebug.auto_trace=1" for xdebug on the command line command were outdated, see docs. Actually its strange that the test.php did run well.

The workaround:

  • set a testsuite to "." in the phpunit.xml and call it via --testsuite default
  • set a "@group dummy" to the specific class I want to test and call it via --group dummy to just call that test.

The testsuite setting in phpunit.xml in detail:

<testsuites>
    <testsuite name="default">
        <directory>./</directory>
    </testsuite>
</testsuites>

The commandline call now:

/usr/bin/php74.bin.cli 
-dzend_extension=/usr/local/php74/lib/php/extensions/xdebug.so 
-dxdebug.collect_params=5 
-dxdebug.profiler_enable=on 
-dxdebug.trace_format=1 
-dxdebug.collect_return=1 
../../vendor/bin/phpunit 
--group no-db 
--verbose 
--debug 
--testsuite default 
--group dummy
Sign up to request clarification or add additional context in comments.

3 Comments

"the settings "-dxdebug.auto_trace=1" for xdebug on the command line command were outdated, see docs." Why outdated? As per your info (Runtime: PHP 7.4.21 with Xdebug 2.8.0) that option is valid for Xdebug v2; the "see docs" link is for Xdebug v3.
@lazyone good point. Need to revisit that.
Xdebug 2 is no longer supported though, so please upgrade! You also really don't want the profiler on when using code coverage. With Xdebug 2, desides the -dzend_extension= line, none of the xdebug settings have any influence on Xdebug's code coverage collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.