Skip to content

Add a unit test for the "ValidBlockLibraryFunctionNameSniff" sniff - #53928

Merged
anton-vlasenko merged 1 commit into
trunkfrom
fix/gutenberg-coding-standards-unit-tests
Sep 27, 2023
Merged

anton-vlasenko merged 1 commit into
trunkfrom
fix/gutenberg-coding-standards-unit-tests

Conversation

@anton-vlasenko

@anton-vlasenko anton-vlasenko commented Aug 24, 2023 •

Copy link
Copy Markdown
Contributor

What?

This PR aims to add a unit test for the recently introduced ValidBlockLibraryFunctionNameSniff sniff.
It also brings a few other minor improvements.
Fixes #53731.

Why?

It's considered best practice to write unit tests for PHPCS sniffs.
Additionally, as Gutenberg moves toward bumping the minimum supported PHP version, this package should also be updated.

How?

  • Add a unit test for the ValidBlockLibraryFunctionNameSniff sniff.
  • Bump the minimum required PHP version to 7.0.
  • Refine phpcs.xml.dist by only excluding necessary rules.
  • Update WPCS to 3.0.
  • Refactor ValidBlockLibraryFunctionNameSniff::processFunctionToken(): eliminate the need to loop through all available prefixes once the function name is confirmed as valid; this results in a minor performance improvement.
  • Remove dealerdirect/phpcodesniffer-composer-installer from the list of dependencies per WPCS 3.0 upgrade guide.

Testing Instructions

  1. Ensure that GitHub CI jobs pass.
  2. Navigate to test/php/gutenberg-coding-standards/.
  3. Run composer update.
  4. Run composer run check-all.

Ensure that the PHPUnit tests in the console pass. PHPUnit should report 0 assertions, but that is expected.

Note: The unit tests will not work on PHP 8.0 and above. This is because the latest stable version of the squizlabs/php_codesniffer package doesn't support PHPUnit 8.x. Also, PHPUnit 7.x is not compatible with PHP 8.0. The unit tests have been confirmed to work on PHP 7.3.

@anton-vlasenko anton-vlasenko added the [Type] Code Quality Issues or PRs that relate to code quality label Aug 24, 2023
@anton-vlasenko
anton-vlasenko force-pushed the fix/gutenberg-coding-standards-unit-tests branch from 58e0aca to 22e1b27 Compare August 24, 2023 21:01
@anton-vlasenko anton-vlasenko changed the title Improve unit tests for the gutenberg/gutenberg-coding-standards package Add a unit test for the ValidBlockLibraryFunctionNameSniff sniff Aug 24, 2023
@anton-vlasenko anton-vlasenko changed the title Add a unit test for the ValidBlockLibraryFunctionNameSniff sniff Add a unit test for the ValidBlockLibraryFunctionNameSniff sniff Aug 24, 2023
@anton-vlasenko anton-vlasenko changed the title Add a unit test for the ValidBlockLibraryFunctionNameSniff sniff Add a unit test for the "ValidBlockLibraryFunctionNameSniff" sniff Aug 24, 2023
@anton-vlasenko
anton-vlasenko marked this pull request as ready for review August 24, 2023 21:13
@ironprogrammer

ironprogrammer commented Sep 11, 2023 •

Copy link
Copy Markdown
Member

Thanks for this PR, @anton-vlasenko!

Test Report

Actual Results

  • ✅ PHPUnit tests pass when run in the terminal (composer run check-all under PHP 7.4.33).

Additional Scenarios

  • ✅ Linting PHP in the project shows that current block functions pass (npm run lint:php).
  • ✅ Adding an invalid function name to a block gets caught by the new sniff, Gutenberg.NamingConventions.ValidBlockLibraryFunctionName.FunctionNameInvalid (e.g. when adding register_block_core_imagetest() with missing underscore after prefix). See Figure 1.

Supplemental Artifacts

Figure 1:
Invalid function name sniff result example

@anton-vlasenko
anton-vlasenko force-pushed the fix/gutenberg-coding-standards-unit-tests branch from fc779b9 to 7c2ab3c Compare September 19, 2023 10:29
@github-actions

github-actions Bot commented Sep 19, 2023 •

Copy link
Copy Markdown

Flaky tests detected in 714e8a6.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6312515538
📝 Reported issues:

@anton-vlasenko
anton-vlasenko force-pushed the fix/gutenberg-coding-standards-unit-tests branch from c654129 to 1c0c248 Compare September 25, 2023 17:06
@anton-vlasenko
anton-vlasenko force-pushed the fix/gutenberg-coding-standards-unit-tests branch 2 times, most recently from 40f5dd2 to dc5936f Compare September 25, 2023 18:24
 2. Bump the minimum required PHP version to 7.0.
 3. Refine `phpcs.xml.dist` by only excluding necessary rules.
 4. Update WPCS to 3.0.
 5. Refactor `ValidBlockLibraryFunctionNameSniff::processFunctionToken()`: eliminate the need to loop through all available prefixes once the function name is confirmed as valid; this results in a minor performance improvement.
 6. Remove `dealerdirect/phpcodesniffer-composer-installer` from the list of dependencies per WPCS 3.0 [upgrade guide](https://github.com/WordPress/WordPress-Coding-Standards/wiki/Upgrade-Guide-to-WordPressCS-3.0.0-for-ruleset-maintainers).
@anton-vlasenko
anton-vlasenko force-pushed the fix/gutenberg-coding-standards-unit-tests branch from dc5936f to 714e8a6 Compare September 26, 2023 12:19

@ironprogrammer ironprogrammer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-ran tests after the recent syncs with trunk and it looks good, @anton-vlasenko -- I approve this PR.

@anton-vlasenko

Copy link
Copy Markdown
Contributor Author

Thank you, @ironprogrammer !

@anton-vlasenko
anton-vlasenko merged commit 4be4d55 into trunk Sep 27, 2023
@anton-vlasenko
anton-vlasenko deleted the fix/gutenberg-coding-standards-unit-tests branch September 27, 2023 10:12
@github-actions github-actions Bot added this to the Gutenberg 16.8 milestone Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor unit tests in the gutenberg/gutenberg-coding-standards package

2 participants