Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Coverage does not work with PHPUnit 8.5.24 PHAR on PHP 7 #4934

Closed
osma opened this issue Mar 16, 2022 · 6 comments
Closed

Code Coverage does not work with PHPUnit 8.5.24 PHAR on PHP 7 #4934

osma opened this issue Mar 16, 2022 · 6 comments
Assignees
Labels
feature/code-coverage Issues related to code coverage (but not php-code-coverage) installation/phar type/bug Something is broken

Comments

@osma
Copy link

osma commented Mar 16, 2022

Q A
PHPUnit version 8.5.24
PHP version 7.4.3
Installation Method PHAR

Summary

There appears to be a regression in PHPUnit 8.5.24 (compared to 8.5.23) when installed from PHAR. Enabling any coverage reports will cause all tests to fail. The error messages are related to php-token-stream; I suspect that something is wrong with the bundling of php-token-stream within the PHAR. I couldn't reproduce this when PHPUnit was installed via Composer.

Originally I found this problem when investigating why GitHub Actions CI tests for the Skosmos project started failing a few days ago, coinciding with the 8.5.24 release (see NatLibFi/Skosmos#1288 for background and details) but I was eventually able to reproduce the problem with a much more minimal test case that I'm reporting here.

Current behavior

Here is an example of broken tests:

$ php phpunit-8.5.24.phar --bootstrap src/autoload.php --coverage-text --whitelist src tests
PHPUnit 8.5.24 #StandWithUkraine

PHP Fatal error:  Uncaught Error: Class 'PHP_Token_OPEN_TAG' not found in phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-token-stream/Token/Stream.php:131
Stack trace:
#0 phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-token-stream/Token/Stream.php(73): PHPUnit\PHP_Token_Stream->scan()
#1 phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-code-coverage/CodeCoverage.php(515): PHPUnit\PHP_Token_Stream->__construct()
#2 phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-code-coverage/CodeCoverage.php(496): PHPUnit\SebastianBergmann\CodeCoverage\CodeCoverage->getLinesToBeIgnoredInner()
#3 phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-code-coverage/CodeCoverage.php(450): PHPUnit\SebastianBergmann\CodeCoverage\CodeCoverage->getLinesToBeIgnored()
#4 phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-code-coverage/CodeCoverage.php(254): PHPUnit\Sebasti in phar:///REDACTED/phpunit-coverage-debug/phpunit-8.5.24.phar/php-token-stream/Token/Stream.php on line 131

How to reproduce

Preconditions: PHP 7.x (seems to affect all of 7.2, 7.3 and 7.4), PHPUnit 8.5.24 installed from PHAR, and either XDebug or PCOV is installed so that coverage collection is possible.

Here is a very simple test project based on the PHPUnit 8 tutorial.

src/Email.php (unchanged from tutorial):

<?php declare(strict_types=1);
final class Email
{
    private $email;

    private function __construct(string $email)
    {
        $this->ensureIsValidEmail($email);

        $this->email = $email;
    }

    public static function fromString(string $email): self
    {
        return new self($email);
    }

    public function __toString(): string
    {
        return $this->email;
    }

    private function ensureIsValidEmail(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException(
                sprintf(
                    '"%s" is not a valid email address',
                    $email
                )
            );
        }
    }
}

tests/EmailTest.php (unchanged from tutorial):

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class EmailTest extends TestCase
{
    public function testCanBeCreatedFromValidEmailAddress(): void
    {
        $this->assertInstanceOf(
            Email::class,
            Email::fromString('user@example.com')
        );
    }

    public function testCannotBeCreatedFromInvalidEmailAddress(): void
    {
        $this->expectException(InvalidArgumentException::class);

        Email::fromString('invalid');
    }

    public function testCanBeUsedAsString(): void
    {
        $this->assertEquals(
            'user@example.com',
            Email::fromString('user@example.com')
        );
    }
}

src/autoload.php (hand-crafted):

<?php

require_once('Email.php');

Expected behavior

This is how it works with PHPUnit 8.5.23 and how I expect it should work also with the newest version:

$ php phpunit-8.5.23.phar --bootstrap src/autoload.php --coverage-text --whitelist src tests
PHPUnit 8.5.23 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 131 ms, Memory: 17.02 MB

OK (3 tests, 3 assertions)


Code Coverage Report:    
  2022-03-16 10:59:42    
                         
 Summary:                
  Classes: 100.00% (1/1) 
  Methods: 100.00% (4/4) 
  Lines:   91.67% (11/12)

Email
  Methods: 100.00% ( 4/ 4)   Lines: 100.00% ( 11/ 11)
@osma osma added the type/bug Something is broken label Mar 16, 2022
@sebastianbergmann sebastianbergmann self-assigned this Mar 16, 2022
@sebastianbergmann sebastianbergmann added feature/code-coverage Issues related to code coverage (but not php-code-coverage) installation/phar labels Mar 16, 2022
@sebastianbergmann
Copy link
Owner

Thank you for bringing this to my attention! This was missed in CI because we did not test the PHAR with code coverage. As of 995d322 we now do.

@sebastianbergmann
Copy link
Owner

This is (likely|almost certainly) related to the new version of PHP-Scoper being used. I will look into this ASAP.

@osma
Copy link
Author

osma commented Mar 16, 2022

Great, thanks for the quick response!

@sebastianbergmann sebastianbergmann changed the title Coverage on PHP 7.x broken in PHPUnit 8.5.24 PHAR install Code Coverage does not work with PHPUnit 8.5.24 PHAR on PHP 7 Mar 16, 2022
sebastianbergmann added a commit that referenced this issue Mar 16, 2022
@osma
Copy link
Author

osma commented Mar 17, 2022

Thanks for fixing this! Are you planning to do a 8.5.25 release soon-ish?

@sebastianbergmann
Copy link
Owner

PHPUnit 8.5.25 with this fix was released yesterday.

@osma
Copy link
Author

osma commented Mar 17, 2022

Oh, right. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/code-coverage Issues related to code coverage (but not php-code-coverage) installation/phar type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

2 participants