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

YoastCS: add some test specific rules and exceptions #367

Merged
merged 5 commits into from
Dec 14, 2023

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Dec 14, 2023

YoastCS rules: selectively exempt test files [1]

While WordPressCS makes a best effort to try to prevent false positives on test code for the WordPress.WP.GlobalVariablesOverride, it is simpler to just plain exclude the test code from being scanned with this sniff.

YoastCS rules: selectively exempt test files [2]

For mock-based unit tests, it is fine to use the PHP native json_encode() function to mock the WP/Yoast native alternative for this function, so let's prevent the test code needing lots of ignore annotations when the issue is not solvable.

YoastCS rules: selectively exempt test files [3]

Double classes may overload methods from a parent class just to change the visibility of the method from protected to public to allow for testing the method directly. This is fine.

YoastCS rules: enforce namespaces for all test files

Related to #303

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills --
WP Test Utils --
YoastCS --
WHIP 9
Yoast Test Helper --
Duplicate Post --
Yst ACF --
Yst WooCommerce 5
Yst News 9
Yst Local 1
Yst Video 108
Yst Premium 76
Yst Free 118

YoastCS rules: enforce final classes for all test files

... while allowing abstract TestCases.

Note: the typical/default "Doubles" directories within the "tests" directory are exempt from this rule as "Double" classes are often still combined with mocking and making the class final would break those tests.

Related to #303

Impact on Yoast packages:

Plugin/Tool Errors/Warnings
PHPUnit Polyfills 25 (fixtures for the tests, these will be exempt)
WP Test Utils 9 (mostly fixtures for the tests)
YoastCS --
WHIP 9
Yoast Test Helper --
Duplicate Post 25
Yst ACF 10
Yst WooCommerce 25
Yst News 13
Yst Local 11
Yst Video 101
Yst Premium 126
Yst Free 647

While WordPressCS makes a best effort to try to prevent false positives on test code for the `WordPress.WP.GlobalVariablesOverride`, it is simpler to just plain exclude the test code from being scanned with this sniff.
For mock-based unit tests, it is fine to use the PHP native `json_encode()` function to mock the WP/Yoast native alternative for this function, so let's prevent the test code needing lots of ignore annotations when the issue is not solvable.
Double classes may overload methods from a parent class just to change the visibility of the method from `protected` to `public` to allow for testing the method directly. This is fine.
Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | --
| WP Test Utils     | --
| YoastCS           | --
| WHIP              | 9
| Yoast Test Helper | --
| Duplicate Post    | --
| Yst ACF           | --
| Yst WooCommerce   | 5
| Yst News          | 9
| Yst Local         | 1
| Yst Video         | 108
| Yst Premium       | 76
| Yst Free          | 118
... while allowing `abstract` TestCases.

Note: the typical/default "Doubles" directories within the "tests" directory are exempt from this rule as "Double" classes are often still combined with mocking and making the class `final` would break those tests.

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | 25 (fixtures for the tests, these will be exempt)
| WP Test Utils     | 9 (mostly fixtures for the tests)
| YoastCS           | --
| WHIP              | 9
| Yoast Test Helper | --
| Duplicate Post    | 25
| Yst ACF           | 10
| Yst WooCommerce   | 25
| Yst News          | 13
| Yst Local         | 11
| Yst Video         | 101
| Yst Premium       | 126
| Yst Free          | 647
@coveralls
Copy link

Coverage Status

coverage: 99.411%. remained the same
when pulling 80662b1 on JRF/yoastcs-test-specific-rules-and-exceptions
into b92c396 on develop.

@jrfnl jrfnl merged commit a9f5054 into develop Dec 14, 2023
28 checks passed
@jrfnl jrfnl deleted the JRF/yoastcs-test-specific-rules-and-exceptions branch December 14, 2023 11:28
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.

None yet

2 participants