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

Make it easier to write tests for WordPress plugins containing CLI commands #31

Merged
merged 6 commits into from
Dec 3, 2018

Conversation

swissspidy
Copy link
Member

Situation:

I'm developing a WordPress plugin that registers some WP-CLI commands. Now I want to write some functional tests for these, ideally by leveraging this identical testing framework as for standalone commands.

In case anyone's wondering, the plugin I'm developing can be found at https://github.com/wearerequired/traduttore.

To run the tests, I need to activate / load the plugin somehow.

One suggestion by Daniel that led me to the right path was creating a ZIP archive and installing that ZIP file. He did this in a plugin once where FeatureContext.php was part of the code base. I obviously can't edit that file because it's now in the wp-cli-tests package.

Alain's suggestion was to use something like this:

Feature: Testing a custom plugin

  Background:
    Given a WP installation
    And these installed and active plugins:
    """
    {RUN_DIR}/packaged_plugin.zip
    """

However, variables are currently not being replaced in the these installed and active plugins step.

Plus, RUN_DIR points to the WordPress directory used for the tests, not the actual repository where my plugin and my ZIP file reside.

Thus, I decided to add a new variable, PROJECT_DIR which aims to be the plugin source directory. That's also where wp-cli/wp-cli-tests is installed using Composer in PROJECT_DIR/vendor/wp-cli/wp-cli-tests.

This way I can use the following scenario:

Feature: Testing a custom plugin

  Background:
    Given a WP installation
    And these installed and active plugins:
    """
    {PROJECT_DIR}/packaged_plugin.zip
    """

This points to the source directory of the project where the test suite has been installed
Allows installing plugins from ZIP files residing in PROJECT_DIR
@swissspidy
Copy link
Member Author

wearerequired/traduttore#124 is where I'm currently using this PR.

features/bootstrap/FeatureContext.php Outdated Show resolved Hide resolved
@schlessera
Copy link
Member

schlessera commented Dec 1, 2018

I think this is a good first step to support this, and the changes generally make sense in any case. However, I think we should ultimately try to make it possible to test a plugin without needing to zip it up first.

@swissspidy
Copy link
Member Author

Oh, absolutely.

I explored that too but quickly ran into problems with the autoloader, getting errors like these (as described in wearerequired/traduttore#124):

Cannot redeclare composerRequiree4cd214cfd5d4f7870ef4ac7c4e1ab20() (previously declared in vendor/composer/autoload_real.php:63) in /private/var/folders/.../wp-cli-test-run-cli.feature.19-5bf18a4fd5de42.86243506/wp-content/plugins/traduttore/vendor/composer/autoload_real.php

@swissspidy swissspidy requested a review from schlessera December 3, 2018 17:06
@schlessera schlessera added this to the 2.0.11 milestone Dec 3, 2018
@schlessera schlessera merged commit 89b8328 into master Dec 3, 2018
@delete-merged-branch delete-merged-branch bot deleted the plugin-tests branch December 3, 2018 20:42
@schlessera schlessera changed the title [WIP] Make it easier to write tests for WordPress plugins containing CLI commands Make it easier to write tests for WordPress plugins containing CLI commands Dec 3, 2018
@schlessera schlessera modified the milestones: 2.0.11, 2.0.12 Dec 3, 2018
schlessera added a commit that referenced this pull request Jan 5, 2022
Make it easier to write tests for WordPress plugins containing CLI commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants