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

@covers ::<!public> annotation broken #4478

Closed
cs278 opened this issue Oct 2, 2020 · 2 comments
Closed

@covers ::<!public> annotation broken #4478

cs278 opened this issue Oct 2, 2020 · 2 comments
Labels
feature/code-coverage Issues related to code coverage (but not php-code-coverage) type/bug Something is broken

Comments

@cs278
Copy link

cs278 commented Oct 2, 2020

Q A
PHPUnit version 9.4.0 (since 9.1.0)
PHP version 7.4.10
Installation Method Both

Summary

Using the @covers ::<!public> annotation on an exception causes a PHP fatal error. Presumably other variations of this annotation are broken as well.

Current behavior

$ php -f phpunit-9.4.phar -- --coverage-filter myexceptiontest.php --coverage-text myexceptiontest.php  
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.

PHP Fatal error:  Uncaught TypeError: Argument 2 passed to PHPUnit\SebastianBergmann\CodeUnit\CodeUnit::__construct() must be of the type string, bool given, called in phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/sebastian-code-unit/CodeUnit.php on line 59 and defined in phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/sebastian-code-unit/CodeUnit.php:126
Stack trace:
#0 phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/sebastian-code-unit/CodeUnit.php(59): PHPUnit\SebastianBergmann\CodeUnit\CodeUnit->__construct()
#1 phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/sebastian-code-unit/Mapper.php(180): PHPUnit\SebastianBergmann\CodeUnit\CodeUnit::forClassMethod()
#2 phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/sebastian-code-unit/Mapper.php(160): PHPUnit\SebastianBergmann\CodeUnit\Mapper->methodsOfClass()
#3 phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/sebastian-code-unit/Mapper.php(69): PHPUnit\SebastianBergmann\CodeUnit\Mapper->protectedAndPrivateMethodsOfClass()
#4 phar:/ in phar:///tmp/chris.smith/tmp.YrkElvgwGm/phpunit-9.4.phar/phpunit/TextUI/Command.php on line 100

How to reproduce

Trying to test the following code with coverage produces the error.

<?php

/**
 * @covers myexception::__construct
 * @covers myexception::<!public>
 */
final class myexceptiontest extends \PHPUnit\Framework\TestCase
{
    public function test()
    {
        $e = new myexception();
        self::assertSame('Hi', $e->getMessage());
    }
}

class myexception extends \exception
{
    public function __construct(\Throwable $previous = null)
    {
        parent::__construct('Hi', 0, $previous);
    }
}

Run PHPUnit with these arguments:

phpunit --coverage-filter myexceptiontest.php --coverage-text myexceptiontest.php  

It appears PHPUnit is trying to find the source file of the final private method \Exception::__clone, the correct behaviour I believe is to ignore parent methods.

Expected behavior

9.0 exhibits the expected behaviour.

$ php -f phpunit-9.0.phar -- --whitelist myexceptiontest.php --coverage-text myexceptiontest.php  
PHPUnit 9.0.2 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 185 ms, Memory: 14.00 MB

OK (1 test, 1 assertion)


Code Coverage Report:  
  2020-10-02 13:09:54  
                       
 Summary:              
  Classes: 50.00% (1/2)
  Methods: 50.00% (1/2)
  Lines:   40.00% (2/5)

myexception
  Methods: 100.00% ( 1/ 1)   Lines: 100.00% (  2/  2)
@cs278 cs278 added the type/bug Something is broken label Oct 2, 2020
@sebastianbergmann
Copy link
Owner

Sorry, but I will not invest any time in attempting to debug or fix this. This functionality is deprecated and scheduled for removal. I will, of course, consider a pull request that fixes this.

@sebastianbergmann sebastianbergmann added the feature/code-coverage Issues related to code coverage (but not php-code-coverage) label Oct 2, 2020
@cs278
Copy link
Author

cs278 commented Oct 2, 2020

I've submitted a PR to fix this, I've assumed you'll move this issue to that repository.

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) type/bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants