Code Coverage does not work with PHPUnit 8.5.24 PHAR on PHP 7
osma opened this issue ยท 6 comments
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)
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.
This is (likely|almost certainly) related to the new version of PHP-Scoper being used. I will look into this ASAP.
Great, thanks for the quick response!
Thanks for fixing this! Are you planning to do a 8.5.25 release soon-ish?
PHPUnit 8.5.25 with this fix was released yesterday.
Oh, right. Thanks a lot!