matthiasnoback / live-code-coverage

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

No code coverage whereas it should have some #21

Open b2p-fred opened 2 years ago

b2p-fred commented 2 years ago

In a fresh Docker container (alpine linux php-fpm 8.0) I executed the following commands :

# Install required packages
$ composer require --dev matthiasnoback/live-code-coverage
$ composer require --dev phpunit/phpcov

# Modify the main controller (see later)

# Run the behat tests
$ ./vendor/bin/behat features/core/Anonymous.feature

# List the coverage files
$ ls -al artifacts/live-coverage/
total 172
drwxrwxrwx 2 me          10000 20480 Feb 16 13:14 .
drwxr-xr-x 5 me          10000  4096 Feb 16 13:02 ..
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c22603b4.02206227.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c2308d66.84608413.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c23ad1d7.38233268.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c23f6135.11838277.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c2442986.25039401.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c248c4e4.49035877.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c251fe25.43826291.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c256b545.73725875.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c2607088.83906599.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c2655775.81697227.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c269e893.35438473.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c2732671.95159334.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c278d602.21566494.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c27edf26.20185227.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c283a2b7.53465292.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c28d9b31.25165564.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c291d979.48545702.cov
-rw-rw-rw- 1 www-data www-data  5605 Feb 16 13:14 20220216131442620cf8c29b95b1.54902986.cov

# Merge the coverage results
$ ./vendor/bin/phpcov merge --text coverage.txt ./artifacts/live-coverage/
phpcov 8.2.0 by Sebastian Bergmann.

Generating code coverage report in text format ... done

# View the coverage results
$ cat coverage.txt 

Code Coverage Report:     
  2022-02-16 13:15:40     

 Summary:                 
  Classes:  0.00% (0/54)  
  Methods:  0.00% (0/419) 
  Lines:    0.00% (0/1515)

With this procedure the tests results show no coverage at all !

Note that the problem that I encountered in #20 disappeared!

The application main controller:

<?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;
};

One of the .cov produced files (I do not know if it may help?) :

<?php

