Remove use of TestClass constructor

Created on 30 November 2023, 7 months ago
Updated 8 December 2023, 7 months ago

Problem/Motivation

PHPUnit changes the constructor for the TestCase. It actually changed several times. Its well accepted that logic should be in various setup methods instead of the constructor because its internal and the signature is subject to change but core instantiates TestCases's directly in some places which also breaks.

Steps to reproduce

Run tests in PHPUnit 10. See errors.
https://github.com/sebastianbergmann/phpunit/blob/main/src/Framework/Tes...

There was 1 PHPUnit error:

1) Drupal\Tests\Component\Utility\VariableTest::testCallableToString
* The data provider specified for Drupal\Tests\Component\Utility\VariableTest::testCallableToString is invalid
  Too few arguments to function PHPUnit\Framework\TestCase::__construct(), 0 passed in /app/core/tests/Drupal/Tests/Component/Utility/VariableTest.php on line 48 and exactly 1 expected
  /app/vendor/phpunit/phpunit/src/Metadata/Api/DataProvider.php:172
  /app/vendor/phpunit/phpunit/src/Metadata/Api/DataProvider.php:65
  /app/vendor/phpunit/phpunit/src/Framework/TestBuilder.php:39
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:477
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:125
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:215
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:244
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:261
  /app/vendor/phpunit/phpunit/src/TextUI/Configuration/Xml/TestSuiteMapper.php:100
  /app/vendor/phpunit/phpunit/src/TextUI/Configuration/TestSuiteBuilder.php:76
  /app/vendor/symfony/console/Command/Command.php:326
  /app/vendor/symfony/console/Application.php:1078
  /app/vendor/symfony/console/Application.php:324
  /app/vendor/symfony/console/Application.php:175
  /app/vendor/brianium/paratest/bin/paratest:37

* The data provider specified for Drupal\Tests\Component\Utility\VariableTest::testCallableToString is invalid
  Too few arguments to function PHPUnit\Framework\TestCase::__construct(), 0 passed in /app/core/tests/Drupal/Tests/Component/Utility/VariableTest.php on line 48 and exactly 1 expected
  /app/vendor/phpunit/phpunit/src/Metadata/Api/DataProvider.php:172
  /app/vendor/phpunit/phpunit/src/Metadata/Api/DataProvider.php:65
  /app/vendor/phpunit/phpunit/src/Framework/TestBuilder.php:39
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:477
  /app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:125

/app/core/tests/Drupal/Tests/Component/Utility/VariableTest.php:86

--

There were 5 errors:

...

3) Drupal\Tests\Core\Test\AssertContentTraitTest::testGetTextContent
ArgumentCountError: Too few arguments to function PHPUnit\Framework\TestCase::__construct(), 0 passed in /app/core/tests/Drupal/Tests/Core/Test/AssertContentTraitTest.php on line 18 and exactly 1 expected

/app/core/tests/Drupal/Tests/Core/Test/AssertContentTraitTest.php:18
/app/vendor/phpunit/phpunit/src/Framework/TestRunner.php:103
/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:340

...

Proposed resolution

Uses are generally weird mock/stub's relying on a "well known" class. They can easily be converted to other methods.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Fixed

Version

10.2

Component
PHPUnit 

Last updated about 22 hours ago

Created by

🇺🇸United States neclimdul Houston, TX

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @neclimdul
  • Status changed to Needs review 7 months ago
  • 🇺🇸United States neclimdul Houston, TX
  • 🇺🇸United States neclimdul Houston, TX
  • 🇺🇸United States neclimdul Houston, TX
  • Status changed to RTBC 7 months ago
  • 🇺🇸United States smustgrave

    Neat find.

    Not going to pretend to fully understand but the change of remvoing fake() makes sense said out loud. And all tests are passing (Not even a nightwatch failure)

  • Status changed to Needs review 7 months ago
  • 🇺🇸United States xjm

    Does PHPUnit 10 throw a specific exception/error/etc. when this kind of thing happens, so that we can ensure it won't creep back in? Or ideas on other ways to prevent future weirdness along these lines?

  • 🇺🇸United States neclimdul Houston, TX

    Its actually a PHP ArgumentCountError error because the require arguments for the constructor don't exist.

    I've updated the IS with a specific examples of the error that this causes.

    As far as preventing it that's a good question. In the short term, I'm not sure. But it looks like the plan in the parent issue is to support 2 versions and do upgrade testing like we did with previous PHPUnit upgrades. Once we get that PHPUnit 10 testing in place it would definitely start flagging weirdness like this. This issue was mostly part of getting obvious errors out of the way so the upgrade is a smaller easier to review task.

    Its possible this could be caught in static analysis though and we _could_ reach out to mglaman about implementing a rule in phpstan-drupal to catch it. That might help contrib see the error before trying to upgrade to PHPUnit 10. It might also be overkill though because its technically code outside of Drupal. *shrug*

  • Status changed to RTBC 7 months ago
  • 🇺🇸United States smustgrave

    Think that sounds like a plan

    Or is there a script we could run in gitlab? And make it part of the check temporarily?

  • 🇺🇸United States neclimdul Houston, TX

    The script would have to be pretty complicated because the ways this get triggered are complicated. Code could be directly interacting with TestClass like core/tests/Drupal/Tests/Core/Test/AssertContentTraitTest.php which would only require understanding use statements and namespaces or something more complicated like a child class in core/tests/Drupal/Tests/Component/Utility/VariableTest.php which would require understanding LSB, static class instantiation, and the entire php inheritance of every class. Both are pretty complicated and the latter is _very_ difficult. That's why I suggested if we wanted a test, a static analysis tool that understands the structure of php code like phpstan would be the place to put it..

  • 🇮🇹Italy mondrake 🇮🇹

    This seems to me such an edge case that I do not think it makes sense to overengineer a preventive check. If it fails, it will fail in PHPUnit 10 and people will fix the edge case.

    +1 on RTBC.

  • 🇺🇸United States xjm

    @mondrake My concern is that the tests might not fail on PHPUnit 10, and instead would silently behave in weird ways, which could lead to false positives or difficult-to-debug seemingly random failures or such.

    However, given the feedback above I don't think that needs to be a blocking concern. Perhaps we can create a followup for static analysis or such regarding it? Tagging for such.

  • 🇺🇸United States xjm
    • xjm committed 48b171f7 on 11.x
      Issue #3405360 by neclimdul, xjm: Remove use of TestClass constructor
      
    • xjm committed 038c0f8a on 10.2.x
      Issue #3405360 by neclimdul, xjm: Remove use of TestClass constructor
      
      (...
  • Status changed to Fixed 7 months ago
  • 🇺🇸United States xjm

    Committed to 11.x, and cherry-picked to 10.2.x as a patch-safe test improvement. Thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024