bigbite / wp-cypress

WordPress end to end testing with Cypress.io.
MIT License
87 stars 19 forks source link

Running PHP Code during Tests #46

Closed jasonagnew closed 3 years ago

jasonagnew commented 3 years ago

Is your feature request related to a problem? Please describe. When running tests it can be helpful to control the setup or configuration of other plugins. For example running of a filter.

add_filters( 'debug_bar_enable', '__return_false' );

Describe the solution you'd like A bootstrap.php that is loaded at earliest execution from which you add filters and extend from there.

Describe alternatives you've considered On most cases having a testing plugin which you load for tests solves this problem but in situation where you need to affect mu-plugins this likely not early enough. If WP Cypress defined constant in wp-config.php then that could be used decide whether to load your own testing mu-plugin.

liamdefty commented 3 years ago

@jasonagnew is an interesting problem 🤔

However I don't think the bootstrap.php is needed.

I think the simplest solution could be to add an environment variable to define that cypress tests are running. This way you can add code to your plug-in, mu-plug-in or theme to run code that is only executed when when cypress tests are ran which I think is the same as what you're describing in the alternative solution you suggested?

What are your thoughts?

jasonagnew commented 3 years ago

@liamdefty After sleeping on it I agree. I feel a constant/environment variable is quickest and most flexible solution. It's not an urgent requirement but maybe something we can aim to add in release 0.8.1? I can look into doing it some point next week.

liamdefty commented 3 years ago

@jasonagnew yeah I agree 👍🏽 it'd be nice to have something defined in this package

In the mean time, you can add constants in the cypress.json config which might be useful and achieve something similar.

See this example:

{
  "pageLoadTimeout": 30000,
  "wp": {
    "version": ["5.5.1"],
    "wpContent": {
      "path": "./",
      "activeTheme": "my-theme"
    },
    "config": {
      "WP_ENVIRONMENT": "cypress"
    }
  }
}
ampersarnie commented 3 years ago

I'm not sure I agree with the decision made here that a bootstrap.php or similar isn't needed, and think this idea may need exploring more. I'm not against the WP_ENVIRONMENT constant either but what you're going to end up introducing here is test logic amongst your business logic, even if this is just filter usage. This really should be avoided and there should be separation between the two, else you may end up in the realm of having, or forcing bad testing practises.

I don't see why a bootstrap couldn't be added within wp-config.php and have that also include wp-settings.php to ensure add_filter, add_action, etc are defined and have that to utilise any additional setup that may be required.

jasonagnew commented 3 years ago

@ampersarnie Since I raised this issue I've started using the wp-cypress-config.php which means you can override pluginable functions and define constants where needed.

ampersarnie commented 3 years ago

@jasonagnew That sounds like a more reasonable solution and is effectively the same idea under a different file name. Might be worth closing this if solves the issue - looks like it may have.