In my last post, I confirmed how you can alter the WordPress content material endpoints the use of WordPress hooks — aka the WordPress plugins API — right through REST API requests. The sensible instance modified the underlying question to support seek.

Now I want to check that code remoted from the WordPress plugins API. For this newsletter, we can think the Plugins API is operating as documented. This, then again, isn’t a secure assumption, so we want to be in a position if it isn’t running as deliberate.

This submit is in reality about how we will regulate for that downside. I’ll be strolling you via putting in a WordPress plugin for acceptance checking out and unit checking out, and explaining what we will and will’t check with unit exams. Doing so will have to mean you can know the way to construction your code with this difference in thoughts and start to display you the gear and strategies of writing those two distinct forms of exams.

Environment Up A WordPress Plugin For Unit & Integration Exams

The use of WP-CLI To Create A WordPress Plugin With Unit Exams

I don’t need to spend a ton of time on how you can setup and construction the plugin and exams. We will be able to use WP-CLI to scaffold lots of the code for us. So first, let’s create the plugin the use of this command.

wp scaffold plugin rest-api-search

This creates a plugin referred to as “rest-api-search” with the WordPress check suite in a position to move. That is nice, as a result of we’ll need to use that for integration exams. I’d moderately use PHPUnit immediately for my unit exams. Additionally, I’d like to control operating exams and sniffs the use of Composer and feature Composer to supply a PSR-4 autoloader.

You’ll be told extra about how I set this up via studying those posts from Torque:

I did make two giant adjustments to what WP-CLI created. First, I modified the PHPCS ruleset to make use of tabs as an alternative of areas to be able to apply the PSR2 as an alternative of the WordPress coding same old.

Prior to now I had used inline arguments for the code sniffer. Now I’m the use of a PHPCS XML configuration document as command line choices could be too complicated:

    PSR2 with tabs as an alternative of areas.

This imports the PSR-2 same old and provides one amendment. To temporarily ascertain my code to this new same old, I used the command phpcbf, which is part of PHPCS, to mend all of my code to the brand new same old. I modified the “fixes” command from the former article to make use of PHPCS’ integrated fixer since it'll mechanically use that very same xml configuration document. Here's the brand new command:

phpcbf src/ && phpcbf exams/

2d, I restructured the exams in order that they had been in two suites: unit and integration. Afterward an acceptance checking out suite might get added, however for now, that’s what we want. Since I’m going to make use of Composer’s autoloader for exams, I arranged the directories and sophistication names in keeping with the PSR-4 spec.

Because of this, the listing for my unit check is Exams/Unit and the listing for my checking out mocks is Exams/Mocks. I moved the exams WP CLI generated into their very own listing and bootstrap folder that aren't getting used. Here's what my listing construction ended up taking a look like earlier than I began including my code in:

The listing src will space my root namespace and all of my PHP categories. Here's the composer.json that attracts this all in combination:

View the code on Gist.

Operating Exams On Git Hooks

One different new factor you'll have spotted used to be that I added cghooks — composer git hooks. This bundle is a Composer plugin that hooks into Git occasions, and allows you to run Composer scripts at the ones occasions.

I used the pre-commit hook, which fires when you're making a Git devote, earlier than it's recorded. As a result of this script has to go out effectively for the decide to be finished, the setup is imposing a collection of same old, required pre-commit steps.

In my case, I best added the code formatting, no longer the exams. This prevents code formatting problems from getting driven uupstream. Nevertheless it nonetheless permits incomplete paintings to be driven earlier than it fixes all the exams.

I'm hesitant to counsel the use of this on a group open-source challenge as no longer all builders might need to or want to set up the exams to give a contribution.

Making ready For Unit Exams

We’re going to must do some refactoring earlier than we will in reality get into checking out. However first, I’d love to pause and write one check first.

Let’s create a great elementary check that doesn’t duvet any of our code. I love so as to add this sort of check first, simply to verify my plugin check are arrange accurately. It’s simply saying true is correct. Successfully, it covers that PHPUnit works for on this context.

assertNotEquals($excepted, $precise);

Once we’re writing unit exams, our purpose is to check our personal methods with as few dependencies as imaginable. No dependencies is the inconceivable supreme we try for. We’ll accept as true with that PHPUnit’s exams paintings, so long as that one check passes we will accept as true with that dependency.

You could realize that this check is extending a category in the similar namespace referred to as TestCase. That’s simply an empty elegance extending PHPUnitFrameworkTestCase. The really useful document construction for phpunit exams is one elegance of exams according to elegance. So we’re going to have a number of categories in our exams listing. I ensure you sooner or later we’ll need to proportion code between those exams. Having all of our check categories extending one elegance goes to assist so much.

Time To Get started Trying out

If that check passes, then you've your check suite setup. That’s superior. Now you'll be able to get started writing exams by yourself, or wait till the following submit had been we’ll duvet refactoring the code for testability and writing extra exams.

The plugin I’ve been the use of to create and check this situation code can be found on Github. It wishes extra exams to succeed in complete protection. Be at liberty to fork it and be told extra via experimenting with it.

Earlier than we wrap up, I’d like to return again in short to the mechanically generated similar check that WP-CLI generated and it’s set up script. WordPress’ check suite, which we’ll get again to, isn't going to be run with composer like the remainder of our exams are.

It is not uncommon within the WordPress ecosystem to make use of that check suite for unit exams and integration exams. I’m no longer keen on that development, as I need to deal with them as two completely other check suites and I love the use of Composer for scripting all of this. I’ll be masking why over my subsequent two posts, however I need to level that out in case you’re used to the use of them in combination.

Josh Pollock

Josh is a WordPress developer and educator. He's the founding father of Caldera Labs, makers of superior WordPress gear together with Caldera Forms — a drag and drop, responsive WordPress shape builder. He teaches WordPress building at Caldera Learn.

The submit Advanced OOP For WordPress Part 2: Setting Up Automated Testing Of WordPress REST API Plugins seemed first on Torque.

WordPress Agency

[ continue ]