return \unserialize(<<<'END_OF_COVERAGE_SERIALIZATION' O:43:"SebastianBergmann\CodeCoverage\CodeCoverage":15:{s:51:" SebastianBergmann\CodeCoverage\CodeCoverage driver";O:48:"SebastianBergmann\CodeCoverage\Driver\PcovDriver":3:{s:56:" SebastianBergmann\CodeCoverage\Driver\PcovDriver filter";O:37:"SebastianBergmann\CodeCoverage\Filter":2:{s:44:" SebastianBergmann\CodeCoverage\Filter files";a:63:{s:40:"/var/www/src/Action/GetVersionAction.php";b:1;s:50:"/var/www/src/Command/DriverHistoryPurgeCommand.php";b:1;s:43:"/var/www/src/Command/UsersCreateCommand.php";b:1;s:43:"/var/www/src/Command/UsersDeleteCommand.php";b:1;s:41:"/var/www/src/Command/UsersListCommand.php";b:1;s:46:"/var/www/src/Controller/DocumentController.php";b:1;s:44:"/var/www/src/Controller/DriverController.php";b:1;s:42:"/var/www/src/Controller/UserController.php";b:1;s:40:"/var/www/src/DBAL/Types/BaseEnumType.php";b:1;s:39:"/var/www/src/DBAL/Types/ContactType.php";b:1;s:46:"/var/www/src/DBAL/Types/DocumentStatusType.php";b:1;s:40:"/var/www/src/DBAL/Types/DocumentType.php";b:1;s:43:"/var/www/src/DBAL/Types/HumanGenderType.php";b:1;s:40:"/var/www/src/DBAL/Types/LanguageType.php";b:1;s:44:"/var/www/src/DBAL/Types/NotificationType.php";b:1;s:41:"/var/www/src/DataFixtures/AppFixtures.php";b:1;s:46:"/var/www/src/Doctrine/ContactPhoneListener.php";b:1;s:47:"/var/www/src/Doctrine/DocumentAnnexListener.php";b:1;s:50:"/var/www/src/Doctrine/DocumentReceivedListener.php";b:1;s:48:"/var/www/src/Doctrine/DocumentStatusListener.php";b:1;s:45:"/var/www/src/Doctrine/DriverPhoneListener.php";b:1;s:46:"/var/www/src/Doctrine/HashPasswordListener.php";b:1;s:31:"/var/www/src/Entity/Address.php";b:1;s:31:"/var/www/src/Entity/Contact.php";b:1;s:32:"/var/www/src/Entity/Customer.php";b:1;s:32:"/var/www/src/Entity/Document.php";b:1;s:39:"/var/www/src/Entity/DocumentHistory.php";b:1;s:39:"/var/www/src/Entity/DocumentVersion.php";b:1;s:30:"/var/www/src/Entity/Driver.php";b:1;s:37:"/var/www/src/Entity/DriverHistory.php";b:1;s:36:"/var/www/src/Entity/Notification.php";b:1;s:28:"/var/www/src/Entity/Site.php";b:1;s:27:"/var/www/src/Entity/Tag.php";b:1;s:49:"/var/www/src/Entity/Traits/SenderReceiverName.php";b:1;s:28:"/var/www/src/Entity/User.php";b:1;s:45:"/var/www/src/EventListener/KernelListener.php";b:1;s:44:"/var/www/src/Filter/DocumentCustomFilter.php";b:1;s:51:"/var/www/src/Filter/DocumentHistoryCustomFilter.php";b:1;s:40:"/var/www/src/Filter/SiteCustomFilter.php";b:1;s:34:"/var/www/src/Filter/UuidFilter.php";b:1;s:43:"/var/www/src/Handler/UserMessageHandler.php";b:1;s:23:"/var/www/src/Kernel.php";b:1;s:46:"/var/www/src/Manager/ApiInformationManager.php";b:1;s:36:"/var/www/src/Message/UserMessage.php";b:1;s:37:"/var/www/src/OpenApi/JwtDecorator.php";b:1;s:41:"/var/www/src/OpenApi/VersionDecorator.php";b:1;s:45:"/var/www/src/Repository/AddressRepository.php";b:1;s:45:"/var/www/src/Repository/ContactRepository.php";b:1;s:46:"/var/www/src/Repository/CustomerRepository.php";b:1;s:53:"/var/www/src/Repository/DocumentHistoryRepository.php";b:1;s:46:"/var/www/src/Repository/DocumentRepository.php";b:1;s:53:"/var/www/src/Repository/DocumentVersionRepository.php";b:1;s:51:"/var/www/src/Repository/DriverHistoryRepository.php";b:1;s:44:"/var/www/src/Repository/DriverRepository.php";b:1;s:50:"/var/www/src/Repository/NotificationRepository.php";b:1;s:42:"/var/www/src/Repository/SiteRepository.php";b:1;s:41:"/var/www/src/Repository/TagRepository.php";b:1;s:42:"/var/www/src/Repository/UserRepository.php";b:1;s:45:"/var/www/src/Security/ApiKeyAuthenticator.php";b:1;s:47:"/var/www/src/Security/ApiLoginAuthenticator.php";b:1;s:35:"/var/www/src/Security/OAuthUser.php";b:1;s:42:"/var/www/src/Service/FileUploadService.php";b:1;s:35:"/var/www/src/Utils/HydrateTrait.php";b:1;}s:50:" SebastianBergmann\CodeCoverage\Filter isFileCache";a:0:{}}s:74:" SebastianBergmann\CodeCoverage\Driver\Driver collectBranchAndPathCoverage";b:0;s:60:" SebastianBergmann\CodeCoverage\Driver\Driver detectDeadCode";b:0;}s:51:" SebastianBergmann\CodeCoverage\CodeCoverage filter";r:3;s:51:" SebastianBergmann\CodeCoverage\CodeCoverage wizard";O:46:"SebastianBergmann\CodeUnitReverseLookup\Wizard":3:{s:59:" SebastianBergmann\CodeUnitReverseLookup\Wizard lookupTable";a:0:{}s:64:" SebastianBergmann\CodeUnitReverseLookup\Wizard processedClasses";a:0:{}s:66:" SebastianBergmann\CodeUnitReverseLookup\Wizard processedFunctions";a:0:{}}s:79:" SebastianBergmann\CodeCoverage\CodeCoverage checkForUnintentionallyCoveredCode";b:0;s:66:" SebastianBergmann\CodeCoverage\CodeCoverage includeUncoveredFiles";b:0;s:66:" SebastianBergmann\CodeCoverage\CodeCoverage processUncoveredFiles";b:1;s:65:" SebastianBergmann\CodeCoverage\CodeCoverage ignoreDeprecatedCode";b:0;s:54:" SebastianBergmann\CodeCoverage\CodeCoverage currentId";N;s:49:" SebastianBergmann\CodeCoverage\CodeCoverage data";O:56:"SebastianBergmann\CodeCoverage\ProcessedCodeCoverageData":2:{s:70:" SebastianBergmann\CodeCoverage\ProcessedCodeCoverageData lineCoverage";a:0:{}s:74:" SebastianBergmann\CodeCoverage\ProcessedCodeCoverageData functionCoverage";a:0:{}}s:74:" SebastianBergmann\CodeCoverage\CodeCoverage useAnnotationsForIgnoringCode";b:1;s:50:" SebastianBergmann\CodeCoverage\CodeCoverage tests";a:0:{}s:101:" SebastianBergmann\CodeCoverage\CodeCoverage parentClassesExcludedFromUnintentionallyCoveredCodeCheck";a:0:{}s:64:" SebastianBergmann\CodeCoverage\CodeCoverage coveredFileAnalyser";N;s:66:" SebastianBergmann\CodeCoverage\CodeCoverage uncoveredFileAnalyser";N;s:59:" SebastianBergmann\CodeCoverage\CodeCoverage cacheDirectory";N;} END_OF_COVERAGE_SERIALIZATION );

