PHPUnit Tests for WordPress Plugins: auto-increment ID records

by Daniel Convissor at 2012-11-24 21:00:00

This fourth posting in my WordPress plugin / PHPUnit testing series explains how I verify information inserted into database records with auto-increment IDs in my Object Oriented Plugin Template Solution.

In order for this post to be a be a stand-alone entity, I need to re-explain two terms from the earlier posts. First is "parent class." It holds helper properties and methods for use by multiple test classes.

abstract class TestCase extends PHPUnit_Framework_TestCase {}

The second term is "test class." It extends the "parent class" and contain the test methods that PHPUnit will execute.

class LoginTest extends TestCase {}

Naturally, if we're testing database records, we need a table to put them in. Below is the table definition from the activate() method in the plugin's admin class.

CREATE TABLE `$this->table_login` (
login_id BIGINT(20) NOT NULL AUTO_INCREMENT,
user_login VARCHAR(60) NOT NULL DEFAULT '',
date_login TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY  (login_id),
KEY user_login (user_login(5))
)

The real work happens in a method in my "parent class." It uses WordPress' $wpdb->insert_id in the WHERE clause of a query against my table. I then make sure the data matches expectations. Checking the character data is straight ahead via assertEquals().

Verifying the time stamp is a bit trickier. Since the database server may be using a different time zone than the computer running the tests, it is necessary to get the current time from the database, done here via SYSDATE(). I then compare the inserted time against the current time using PHP's DateTime::diff().

protected function check_login_record($user_name) {
    global $wpdb;

    $this->assertInternalType('integer', $wpdb->insert_id,
            'This should be an insert id.');

    $sql = 'SELECT *, SYSDATE() AS sysdate
            FROM `' . self::$o->table_login . '`
            WHERE login_id = %d';
    $actual = $wpdb->get_row($wpdb->prepare($sql, $wpdb->insert_id));
    if (!$actual) {
        $this->fail('Could not find the record in the "login" table.');
    }

    $this->assertEquals($user_name, $actual->user_login,
            "'user_name' field mismatch.");

    $date_login = new DateTime($actual->date_login);
    // Keep tests from going fatal under PHP 5.2.
    if (method_exists($date_login, 'diff')) {
        $sysdate = new DateTime($actual->sysdate);
        $interval = $date_login->diff($sysdate);
        $this->assertLessThanOrEqual('00000000000001',
                $interval->format('%Y%M%D%H%I%S'),
                "'date_login' field off by over 1 second: $actual->date_login.");
    }
}

Now let's utilize that stuff in a test in the "test class."

public function test_insert_login() {
    self::$o->insert_login($this->user_name);
    $this->check_login_record($this->user_name);
}

That's All Folks. I'm curious what people think of this series. Feel free to submit comments via the form on the post's permalinked page. The full framework, can be found in the Object Oriented Plugin Template Solution.

Tags: wordpress, phpunit, php

View all posts

Email me a comment:

(I'll append it here when I get a chance.)