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

Remove testing code from plugin #117

Merged
merged 11 commits into from
Dec 21, 2017
Merged

Remove testing code from plugin #117

merged 11 commits into from
Dec 21, 2017

Conversation

kraftner
Copy link
Contributor

@kraftner kraftner commented Nov 2, 2017

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:

  • No code left in Plugin itself
  • Load dev bootstrap with composers autoload/files functionality
  • Use normal composer autoloader with PHP 5.3.2+ as xrstf/composer-php52 apparently doesn't support the files 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.
  • Adds hooks before WP is initialized by adding a hook to global $wp_filter

Test instructions

This PR can be tested by following these steps:

  • Checkout the branch and make sure to composer dumpautoload afterwards so the autoloader is regenerated.

Possibly fixes #111

Copy link
Contributor

@moorscode moorscode left a 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 ) {
Copy link
Contributor

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.


$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', '>=' ) ) {
Copy link
Contributor

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{
Copy link
Contributor

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.
Copy link
Contributor

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 );

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@kraftner
Copy link
Contributor Author

@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 think this is now clean, simple and ready for merging.

@moorscode
Copy link
Contributor

I agree, have tested this by running the unit tests and confirmed that it works.
Running nightwatch also loads the code as expected.
CR done!
Acceptance done!

@moorscode moorscode merged commit ae9ea16 into develop Dec 21, 2017
@moorscode moorscode deleted the stories/testing-code branch December 21, 2017 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Removing conditional testing code from the plugin
2 participants