Embedding WordPress for testing purposes 01

An attempt at a fully contained WordPress testing environment.

The need for a server in WP Loader

When setting up to test WordPress plugins and themes on a functional level using the WP Loader module, part of the wp-browser extension for Codeception, the need for a runnin server will arise.
WordPress will not run if a underlyin MySQL server is not supporting it and so, even not making any use of a WordPress database abstraction function like wp_insert_post or get_option, a test like this one will fail:

// file plugin/slasher/slasher.php


class Slasher {

    public function slashit($string){
        return trailingslashit($string);
    }

}



// file tests/functional/MyPluginTest.php


class MyPluginTest extends WP_UnitTestCase {

    public function testTrailingslashit(){

        $sut = new Slasher();
        $this->assertEquals('foo/', $sut->slashit('foo'));

    }
}

The trailingslashit function is defined by the WordPress code base and the use of this function in a class under test, or in a test itself, without a WordPress instance running in the same variable scope will generate errors.
WP Loader makes it very easy to have such a pre-condition fulfilled by pointing it to a running WordPress installation with its configuration settings

modules:
  enabled:
      - WPLoader
  config:
      WPLoader:
          wpRootFolder: "/Users/User/www/wordpress"
          dbNAme: "wpress-tests"
          dbHost: "localhost"
          dbUser: "root"
          dbPassword: "root"
          wpDebug: true
          dbCharset: "utf8"
          dbCollate: ""
          tablePrefix: "wptests_"
          domain: "example.org"
          adminEmail: "admin@example.com"
          title: "Test Blog"
          phpBinary: "php"
          language: ""
          config_file: ""
          plugins: ['hello.php', 'my-plugin/my-plugin.php']
          activatePlugins: ['hello.php', 'my-plugin/my-plugin.php']
          booststrapActions: ['my-first-action', 'my-second-action']

Trying to skip the need for a server

The idea of an “embedded WordPress” installation that could supply the above mentioned running WordPress instance came to me taking the possibility the SQLite Integration plugin offers of running WordPress on a SQLite server.
The idea would be to have an extension to WP Browser that could use an embedded version of WordPress to access WordPress code base, all of it would be spun up using a SQLite server.
I’ve put together the first pieces of the project and would like to have a working, or seemingly so, version up and running soon.
To make a code example the functional.suite.yml file would look like this

# Codeception Test Suite Configuration

# suite for WordPress functional tests.
# Emulate web requests and make application process them.
class_name: FunctionalTester
modules:
    enabled: [Filesystem, EmbeddedWpLoader, \Helper\Functional]

and the codeception.yml file would add the EmbeddedWpLoader settings

 modules:
  enabled:
      - EmbeddedWpLoader
  config:
      EmbeddedWpLoader:
          dbName: "wordpress"
          dbDir: "."
          wpDebug: true
          tablePrefix: "wptests_"
          domain: "example.org"
          adminEmail: "admin@example.com"
          title: "Test Blog"
          phpBinary: "php"
          language: ""
          config_file: ""
          plugins: ['hello.php', 'my-plugin/my-plugin.php']
          activatePlugins: ['hello.php', 'my-plugin/my-plugin.php']
          booststrapActions: ['my-first-action', 'my-second-action']

Many of the mechanics I still have to work out but this is a rough idea.