Reschedule Events 03

Moving on with the development of the reschedule utility.

Picking from where I had left off

In the last article I had left a test failing and I will add the necessary code to make it pass, the test checks for the function return value

/**
 * @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' ) );
}

making it pass is a one line effort

<?php
/**
 * ...plugin header
 */

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

        return new tad_Reschedule();
    }
}


class tad_Reschedule {

}

Function Mocker

Since I’ve made a tool to suit my coding style it’s no surprise I’m pulling it in via Composer as soon as tests get serious

composer require lucatume/function-mocker --dev

By now it’s only use is replacing the $this->assert... syntax in favour of the Test::assert... one; the test case header I’ve modified to allow for that

<?php

use tad\FunctionMocker\FunctionMocker as Test;

...

Feeding the until

In testimony of my utter ignorance the while name method is not available as reserved word; until will do.
Before diving into the tests I’ve thought about what I could and would like to pass into the until method of the chain; the method should take care of verifying that a certain condition is still true or “truthy” and such condition might be a primitive (true,false, 1, 0, foo, the return value of a function or method) or a callback method or function responsible for that condition.
This quick recap of what I want that method to accept as a parameter generates the tests below (written before I’ve written any line of code):

public function primitives() {
    return array_map( function ( $val ) {
        return [ $val ];
    }, [ 1, '1', true, false, 'foo', 23, array(), new stdClass() ] );
}

/**
 * @test
 * it should accept primitives in the until method
 * @dataProvider primitives
 */
public function it_should_accept_primitives_in_the_until_method( $primitive ) {
    $out = tad_reschedule( 'some_hook' )->until( $primitive );
    Test::assertInstanceOf( 'tad_Reschedule', $out );
}

public function callables() {
    return array_map( function ( $val ) {
        return [ $val ];
    }, [ 'my_condition', [ 'Dummy', 'static_method' ], [ new Dummy(), 'instance_method' ] ] );
}

/**
 * @test
 * it should accept callables in the until method
 * @dataProvider callables
 */
public function it_should_accept_callables_in_the_until_method( $callable ) {
    $out = tad_reschedule( 'some_hook' )->until( $callable );
    Test::assertInstanceOf( 'tad_Reschedule', $out );
}

Why write these tests in the first place? For back-compatibility purposes: I might one day decide the until method will accept some other type of combination of parameters and have to make sure that the now trivial types will still be accepted and used. Takes little time to write now and will save much pain later in the “why is this not working?” field.
The code needed to pass the tests is again little:

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

        return new tad_Reschedule();
    }
}


class tad_Reschedule {

    public function until($condition){
        return $this;
    }
}

Until tests passing

Next

I will move one covering the missing two methods to then put the whole system to the whip; this code is on GitHub.