WPDb post related methods refresh

Thinking post handling in acceptance testing with WPDb.

Functional tests tools

One of the tools available when writing WordPress functional tests, either using WordPress automated testing suite or an alternate solution like the WP Loader module part of wp-browser, is the posts factory.
In coding terms this means that if I need to populate the posts table with some random content then I could write this:

class AClassTest extends \WP_UnitTestCase {

    public function test_some_method(){
        $post_id = $this->factory()->post->create();

        udpate_post_meta($post_id, 'meta_key', 'meta_value');

        $this->assertEquals('meta_value', get_post_meta('meta_key', true));
    }

}

The factory()->post method has “siblings” allowing to create many posts at once like

$this->factory()->post->create_many();

and more to create users, blogs and in general any other entity WordPress handles.
The same is not true in acceptance tests.

An ideal solution

While I’ve bundled a method in the WPDb wp-browser module to easily add a post in the database the code is dated and no more fulfilling all my needs.
It’s time for a refresh and some tests and hence I will start from the API I’d like to use.
First of all I will review and refactor the current implementation of the

$I->havePostInDatabase()

method allowing for richer overrides like allow for the definition of the post meta

$meta = ['key_one' => 23, 'key_two' => 24, 'key_three' => 'foo'];
$I->havePostInDatabase(['meta'=> $meta]);

or the definition of the categories and tags associated with the post

$terms = ['category' => ['cat_one','cat_two'], 'post_tag' => ['tag_one', 'tag_two']];
$I->havePostInDatabase(['terms'=> $terms]);

The function could be extended to custom taxonomies too

$terms = ['taxonomy_one' => ['term_one','term_two']];
$I->havePostInDatabase(['terms'=> $terms]);

The same should apply to the creation of many posts

$count = 10;
$I->haveManyPostsInDatabase($count);

A simple placeholder system to allow for count based overriding of defaults like a progressive post title

$count = 10;
$overrides = ['post_title' => 'Post Title {{n}}'];
$I->haveManyPostsInDatabase($count, $overrides);

Next

The two methods above alone will keep me busy on the project for some time so down to code and tests.