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

PHPUnit\Framework\TestCase::__constructor marked as internal even though it's intended to be used #3975

Closed
dkarlovi opened this issue Dec 6, 2019 · 5 comments
Labels
type/bug Something is broken

Comments

@dkarlovi
Copy link

dkarlovi commented Dec 6, 2019

Q A
PHPUnit version 8.5.0
PHP version 7.3.12
Installation Method PHAR

Summary

Method PHPUnit\Framework\TestCase::__constructor is marked as internal with this message:

@internal This method is not covered by the backward compatibility promise for PHPUnit

Current behavior

Method is marked as internal, seemingly just to signal it not being covered by BC.

But it's not actually internal since it's intended to be used by the fast you extend TestCase in your own suites.

The usage of internal methods is reported by running Psalm on your tests:

ERROR: InternalMethod - tests/Xezilaires/Bridge/PhpSpreadsheet/RowIteratorTest.php:28:37 - The method PHPUnit\Framework\TestCase::__construct has been marked as internal
final class RowIteratorTest extends TestCase

How to reproduce

Run Psalm on your tests backed by PHPUnit 8.5.0.

Expected behavior

Method(s) which are intended to be used by the tests shouldn't be marked as internal.

@dkarlovi dkarlovi added the type/bug Something is broken label Dec 6, 2019
@dkarlovi
Copy link
Author

dkarlovi commented Dec 6, 2019

Just found another one:

ERROR: InternalMethod - tests/Xezilaires/SpreadsheetIteratorTest.php:123:30 - The method PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn has been marked as internal
                    $mocker->willReturn($spec['return']);

@sebastianbergmann
Copy link
Owner

I do not know where you get the idea that PHPUnit\Framework\TestCase::__constructor() is intended to be used in your own tests.

@dkarlovi
Copy link
Author

dkarlovi commented Dec 6, 2019

It's not used directly, but the ctor is invoked just by extending TestCase, no? So basically any test case out there is using an internal method by default.

@r-sw-eet
Copy link

Just as dkarlovi said, how am I supposed to extend the Testcase, if the extended class needs its own constructor (and thus parent::constructor(); is unavoidable)

@JohnRDOrazio
Copy link

JohnRDOrazio commented Apr 21, 2024

After upgrading PHPUnit from 9 to 10, I started getting errors about not having enough arguments for the constructor, when trying to extend TestCase and then instantiating my class. Only way I could get around the error was to explicitly declare a constructor, which I wasn't even using before:

class MyTest extends TestCase {

    public function __construct($name = '') {
        parent::__construct($name);
    }

    public function test() {
        //do a test
    }

}

Why can't the $name parameter just default to an empty or null value in TestCase like it used to?

Another way I get around this instead of declaring the constructor, is explicitly passing a $name parameter when instantiating, I don't quite understand how this is useful, it doesn't seem to be documented anywhere?

-$mytest = new MyTest;
+$mytest = new MyTest( 'MyTest' );

Of what use is the required $name parameter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

4 participants