Reschedule Events 02

Test driven development of the reschedule plugin.

Set up

I’ve outlined the scope of the project in an earlier post and will simply move into development using TDD as much as I can.
The first step is setting up the project using Composer

composer init

after the usual question and answer drill I will end up with a scaffold composer.json file

{
    "name": "lucatume/tad-reschedule",
    "description": "Easy cron event rescheduling in WordPress",
    "type": "wordpress-plugin",
    "require-dev": {
        "lucatume/wp-browser": "dev-master"
    },
    "license": "GPL 2.0",
    "authors": [
        {
            "name": "Luca Tumedei",
            "email": "luca@theaveragedev.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

a call to composer update command will pull in the project only requirement: WP Browser; after a little CLI output I will be able to scaffold the tests

wpcept bootstrap

and have Codeception set up the project testing folders for me: tad-reschedule wpcept scaffold

I’ve customized the codeception.yml file to match my local testing environment and moved on (see wp-browser readme).

Plugin skeleton

I wrote the smallest possible amount of code to have the plugin activate/deactivate properly and define the tad_reschedule function that’s taking charge of initializing the method chain.

<?php
/**
 * Plugin Name: Reschedule Utility
 * Plugin URI: http://theAverageDev.com
 * Description: Easy cron event rescheduling.
 * Version: 1.0
 * Author: theAverageDev
 * Author URI: http://theAverageDev.com
 * License: GPL 2.0
 */

if ( ! function_exists( 'tad_reschedule' ) ) {
    function tad_reschedule( $hook ) {
        if ( ! is_string( $hook ) ) {
            throw new InvalidArgumentException( 'Hook name must be a string' );
        }
    }
}

The first test

Since there is a chain involved I will have to develop at least one class and naming it tad_Reschedule makes sense as a starting point; before anything I will scaffold a first test case, a WP_Unit powered functional one

wpcept generate:wpunit functional tad_Reschedule

I’ve modified the main bootstrap file, the tests/_bootstrap.php one, to require the plugin file

<?php
// This is global bootstrap for autoloading
include_once dirname( dirname( __FILE__ ) ) . '/tad-reschedule.php';

and began by writing a first test method

<?php


class tad_RescheduleTest extends \WP_UnitTestCase {

    protected $backupGlobals = false;

    public function setUp() {
        // before
        parent::setUp();

        // your set up methods here
    }

    public function tearDown() {
        // your tear down methods here

        // then
        parent::tearDown();
    }

    /**
     * @test
     * it should return an instance of the tad_Reschedule class
     */
    public function it_should_return_an_instance_of_the_tad_reschedule_class() {
        $this->assertInstanceOf( 'tad_Reschedule', tad_reschedule( 'my_hook' ) );
    }

}

and watch it fail tad-reschedule first failing test

Next

Now the basic test scaffold is up and on GitHub I will iterate over it to add all the functionality I need.