How do I test a WordPress-plugin built with composer (bedrock style)

Hi!

I’d like to know how you guys test your WordPress-plugins when you build them as composer libs. What’s the correct WorkFlow?

Fyi, I use Codeception, but this shouldn’t be specific towards that test framework.

So, first, I put my plugin in app/plugins/myplugin and obviously then I had access to the WordPress env to let the test environment run with WP-methods.

Then, I took it out to be its own git repo and added the composer file, like you should, and did a local repository in my main project to lift it in using composer. But then my plugin don’t have access to the WordPress env when it’s standalone (wp-load.php with the correct wp-config etc…).

So, what’s the best workflow here, when testing plugins?

I also tried installing WordPress in my plugin, as a dependency, but I wouldn’t want to (since you can only have one webroot) add my info to the wp-config.php in vendor/wordpress.

Any help on this would be appreciated so I can just get on with coding - the right way. :smile:

Thanks for a great lib btw (Bedrock)!

Regards,
Fredrik

Testing any WordPress requires some more complex test bootstrapping. The best resource I’ve seen is the WP-CLI command to scaffold a plugin which includes tests. If you aren’t using WP-CLI, they have a sample plugin here https://github.com/wp-cli/sample-plugin

It’s a little specific to TravisCI but everything is in there that you’d need to get a proper test environment up and running. I’m not sure there’s much you’d do differently with Composer.

Thanks for the quick reply! I ended up going with vagrant+ansible to deploy a test server in my plugin, which symlinks in the synced folder (the plugin) into app/plugins which gives it access to the wp env + lets me write acceptance tests on it as well. :slight_smile:

Looking forward to see what your Vagrant implementation will look like! There’s no dev preview available on github…? @swalkinshaw

I just created a modified version of the Chef cookbook we were using here https://github.com/roots/wp-cookbook

It’s missing the Vagrantfile but that’s just using Chef solo, running the recipes and setting some attributes. I’d probably like to redo it with Ansible since it’s simpler to get started with.

Do you have the Vagrantfile or at least the provisioner config for me as well, so I can just spin that one up and have a look at what’s in store for bedrock? :slight_smile:

I did upload my vagrant-ansible-plugin-test-test to bitbucket if you wanted to have a look. I’m not completely sure if it works 100% and it’s very dirty so I’ll remove it and re-add it when cleaned up. But still. https://bitbucket.org/hpress/wp-loves-s3

Should be to just clone that repo and do:

composer install (or whatever combination you have)
vagrant up
vagrant ssh
codecept run

You also need to add a .env file in the root of that project containing a S3 bucket (it tests a quick and dirty POC of media upload overrides).

.env:

TESTS_S3_KEY="X"
TESTS_S3_SECRET="X"
TESTS_S3_REGION="eu-west-1"
TESTS_S3_BUCKET=“X”

It’s my very first vagrant/ansible setup ever so… :slight_smile:

It’s also possible to visit localhost:1234 to do stuff in Wordpress but it’s resetted every time you run codecept run… Which is kinda handy anyways…

It should also be setup to connect to the mysql server with, say, Sequel pro with the test/test user, to debug and see what’s in the database before/after/during tests. Or to import/export stuff. :slight_smile:

But that’s kinda a bonus because of the setup…

The last piece before I can actually use this effeciently is a grunt-cli script with watch - to run the tests every time I save a file. Then I can break that terminal window with vagrant ssh out and just see the output, while I work locally with the project!

Here’s the Vagrantfile+Berksfile we use: https://gist.github.com/swalkinshaw/cd2a240b5cb95b41ff26

Requires these vagrant plugins:

vagrant plugin install vagrant-berkshelf
vagrant plugin install vagrant-omnibus
vagrant plugin install vagrant-hostsupdater
```