laravelphpunitphp-code-coverage

Generating code coverage report in Clover XML format ... syntax error


I'm using phpstorm and wrote some test in my laravel app. The phpunit.xml is alsmost default.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

At the end of my thest I get the following error:

Time: 00:15.742, Memory: 58.00 MB

OK (26 tests, 69 assertions)

Generating code coverage report in Clover XML format ... syntax error, unexpected '-', expecting '{'

Process finished with exit code 2

I've no idea where I shall start to look at. Appreciate any help!

Update:

same issue by trying to generate html report

❯ .\vendor\bin\phpunit --coverage-html ./coverage.html
PHPUnit 9.5.5 by Sebastian Bergmann and contributors.

............................                                      28 / 28 (100%)

Time: 00:23.702, Memory: 74.00 MB

OK (28 tests, 125 assertions)

Generating code coverage report in HTML format ... syntax error, unexpected '-', expecting '{'

Update 2:

Run reduced minimal reproducing test case:

class MinimalTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('https://google.com');

        $response->assertStatus(200);
    }
}

❯ .\vendor\bin\phpunit --filter MinimalTest --coverage-html ./coverage.html

❯ .\vendor\bin\phpunit --debug --filter MinimalTest --coverage-html ./coverage.html
PHPUnit 9.5.5 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.16 with Xdebug 3.0.4
Configuration: C:\Users\***\localGit\xampp\htdocs\oneup\app\phpunit.xml

Test 'Tests\Feature\MinimalTest::test_example' started
Test 'Tests\Feature\MinimalTest::test_example' ended


Time: 00:01.242, Memory: 42.00 MB

OK (1 test, 1 assertion)

Generating code coverage report in HTML format ... syntax error, unexpected '-', expecting '{'

Solution

  • I finally found the issue. I isolated the root cause by excluding directories of Laravel. The bad directory was actually the app/View due too an early mistake I made by generating laravel components Laravel generates view and controller files for a component. It's not always needed to touch the controller if you don't need php logic, which is why I did not see the issue.

    I've created the components these times with a wrong pattern, which led Laravel to generate broken classes (my bad, not Laravel's). By cleaning those out, the coverage report was successfully generated without any exclusions.

    Thanks to @hakre, I'm aware my issue was very generic and not really reproduceable.