Skip to content

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 9, 2024

The PHPCS native Config class uses a number of static properties, which may be updated during tests. These were previously reset to their default values in the UtilityMethodTestCase::resetTestFile() method, but this reset was inadvertently removed in commit 4f0f9a4 with the reasoning that, if all tests use the ConfigDouble, the reset would no longer be needed as the ConfigDouble resets on being initialized.

The flaw in this logic is that not all tests are guaranteed to use the ConfigDouble, which means that without the reset, the Config class may be left "dirty" after tests using the ConfigDouble, which could break tests.

This commit fixes this issue by:

  • Adding a __destruct() method to the ConfigDouble class which will reset the static properties on the PHPCS native Config class whenever an object created from this class is destroyed.
  • Explicitly calling the __destruct() method from the UtilityMethodTestCase::resetTestFile() method to ensure it is always run after a test has finished ("after class"), even if there would still be a lingering reference to the object.

Includes tests for the new __destruct() method.
Includes an additional test for the UtilityMethodTestCase class to ensure this snafu cannot come back without tests failing on it.

Props to @fredden for helping me debug this issue.

The PHPCS native `Config` class uses a number of static properties, which may be updated during tests. These were previously reset to their default values in the `UtilityMethodTestCase::resetTestFile()` method, but this reset was inadvertently removed in commit 4f0f9a4 with the reasoning that, if all tests use the `ConfigDouble`, the reset would no longer be needed as the `ConfigDouble` resets on being initialized.

The flaw in this logic is that not all tests are guaranteed to use the `ConfigDouble`, which means that without the reset, the `Config` class may be left "dirty" after tests using the `ConfigDouble`, which could break tests.

This commit fixes this issue by:
* Adding a `__destruct()` method to the `ConfigDouble` class which will reset the static properties on the PHPCS native `Config` class whenever an object created from this class is destroyed.
* Explicitly calling the `__destruct()` method from the `UtilityMethodTestCase::resetTestFile()` method to ensure it is always run after a test has finished ("after class"), even if there would still be a lingering reference to the object.

Includes tests for the new `__destruct()` method.
Includes an additional test for the `UtilityMethodTestCase` class to ensure this snafu cannot come back without tests failing on it.
@jrfnl jrfnl force-pushed the feature/utilitymethodtestcase-fix-reset branch from 499e04a to acd940a Compare June 9, 2024 12:33
@jrfnl jrfnl merged commit 2616376 into develop Jul 27, 2024
@jrfnl jrfnl deleted the feature/utilitymethodtestcase-fix-reset branch July 27, 2024 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

1 participant