-
Notifications
You must be signed in to change notification settings - Fork 93
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
Move test files to respective plugins #1262
Conversation
<!-- Do not apply filename rules for unit tests and object cache --> | ||
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase"> | ||
<exclude-pattern>tests/*</exclude-pattern> | ||
</rule> | ||
<rule ref="WordPress.Files.FileName.InvalidClassFileName"> | ||
<exclude-pattern>tests/*</exclude-pattern> | ||
</rule> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Autoloading test helpers with Composer has been removed so PSR4 file naming is no longer, required as it is not ideal to autoload test helpers for multiple plugins from a single location. In most cases, PHPUnit's file loader will handle this effectively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where were the autoloading test helpers removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved them to their plugin tests/data directories.
@@ -9,6 +9,11 @@ | |||
// phpcs:disable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint | |||
// phpcs:disable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint | |||
|
|||
// For the sake of auto-loading by PHPUnit, we may need to include this file here. | |||
if ( ! class_exists( 'WP_Filesystem_Base' ) ) { | |||
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is necessary because when PHPUnit's file loader requires this file, the base FS file is not loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this make more sense in the set_up_mock_filesystem()
method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to require it manually because filesystem_method_file
already loads it if necessary. However, there is an issue with how PHPUnit includes files defined in a test suite. When PHPUnit tries to include this file, WP_Filesystem_Base
isn't loaded by WordPress, resulting in a fatal error:
[error]
PHP Fatal error: Uncaught Error: Class "WP_Filesystem_Base" not found in /app/public/content/plugins/performance/plugins/performance-lab/tests/data/class-wp-filesystem-mockfilesystem.php:20
Stack trace:
#0 phar:///usr/local/bin/phpunit/phpunit/Util/FileLoader.php(57): include_once()
#1 phar:///usr/local/bin/phpunit/phpunit/Util/FileLoader.php(43): PHPUnit\Util\FileLoader::load('/app/public/con...')
#2 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(309): PHPUnit\Util\FileLoader::checkAndLoad('/app/public/con...')
#3 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(397): PHPUnit\Framework\TestSuite->addTestFile('/app/public/con...')
#4 phar:///usr/local/bin/phpunit/phpunit/TextUI/TestSuiteMapper.php(56): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(307): PHPUnit\TextUI\TestSuiteMapper->map(Object(PHPUnit\TextUI\XmlConfiguration\TestSuiteCollection), 'performance-lab')
#6 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(106): PHPUnit\TextUI\Command->handleArguments(Array)
#7 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(96): PHPUnit\TextUI\Command->run(Array, true)
#8 /usr/local/bin/phpunit(2520): PHPUnit\TextUI\Command::main()
#9 {main}
Next PHPUnit\TextUI\RuntimeException: Class "WP_Filesystem_Base" not found in phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:98
Stack trace:
#0 /usr/local/bin/phpunit(2520): PHPUnit\TextUI\Command::main()
#1 {main}
thrown in phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php on line 98
Fatal error: Uncaught Error: Class "WP_Filesystem_Base" not found in /app/public/content/plugins/performance/plugins/performance-lab/tests/data/class-wp-filesystem-mockfilesystem.php:20
Stack trace:
#0 phar:///usr/local/bin/phpunit/phpunit/Util/FileLoader.php(57): include_once()
#1 phar:///usr/local/bin/phpunit/phpunit/Util/FileLoader.php(43): PHPUnit\Util\FileLoader::load('/app/public/con...')
#2 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(309): PHPUnit\Util\FileLoader::checkAndLoad('/app/public/con...')
#3 phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php(397): PHPUnit\Framework\TestSuite->addTestFile('/app/public/con...')
#4 phar:///usr/local/bin/phpunit/phpunit/TextUI/TestSuiteMapper.php(56): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(307): PHPUnit\TextUI\TestSuiteMapper->map(Object(PHPUnit\TextUI\XmlConfiguration\TestSuiteCollection), 'performance-lab')
#6 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(106): PHPUnit\TextUI\Command->handleArguments(Array)
#7 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(96): PHPUnit\TextUI\Command->run(Array, true)
#8 /usr/local/bin/phpunit(2520): PHPUnit\TextUI\Command::main()
#9 {main}
Next PHPUnit\TextUI\RuntimeException: Class "WP_Filesystem_Base" not found in phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:98
Stack trace:
#0 /usr/local/bin/phpunit(2520): PHPUnit\TextUI\Command::main()
#1 {main}
thrown in phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php on line 98
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this is needed because autoloading of test helpers was removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess not. It's just not clear to me how this WP_Filesystem_Base
class was being autoloaded before but now it isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 38 in 9c410e5
"PerformanceLab\\Tests\\": "tests/utils" |
And on trunk it's inside the utils - https://github.com/WordPress/performance/tree/trunk/tests/utils/Filesystem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So will the autoload-dev
then be removed from composer.json
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if instead of psr-4
it was replaced with a classmap
instead? Then we could keep the autoloading, although I guess the issue then is that we have a classmap for all of the plugins together?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's the concern. It seems like I haven't removed the autoloading from composer yet, but that's need to be removed.
5a240ce
to
75c1ef2
Compare
@@ -121,6 +121,7 @@ const buildPlugin = ( env ) => { | |||
ignore: [ | |||
'**/.wordpress-org', | |||
'**/phpcs.xml.dist', | |||
'**/tests', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎
1b2f270
to
f1e331a
Compare
plugins/webp-uploads/tests/data/class-image-has-size-source-constraint.php
Outdated
Show resolved
Hide resolved
plugins/performance-lab/tests/data/class-wp-filesystem-mockfilesystem.php
Outdated
Show resolved
Hide resolved
Co-authored-by: Weston Ruter <westonruter@google.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming this PR is ready for review? Approved! cc @joemcgill
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without repeating all of my previous comments from #1012 (comment) and #1012 (comment) I still think we would be better served to organize and run our PHPUnit tests from a single root directory.
However, I acknowledge that ever since we began the move from modules to standalone plugins for performance features, we are already functionally treating each set of tests for each plugin as separate suites and this PR does nothing to change that, but instead makes the current organization of the files make more sense now that we've moved the main Performance Lab plugin into the /plugins/
folder, so I'm ok for us to merge this.
That said, I would strongly suggest that we first agree on the solution in #1012 or a new issue just to define our PHPUnit structure before moving forward with anything else that was originally implemented in #1065, i.e.:
- Move tests to their plugin directories and move any helper files to the tests/data directory.
- Add separate phpunit.xml.dist to plugin directories. The current setup also allows to override of PHPUnit config per plugin e.g. put a phpunit.xml file in the plugin directory and PHPUnit will override phpunit.xml.dist.
- Move test commands from composer to npm scripts. NPM allows to run scripts from any directory and its config feature helps provide required args from CLI to npm commands out of the box. See: https://docs.npmjs.com/cli/v10/using-npm/config#command-line-flags
As @joemcgill and I discussed over DM, even though all of the standalone plugins are being tested in isolation today, we should look for opportunities to test them together to ensure that they all work together while all are active. To this end, could still put tests in the root tests directory specifically for testing plugins together (beyond just running all of the tests as part of a single test suite in one process). So we could have a We could also consider adding an In this way, we'll have both angles covered: plugins should be able to work 100% while running in complete isolation as a standalone product, but the plugins should also be able to work 100% when all other plugins are active as part of the complete suite of Performance Lab plugins. We just need to make sure that in this process that we minimize as much as possible any fragmentation or duplication of the infrastructure code being used by the standalone plugins. |
Summary
Previously #1065
See #1065 (comment)
Relevant technical choices
tests/plugins
to respective plugins.tests/data
directories.