matthiasnoback / live-code-coverage

Generate code coverage reports on a live server
MIT License
116 stars 9 forks source link

Unable to merge with phpcov with Symfony #20

Closed b2p-fred closed 2 years ago

b2p-fred commented 2 years ago

I give a try to this package with a recent (e.g. version 5/6) Symfony application and I collected a bunch of .cov files. I tried to compile all these files with the most recent (e.g. 8.2) version of phpcov without any success:

./vendor/bin/phpcov merge --text ./artifacts/live-coverage/ ./artifacts/live-coverage/
phpcov 8.2.0 by Sebastian Bergmann.

Generating code coverage report in text format ... PHP Fatal error:  Uncaught Error: Class "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" not found in /var/www/src/Command/Export/ExportAnswersCommand.php:34
Stack trace:
#0 /var/www/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(540): include_once()
#1 /var/www/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(167): SebastianBergmann\CodeCoverage\CodeCoverage->processUncoveredFilesFromFilter()
#2 /var/www/vendor/phpunit/php-code-coverage/src/Node/Builder.php(44): SebastianBergmann\CodeCoverage\CodeCoverage->getData()
#3 /var/www/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(139): SebastianBergmann\CodeCoverage\Node\Builder->build(Object(SebastianBergmann\CodeCoverage\CodeCoverage))
#4 /var/www/vendor/phpunit/php-code-coverage/src/Report/Text.php(89): SebastianBergmann\CodeCoverage\CodeCoverage->getReport()
#5 /var/www/vendor/phpunit/phpcov/src/cli/Command.php(143): SebastianBergmann\CodeCoverage\Report\Text->process(Object(SebastianBergmann\CodeCoverage\CodeCoverage), false)
#6 /var/www/vendor/phpunit/phpcov/src/cli/MergeCommand.php(84): SebastianBergmann\PHPCOV\Command->handleReports(Object(SebastianBergmann\CodeCoverage\CodeCoverage), Object(SebastianBergmann\PHPCOV\Arguments))
#7 /var/www/vendor/phpunit/phpcov/src/cli/Application.php(47): SebastianBergmann\PHPCOV\MergeCommand->run(Object(SebastianBergmann\PHPCOV\Arguments))
#8 /var/www/vendor/phpunit/phpcov/phpcov(30): SebastianBergmann\PHPCOV\Application->run(Array)
#9 {main}
  thrown in /var/www/src/Command/Export/ExportAnswersCommand.php on line 34

Fatal error: Uncaught Error: Class "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" not found in /var/www/src/Command/Export/ExportAnswersCommand.php:34
Stack trace:
#0 /var/www/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(540): include_once()
#1 /var/www/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(167): SebastianBergmann\CodeCoverage\CodeCoverage->processUncoveredFilesFromFilter()
#2 /var/www/vendor/phpunit/php-code-coverage/src/Node/Builder.php(44): SebastianBergmann\CodeCoverage\CodeCoverage->getData()
#3 /var/www/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(139): SebastianBergmann\CodeCoverage\Node\Builder->build(Object(SebastianBergmann\CodeCoverage\CodeCoverage))
#4 /var/www/vendor/phpunit/php-code-coverage/src/Report/Text.php(89): SebastianBergmann\CodeCoverage\CodeCoverage->getReport()
#5 /var/www/vendor/phpunit/phpcov/src/cli/Command.php(143): SebastianBergmann\CodeCoverage\Report\Text->process(Object(SebastianBergmann\CodeCoverage\CodeCoverage), false)
#6 /var/www/vendor/phpunit/phpcov/src/cli/MergeCommand.php(84): SebastianBergmann\PHPCOV\Command->handleReports(Object(SebastianBergmann\CodeCoverage\CodeCoverage), Object(SebastianBergmann\PHPCOV\Arguments))
#7 /var/www/vendor/phpunit/phpcov/src/cli/Application.php(47): SebastianBergmann\PHPCOV\MergeCommand->run(Object(SebastianBergmann\PHPCOV\Arguments))
#8 /var/www/vendor/phpunit/phpcov/phpcov(30): SebastianBergmann\PHPCOV\Application->run(Array)
#9 {main}
  thrown in /var/www/src/Command/Export/ExportAnswersCommand.php on line 34

This is how I used the live code coverage:

<?php

use App\Kernel;
use LiveCodeCoverage\LiveCodeCoverage;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    $shutDownCodeCoverage = LiveCodeCoverage::bootstrap(
        true,
        __DIR__.'/../artifacts/live-coverage',
        __DIR__.'/../phpunit.xml.dist'
    );

    // Run your web application now...
    $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);

    // This will save and store collected coverage data:
    $shutDownCodeCoverage();

    return $kernel;
};

Any thoughts about this ? Many thanks in advance.

PS: I googled for a while to find more information about the phpcov merge feature and such a problem but I got no success... perhaps you already encountered the same problem in one of your project :wink:

matthiasnoback commented 2 years ago

Just to be clear; the ExportAnswersCommand is your own code, right?

b2p-fred commented 2 years ago

Yes. Sorry I should have been more precise about this. ExportanswersCommand is a function in my source code.

matthiasnoback commented 2 years ago

No problem, so then to me this looks like that class itself extends from Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand which can't be auto-loaded. Is this class available in your project? I don't know why phpcov needs it anyway, nor why this code is even loaded, because phpcov is supposed to just merge .cov files.

b2p-fred commented 2 years ago

I do agree with you ... I do not understand why phpcov tries to load some PHP files.

Indeed, my project is based on the Symfony framework and all these classes are involved in the project. Anyway, I do not know how to make phpcov autoload my project included files.

matthiasnoback commented 2 years ago

I'd expect phpcov itself to load vendor/autoload.php (or it couldn't even find its own classes, right?), but you can explicitly include it like so: php -dauto_prepend_file=vendor/autoload.php vendor/bin/phpcov ....

b2p-fred commented 2 years ago

I will make a full new test of all this stuff starting from scratch. I containerized all my application and I hope it may help understanding what happened. I will keep you informed

b2p-fred commented 2 years ago

I will open another issue to explain my new test and all the results I got. I am closing this one.