3

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 '{'
lordisp
  • 634
  • 9
  • 21
  • 1
    this looks like as if one of your files - at least - has a syntax error. take the time and check all of them for syntax errors, study your directory layout, the php version and related configuration. additional tooling might distract you from your studies: https://github.com/php-parallel-lint/PHP-Parallel-Lint – hakre Jun 29 '21 at 05:50
  • 1
    @hakre, thanks for your replay. I've run other [reduced to a minimum test](https://github.com/sebastianbergmann/php-code-coverage/issues/858#issuecomment-870463210) with the same issue. Testing works fine, its just the coverage report that fail. However, I'll try `PHP-Parallel-Lint`. Thanks for now! – lordisp Jun 29 '21 at 11:21
  • 1
    The error is perhaps happening during code-coverage data processing, so you can (temporarily) disable that to find out if it is the cause. Then you would know that. Enable it again to provoke the error again. Also check `--verbose` flag next to `--debug` (hopefully it helps). – hakre Jun 29 '21 at 12:29

2 Answers2

2

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.

lordisp
  • 634
  • 9
  • 21
  • 1
    Were you able to make phpunit be more verbose and pointing you to the file? – Borjante Nov 23 '21 at 12:46
  • Not really. I simply went through the app structure and phpstorm highlighted the mentioned classes. That's how I found them. – lordisp Nov 24 '21 at 13:10
-1

In case your wondering how he isolated.

Edit your phpunit.xml file

<coverage processUncoveredFiles="true">
    <include>
        <directory suffix=".php">./app</directory>            
    </include>
    <exclude>
        <directory suffix=".php">./app/Http</directory>
        <!-- <file>src/example.php</file> -->
    </exclude>
</coverage>
Harry Bosh
  • 3,611
  • 2
  • 36
  • 34