matthiasnoback commented 2 years ago

Could it be that phpunit.xml contains invalid configuration for what to include in/exclude from code coverage?

b2p-fred commented 2 years ago

Here is my phpunit.xml.dist file :

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
         convertDeprecationsToExceptions="false"
>
    <php>
        <ini name="display_errors" value="1" />
        <ini name="error_reporting" value="-1" />
        <env name="APP_ENV" value="test" force="true" />
        <server name="SHELL_VERBOSITY" value="-1" />
        <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
        <server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
        <server name="KERNEL_CLASS" value="App\Kernel" />
        <!-- Set this variable to avoid many deprecation warning when running the tests:
        - weak dumps the warning count
        - disabled removes all the messages
        Run `SYMFONY_DEPRECATIONS_HELPER= ./vendor/bin/phpunit`
        -->
        <server name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
    </php>

    <testsuites>
        <testsuite name="Utils tests">
            <directory>tests/Utils</directory>
        </testsuite>
        <testsuite name="Unit tests">
            <directory>tests/Unit</directory>
        </testsuite>
        <testsuite name="API tests">
            <directory>tests/Api</directory>
        </testsuite>
    </testsuites>

    <coverage includeUncoveredFiles="false" processUncoveredFiles="true">
        <include>
            <directory suffix=".php">src</directory>
        </include>
        <report>
            <text outputFile="artifacts/phpunit/coverage.txt" showUncoveredFiles="true" showOnlySummary="true"/>
        </report>
    </coverage>

    <listeners>
        <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
    </listeners>
</phpunit>

When I run my unit tests with phpunit, I get correct code coverage. Do you have some specific requirements for as phpunit configuration. I did not see any except having correct inclusions ...

matthiasnoback commented 2 years ago

Sorry, I have no idea what may be wrong here. I'd recommend step-debugging. Maybe something has changed here: https://github.com/matthiasnoback/live-code-coverage/blob/master/src/LiveCodeCoverage/CodeCoverageFactory.php This replicates part of PHPUnit itself, but maybe PHPUnit has diverged from this code.

AipNooBest commented 1 year ago

I ran into the same problem, but I was able to solve it by adding the <filter> and <whitelist> tags to the phpunit.xml.dist file. I guess that the coverage whitelist filter is empty by default which means that none of the files would be covered. Here's my phpunit.xml.dist file for reference in case someone else will encounter the same issue.

<?xml version="1.0" encoding="utf-8"?>
<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
    bootstrap="vendor/autoload.php"
    colors="true"
>
    <coverage>
        <include>
                <directory suffix=".php">source</directory>
        </include>
    </coverage>
    <php>
        <env name="XDEBUG_MODE" value="coverage"/>
    </php>
    <filter>
        <whitelist>
                <directory suffix=".php">source</directory>
        </whitelist>
    </filter>
</phpunit>