Opened 4 weeks ago
Last modified 3 weeks ago
#65208 new task (blessed)
Introduce namespacing for the test classes for 7.1
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | 7.1 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Build/Test Tools | Keywords: | has-patch has-unit-tests |
| Focuses: | tests | Cc: |
Description (last modified by )
This ticket is for continuing to introduce namespacing for test classes during the 7.1 release cycle.
Previous releases:
- 7.0: #53010
Overview copied directly from #53010 (please read the discussion on that ticket):
Introducing namespaces in the production code for WordPress Core is a hot topic, so I very purposely do NOT want to touch that in this ticket.
However, for the test suite, which doesn't get shipped with the WordPress production code, it's a whole other matter.
Benefits
Using namespaces in the test suite provides us with the following benefits:
- If used consistently and providing they follow a set pattern (more about this below), they will make it very easy for contributors to find the test files/classes they are looking for.
- It will allow for shorter file and test class names, while those will still be descriptive.
- And... it will allow for mocking PHP native functions by declaring a namespaced version of that same function in the test class.
- It will also allow more easily for multiple test classes to be created to test one particular feature/function, which the current naming scheme does not allow for. This will allow for tests for the same functionality, but which need different fixtures (setup/teardown) to be encapsulated in their own test classes.
Caveats:
As the WordPress Core test suite is used not only by Core, but also by plugins and themes for integration tests, the test class namespacing should be reserved for actual test classes and - for now - not be applied to test utility classes / Abstract base test classes (i.e. the
tests/phpunit/includesdirectory should NOT be touched for now).
Proposed pattern
The current directory structure for tests is, to put it mildly, confusing and inconsistent.
To solve that, I would like to propose the following pattern:
- File paths:
tests/phpunit/tests/wp-[includes|admin]/[SubFolder/]*Class_Under_Test/FunctionUnderTest[OptionalSubsetIndicator]Test.php- Namespace:
WordPress\Tests\WP_[Includes|Admin]\[SubFolder\]*Class_Under_Test- Classname:
FunctionUnderTest[OptionalSubsetIndicator]TestFor WP Core files which only contain functions outside of a class structure, the following pattern is proposed:
- File paths:
tests/phpunit/tests/wp-[includes|admin]/[SubFolder/]*Functions_FileName/FunctionUnderTest[OptionalSubsetIndicator]Test.php- Namespace:
WordPress\Tests\WP_[Includes|Admin]\[SubFolder\]*Functions_FileName- Classname:
FunctionUnderTest[OptionalSubsetIndicator]TestThe pattern I'm proposing does imply a re-organisation of the test suite directory and file structure, but that IMO is for the better.
It also follows a PSR4-like pattern which will be more intuitive for new contributors to work with, as well as follow the PHPUnit recommended test class name pattern with having the
Testas the end of the class name.
This will also allow for using PSR-4 autoloading for the tests classes and adding the
autoload-devdirective to thecomposer.jsonfile.
Planning
This should be regarded as a small project and not all renaming needs to be done at the same time.
New tests should start following the above proposed pattern as soon as consensus has been reached about this proposal.
Existing tests can be gradually switched over to the new pattern over time.
Additional tasks associated with this project
- [ ] Updating the contributors handbook for Core.
- [ ] Verify that the WordPressCS sniffs will validate this pattern correctly.
- [ ] Write a Make post about the decision once consensus has been reached.
Change History (24)
This ticket was mentioned in PR #6235 on WordPress/wordpress-develop by @jrf.
4 weeks ago
#1
- Keywords has-patch has-unit-tests added
This ticket was mentioned in PR #6272 on WordPress/wordpress-develop by @afercia.
4 weeks ago
#2
## Tests/admin/wpPluginInstallListTable: rename the original test class to be more descriptive and adjust covers notaiton.
This will allow the test class to be compatible with PHPUnit 11.
Includes removing method level @covers tags in favour of class level @covers tags to allow for supporting PHPUnit 11 attributes instead of annotations.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6273 on WordPress/wordpress-develop by @afercia.
4 weeks ago
#3
## Tests/admin/wpPluginsListTable: split the original test class and adjust covers notaitons.
This will allow the test class to be compatible with PHPUnit 11.
Includes removing method level https://github.com/Covers tags in favour of class level https://github.com/Covers tags to allow for supporting PHPUnit 11 attributes instead of annotations.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6274 on WordPress/wordpress-develop by @afercia.
4 weeks ago
#4
## Tests/admin/ExportWp: rename the original test class.
This will allow the test class to be compatible with PHPUnit 11.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6276 on WordPress/wordpress-develop by @poena.
4 weeks ago
#5
Rename the test class Tests_Admin_wpPostCommentsListTable to Admin_WpPostCommentsListTable_GetViews_Test so that the name includes both the class and the method that the test covers.
Moves @covers to the class level.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6277 on WordPress/wordpress-develop by @poena.
4 weeks ago
#6
Tests/admin/wpMediaListTable:
The tests for WP_Media_List_Table use the same set up, tear down and helper methods. By moving them to a new abstract TestCase class, Admin_WpMediaListTable_TestCase, they can be reused by multiple tests.
The test for prepare_items is moved to a new test class; Admin_WpMediaListTable_PrepareItems_Test and the @covers is moved to the class level.
This will allow the test class to be compatible with PHPUnit 11 using the new Admin_WpListMediaTable_TestCase class.
Tests_Admin_wpMediaListTable is renamed to Admin_WpMediaListTable_GetRowActions_Test to reflect that the remaining test is for _get_row_actions. The @covers is moved to the class level.
Trac ticket:
https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6296 on WordPress/wordpress-develop by @poena.
4 weeks ago
#7
…ive and move @covers
Renames IncludesComment to include the path, file name, and function name: Admin_Includes_Comment_CommentExists_Test
Removes duplicate @covers and moves @covers to the test class level.
Tests/Admin/IncludesComment: Rename the test file to be more descriptive and move @covers.
Renames IncludesComment to include the path, file name, and function name: Admin_Includes_Comment_CommentExists_Test
Removes duplicate @covers and moves @covers to the test class level.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6297 on WordPress/wordpress-develop by @poena.
4 weeks ago
#8
Test/Admin/IncludesFile: Split the test class into one file and test class for get_home_path and one for download_url.
The test for get_home_path is moved to Admin_Includes_File_GetHomePath_Test.
The file- and class name for the tests for download_url is renamed from IncludesFile to Admin_Includes_File_DownloadUrl_Test.
Duplicate the @covers tags are removed.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6384 on WordPress/wordpress-develop by @SergeyBiryukov.
4 weeks ago
#9
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6447 on WordPress/wordpress-develop by @poena.
4 weeks ago
#10
Move the setup functions from Tests_Admin_WpAutomaticUpdater to a new abstract TestCase class, so that they can be used by multiple tests for the WpAutomaticUpdater class.
Splits the existing test class Tests_Admin_WpAutomaticUpdater into three classes. This will allow these test classes to be compatible with PHPUnit 11.
class Tests_Admin_WpAutomaticUpdateris renamed toAdmin_WpAutomaticUpdater_SendPluginThemeEmail_Testand coversWP_Automatic_Updater::send_plugin_theme_email
- Tests that covers
WP_Automatic_Updater::is_allowed_dirare moved to a new test class:Admin_WpAutomaticUpdater_IsAllowedDir_Test
- The test that covers
WP_Automatic_Updater::is_vcs_checkoutis moved to a new test class:Admin_WpAutomaticUpdater_IsVcsCheckout_Test
- Duplicate
@coversare removed, the remaining@coversare moved to the test class level.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6481 on WordPress/wordpress-develop by @poena.
4 weeks ago
#11
This PR splits the existing test class wpPrivacyRequestsTable into two, and creates a new TestCase.
The purpose is to make the test classes compatible with PHPUnit 11.
Move tear down and helper functions from wpPrivacyRequestsTable to a new abstract TestCase so that they can be reused in multiple tests.
Split off the test for WP_Privacy_Requests_Table::get_views into a separate test class,
Admin_WpPrivacyRequestsTable_GetViews_Test.
Correct the covers for `WP_Privacy_Requests_Table::get_views:
- Previously
@covers WP_Privacy_Requests_List_Table::get_views. - Updated
@covers WP_Privacy_Requests_Table::get_views.
The previous was possibly a typo.
Rename the remaining test Tests_Admin_wpPrivacyRequestsTable and test file to Admin_WpPrivacyRequestsTable_PrepareItems_Test, to clarify which method that is being tested. Move the covers to the class level.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6483 on WordPress/wordpress-develop by @poena.
4 weeks ago
#12
Rename the test class Tests_Admin_wpUsersListTable and the file to Admin_WpUsersListTable_GetViews_Test to clarify which method is being tested.
Moves the @covers to the class level.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #6644 on WordPress/wordpress-develop by @poena.
4 weeks ago
#13
Trac ticket:
https://core.trac.wordpress.org/ticket/65208
The test class Tests_Admin_WpUpgrader (in tests/phpunit/tests/admin/wpUpgrader.php) contained tests for several methods.
This PR splits the test class into a new test class for each method, to indicate which methods are being tested, and to improve compatibility with PHPUnit 11.
Each of the split test classes has one covers annotation at the class level.
A new abstract TestCase is created, so that the set up and tear down functionality for WP_Upgrader can be reused in multiple tests. The name of the test case is Admin_WpUpgrader_TestCase
This ticket was mentioned in PR #6679 on WordPress/wordpress-develop by @afercia.
4 weeks ago
#14
…ss to be more descriptive and adjust covers annotations.
Trac ticket: https://core.trac.wordpress.org/ticket/65208
This ticket was mentioned in PR #7424 on WordPress/wordpress-develop by @pbearne.
4 weeks ago
#15
Created new PHPUnit test classes for get_avatar, get_avatar_data, and get_avatar_url functions. These changes improve test organization and maintainability by grouping related tests together. Also, renamed avatar.php to Functions_GetAvatarUrl_Test.php to better reflect its content.
@SergeyBiryukov commented on PR #6296:
4 weeks ago
#18
Thanks for the PR! Merged in r62328.
This ticket was mentioned in PR #11795 on WordPress/wordpress-develop by @abcd95.
4 weeks ago
#21
- Keywords has-patch has-unit-tests added
Trac ticket: https://core.trac.wordpress.org/ticket/65208
Rename the test class Tests_Admin_wpThemeInstallListTable to Admin_WpThemeInstallListTable_GetViews_Test so that the name includes both the class under test and the method being tested.
Moves the @covers annotation to the class level and removes the duplicate method-level annotation. This makes the test class compatible with PHPUnit 11.
This ticket was mentioned in PR #11822 on WordPress/wordpress-develop by @abcd95.
3 weeks ago
#22
Trac ticket: https://core.trac.wordpress.org/ticket/65208
Rename the test class Tests_Admin_wpUserSearch to Admin_WpUserSearch_Construct_Test so that the name includes both the class under test (WP_User_Search) and the method being tested (__construct).
Replaces the @coversDefaultClass + relative @covers ::__construct() pattern with a single explicit class-level @covers WP_User_Search::__construct annotation. This is cleaner and compatible with PHPUnit 11.
This ticket was mentioned in PR #11831 on WordPress/wordpress-develop by @abcd95.
3 weeks ago
#23
Trac ticket: https://core.trac.wordpress.org/ticket/65208
Rename the test class Tests_Admin_WpTermsListTable to Admin_WpTermsListTable_HandleRowActions_Test so that the name includes both the class under test (WP_Terms_List_Table) and the method being tested (handle_row_actions).
Both existing test methods already covered the same production method, so no splitting was required. Consolidates the repeated method-level @covers WP_Terms_List_Table::handle_row_actions annotations into a single class-level declaration. This is cleaner and compatible with PHPUnit 11.
This ticket was mentioned in PR #11844 on WordPress/wordpress-develop by @abcd95.
3 weeks ago
#24
Trac ticket: https://core.trac.wordpress.org/ticket/65208
Splits the existing test class Tests_Admin_wpCommentsListTable into five focused test classes, one per method under test:
Admin_WpCommentsListTable_ExtraTablenav_Test- coversWP_Comments_List_Table::extra_tablenavAdmin_WpCommentsListTable_BulkActions_Test- coversWP_Comments_List_Table::bulk_actionsAdmin_WpCommentsListTable_PrintColumnHeaders_Test- coversWP_Comments_List_Table::print_column_headersAdmin_WpCommentsListTable_GetViews_Test- coversWP_Comments_List_Table::get_viewsAdmin_WpCommentsListTable_PrepareItems_Test- coversWP_Comments_List_Table::prepare_items
The @covers annotation is moved from method-level (repeated on each test) to a single class-level declaration in each new class. This makes all classes compatible with PHPUnit 11.
No abstract TestCase base class was introduced since the shared set_up() is a single line. The PrintColumnHeaders class constructs its own mock directly and needs no set_up() at all.
## Use of AI Tools
Used AI to debug test failures and refactor issues.
### Tests/admin/wpListTable: create a testcase for tests for WP_List_Table
Most tests testing the
WP_List_Tableclass will need the same set up/tear down fixtures, so moving those fixtures from the actual test class to a separate abstractTestCaseclass will allow for expanding the tests more easily.While doing this, we ran the tests in isolation using the
--filterCLI argument and discovered that there was a previously unrecognized presumption in the fixtures that the$hook_suffixglobal would always be set.This presumption proved incorrect as was demonstrated by the
Undefined global variable $hook_suffixwarning being thrown when running the tests.This warning has been fixed now in the new
Admin_WpListTable_TestCaseby adding some additional defensive coding and making sure that the global state is always brought back to its original state, including if that original state means that the$hook_suffixglobal variable was not set.### Tests/admin/wpListTable: split off tests for the get_column_info() method to separate test class
This will allow the test class to be compatible with PHPUnit 11 using the new
Admin_WpListTable_TestCaseclass as introduced in the previous commit.Includes removing method level
@coverstags in favour of class level@coverstags to allow for supporting PHPUnit 11 attributes instead of annotations.### Tests/admin/wpListTable: split off tests for the get_views_links() method to separate test class
This will allow the test class to be compatible with PHPUnit 11 using the new
Admin_WpListTable_TestCaseclass as introduced in the previous commit.Includes removing method level
@coverstags in favour of class level@coverstags to allow for supporting PHPUnit 11 attributes instead of annotations.### Tests/admin/wpListTable: rename the original test class to be more descriptive for the remaining tests
The remaining tests in the original test class all cover the magic methods and those are very closely related, so it is perfectly fine for those tests to be grouped together in one test class.
This commit, however, does rename the test class to use the new naming conventions, both for the class name as well as the file name.
It also includes removing method level
@coverstags in favour of class level@coverstags to allow for supporting PHPUnit 11 attributes instead of annotations.This will allow the test class to be compatible with PHPUnit 11.
Trac ticket: https://core.trac.wordpress.org/ticket/65208