-
Notifications
You must be signed in to change notification settings - Fork 20
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
Remove testing code from plugin #117
Conversation
…parently doesn't support "files" in "autoload")
Conflicts: inc/class-ac-yoast-seo-acf-content-analysis.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.
Make the code a bit more readable and use Monkey Brains to define the add_action
method to reduce mock code.
|
||
function yoast_acf_analysis_test_data_loader() { | ||
|
||
if ( defined( 'AC_YOAST_ACF_ANALYSIS_ENVIRONMENT' ) && 'development' === AC_YOAST_ACF_ANALYSIS_ENVIRONMENT ) { |
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 prefer to have a negated condition check and do a return
instead of encapsulating this piece of code with the condition indentation.
yoast-acf-analysis.php
Outdated
|
||
$ac_yoast_seo_acf_analysis = new AC_Yoast_SEO_ACF_Content_Analysis(); | ||
$ac_yoast_seo_acf_analysis->init(); | ||
if ( version_compare( PHP_VERSION, '5.3.2', '>=' ) ) { |
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 be less complicated if you can create a variable to hold the file to be loaded.
Defaulting to vendor/autoload.php
and making an exception to overwrite with the autoload_52.php
version.
After that we only have to use one is_file
and require
call with that same variable.
'accepted_args' => 1, | ||
); | ||
|
||
}else{ |
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.
CS: Missing spaces around the else
statement.
<?php | ||
|
||
/** | ||
* This file is loaded by Composer autoload-dev, and that happens before `add_action` is available. |
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 have tried to manually trigger the Monkey Brains setup method, to avoid having to use the ugly global WordPress mocking.
The following code snippet works as expected and removes all problems we have with the current approach.
// Load Monkey Brains WP mock methods.
Brain\Monkey\setUp();
add_action( 'admin_init', 'yoast_acf_analysis_test_data_loader', 11 );
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.
Oh, this code isn't mocking add_action
but actually adds a filter by messing with the globals before the plugin API is ready.
But I just realised that that isn't needed anyway because the autoloader only gets loaded after the Plugin API is ready anyway. So I can further simplify this.
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 that was there for a reason because of course when running Unit Tests the Plugin API isn't ready. Your alternative approach is nice though. I just wrapped it inside a conditional now because otherwise Brain\Monkey\setUp()
is also called during normal execution which needlessly loads Patchwork.
@moorscode Thanks for your feedback, I have updated the PR. After some messing around I realised that we only really need to load the test data when we're actually in the context of WP with the Plugin API available because in Unit Tests we don't need the test data anyway. See my ramblings for details. |
I agree, have tested this by running the unit tests and confirmed that it works. |
Summary
As noticed in #103 it is not ideal to handle the loading of testing data inside the production plugin code as said in #111.
I've been thinking about several approaches: This PR proposes the one I like the most.
Relevant technical choices:
xrstf/composer-php52
apparently doesn't support thefiles
autoload functionality, hence this only works with PHP 5.3.2+. But as this is only needed for client-side tests those shouldn't really care about the PHP version in use.global $wp_filter
Test instructions
This PR can be tested by following these steps:
composer dumpautoload
afterwards so the autoloader is regenerated.Possibly fixes #111