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

3.0: Move "is unit test" utility functions to dedicated trait #1960

Merged
merged 8 commits into from
Feb 20, 2021

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 20, 2020

This PR will be easiest to review by looking through the individual commits.

Related to #1877

Commit Details

βž• Sniff: remove the is_token_in_test_method() method πŸ†•

This method is currently unused in WPCS and it is not 100% clear what the method should do, so removing the method for now.

If needs be it can be brought back later if/when there is more clarity on what the method is supposed to solve.

Move "is this test code" related utilities to dedicated IsUnitTestTrait

The "is this test code" related utilities are only used by a small set of sniffs, so are better placed in a dedicated trait.

This moves the (public) $custom_test_class_whitelist and the $test_class_whitelist properties as well as the is_token_in_test_method() and the is_test_class method to a new WordPressCS\WordPress\Helpers\IsUnitTestTrait and starts using that trait in the relevant sniffs.

The trait has been made stand-alone, i.e. it does not presume that a sniff implementing it extends the WordPressCS\WordPress\Sniff class.
This makes it easier to (re-)use the traits, even when a sniff extends an abstract sniff class from PHPCS itself or from PHPCSUtils.

Note: the is_token_in_test_method() method is currently unused in WPCS, but seems useful enough to keep in place.

Related to #1465

IsUnitTestTrait::is_test_class(): use PHPCSUtils

This implements PHPCSUtils alternatives for PHPCS native methods and other PHPCSUtils utilities in the IsUnitTestTrait::is_test_class() code.

The functionality is unchanged (just less buggy).

βž– IsUnitTestTrait::is_token_in_test_method(): implement PHPCSUtils/improve handling nested functions

IsUnitTestTrait: rename protected property

The protected property $test_class_whitelist has been renamed to $known_test_classes as a step towards removing whitelist/blacklist terminology.

IsUnitTestTrait: rename public property

The public property $custom_test_class_whitelist has been renamed to $custom_test_classes as a step towards removing whitelist/blacklist terminology.

Includes adjusting the property name in all tests which included the property.

Related to #1915

To do before release:

  • Annotate this change in the wiki custom properties list

IsUnitTestTrait: rename local variable

The function local variable $whitelist has been renamed to $known_test_classes as a step towards removing whitelist/blacklist terminology.

IsUnitTestTrait: add missing WP Core test case classes

Add two more test cases which were added to /tests/phpunit/includes/.

Note: WP Core contains more test cases, but those are in the tests/phpunit/tests directory, while the test cases in /tests/phpunit/includes/ are supposed to be the "public test API", which is why those are the only ones in this list.

IsUnitTestTrait: improve documentation

@dingo-d
Copy link
Member

dingo-d commented Nov 20, 2020

For example: for a token in a function nested within a test method, the method would previously return true and strictly speaking this is correct when the question is "is the token in a test method ?".

However, this answer is incorrect if the question would be "is this a token which is unit test code ?" as the nested function would be in the global scope and not in the scope of the test class.

It seems to me that, in both cases, the current status of what this helper does is not truthful.

If we want to cover both cases, a new helper would need to be created which will account for the scope of the unit test, and a check should be added in the current helper that would exclude the case if it's in a unit case, but just cover the part where we check if the token is in a test method.

If I'm not mistaken, this could be useful if anonymous functions or classes are used in a method within a test class. Don't ask me how I know of this case, let's just say that integration tests in WordPress require some level of imagination πŸ˜‚

@jrfnl
Copy link
Member Author

jrfnl commented Nov 20, 2020

It seems to me that, in both cases, the current status of what this helper does is not truthful.

Well, as the method is not used in WPCS at this time, this discussion is theoretical.

Maybe we should just remove the method completely until such time that WPCS needs it again.

More than anything, it would be better for custom rulesets to exclude the test directories completely for these sniffs. This can't be done from within WPCS as we don't know what the name of the test directory is for individual projects.
These methods are only a guestimation to try and bow out before reporting.

@GaryJones
Copy link
Member

As this is going to be for a major release, I'm not against the behaviour here being clarified in a particular way, even if there is no extra helper method to help answer the other question.

@jrfnl
Copy link
Member Author

jrfnl commented Dec 4, 2020

@dingo-d @GaryJones What can I do to move this PR forward ? I'm happy to adjust it, but at this moment there doesn't seem to be consensus on if and how it should be adjusted. Would it be possible to continue the above discussion ?

