WordPress Db Codeception module - 05

Keeping up the work on my WordPress specific Codeception Db module extension I’ve refactored a little code and added some more method to cover more tables.

Allow me negligence

Developing the module I’ve tried striking a difficult equilibrium between a raw database manipulation (Codeception Db module allows that already) and a more high-level approach to a WordPress specific database wrapping things up to a point.
Being the development of the module driven by my current needs I’ve left the possibility to inject incongruences into the database open for use; an example might be setting up a test case where a post meta field is set for a non existing post.

<?php 
$I = new AcceptanceTester($scenario);

$I->am('an admin');
$I->wantTo('create a meta value for a post that does not exist');

// post_id, meta_key, meta_value
$I->havePostMetaInDatabase(10, 'somePostMeta', 'foo');

$I->...

I might want to test a database cleaner as an example.

But kindly remind me

On the other hand I might want to make sure to add things in an orderly and logical fashion and ask the module to help me do it by checking for target existence; I’ve added a checkExistence optional configuration value (default is false) to do that which, in the case above, would raise an exception like

A post with an id of 10 does not exist

That’s an experiment with the grabFromDatabase method and I’m quite proud of the accomplishment.

Post meta and term relationships

I’ve added methods for both tables that can be used like

<?php 
$I = new AcceptanceTester($scenario);

$I->am('an admin');
$I->wantTo('add some post meta and assign terms');

$I->havePostInDatabase(10);
$I->haveTermInDatabase('Some fancy category', 12, array('description' => 'Some fancy category description'));

$I->havePostMetaInDatabase(10, 'somePostMeta', 'foo');
$I->havePostWithTermInDatabase(10, 12);

$I->seePostMetaInDatabase(array('post_id' => 10, 'meta_key' => 'somePostMeta'));
$I->seePostWithTermInDatabase(10, 12));

Sharing

The code is on GitHub and in a Composer package.