@dingo-d
Copy link
Member

dingo-d commented Dec 4, 2020

I'm fine with removing it, especially since it's not being used at the moment. We can easily return it back (it's in the commits).

@GaryJones
Copy link
Member

I'm in agreement here.

@jrfnl jrfnl force-pushed the 3.0/new-unittesttrait branch from 546c018 to fe2bf3e Compare December 5, 2020 22:51
@jrfnl
Copy link
Member Author

jrfnl commented Dec 5, 2020

Ok, in that case, the method is hereby gone. I've added a new commit at the start of the stack and removed the "improve the method" commit. The changes have been annotated in the above PR description.

This method is currently unused in WPCS and it is not 100% clear what the method _should_ do, so removing the method for now.

If needs be it can be brought back later if/when there is more clarity on what the method is supposed to solve.
…ait`

The "is this test code" related utilities are only used by a small set of sniffs, so are better placed in a dedicated trait.

This moves the (`public`) `$custom_test_class_whitelist` and the `$custom_test_class_whitelist` properties as well as the `is_test_class` method to a new `WordPressCS\WordPress\Helpers\IsUnitTestTrait` and starts using that trait in the relevant sniffs.

The trait has been made stand-alone, i.e. it does not presume that a sniff implementing it extends the `WordPressCS\WordPress\Sniff` class.
This makes it easier to (re-)use the traits, even when a sniff extends an abstract sniff class from PHPCS itself or from PHPCSUtils.

Related to 1465
This implements PHPCSUtils alternatives for PHPCS native methods and other PHPCSUtils utilities in the `IsUnitTestTrait::is_test_class()` code.

The functionality is unchanged (just less buggy).
The protected property `$test_class_whitelist` has been renamed to `$known_test_classes` as a step towards removing whitelist/blacklist terminology.
The public property `$custom_test_class_whitelist` has been renamed to `$custom_test_classes` as a step towards removing whitelist/blacklist terminology.

Includes adjusting the property name in all tests which included the property.

To do before release:
* Annotate this change in the wiki custom properties list
The function local variable `$whitelist` has been renamed to `$known_test_classes` as a step towards removing whitelist/blacklist terminology.
Add two more test cases which were added to `/tests/phpunit/includes/`.

Note: WP Core contains more test cases, but those are in the `tests/phpunit/tests` directory, while the test cases in  `/tests/phpunit/includes/` are supposed to be the "public test API", which is why those are the only ones in this list.
@jrfnl jrfnl force-pushed the 3.0/new-unittesttrait branch from fe2bf3e to 0248285 Compare February 19, 2021 19:45
@jrfnl
Copy link
Member Author

jrfnl commented Feb 19, 2021

Rebased now the GH actions PR has been merged. Would appreciate a merge if the build passes,

@dingo-d dingo-d merged commit 8f39d40 into develop Feb 20, 2021
@dingo-d dingo-d deleted the 3.0/new-unittesttrait branch February 20, 2021 10:31
lesterchan added a commit to lesterchan/WordPress-Coding-Standards that referenced this pull request Dec 14, 2021
* upstream/develop: (106 commits)
  PHP/NoSilencedErrors: add `imagecreatefromwebp`
  PHP/NoSilencedErrors: add libxml_disable_entity_loader()
  GH Actions: turn display_errors on
  Bug report template: various improvements
  QA: import all used classes
  QA: use fully qualified global constant references
  QA: remove unnecessary assignment
  CS: get rid of "commented out code" warnings
  OneObjectStructurePerFile: move from `Extra` to `Core`
  GH Actions: don't use cs2pr in quicktest
  GH Actions: report CS violations in the PR
  GH Actions: get the tests running on PHP 8.1 (nightly)
  PHP 8.1 compatibility: fix deprecation notice [2]
  PHP 8.1 compatibility: fix deprecation notice [1]
  GH Actions: don't test against PHPCS 4.x (yet)
  Security/EscapeOutput: bug fix - allow for basename() being fully qualified
  3.0: Move "is unit test" utility functions to dedicated trait (WordPress#1960)
  Move from TravisCI to GitHub Actions (WordPress#1965)
  Travis: add build against PHP 8.0
  PHP/RestrictedPHPFunctions: update error message
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants