Symlinker extension and environments

Environments support added to the Symlinker extension.

The power of environments

Environments are a big feature of Codeception and narrowing down the use cases to what could apply in a WordPress testing context some cases are:

  • testing with different users
  • testing with different database starting fixtures
  • testing on single and multisite installations

While the first two points are easy to cover thanks to Codeception flexibility the last one can be tricky.
It’s what I’ve tried to make easy again supporting environments in the Symlinker wp-browser extension.

An example

Without going into the same details the README file covers I’ve set up a basic example to show how I’d use it.
I’ve got two WordPress local installations set up:

  • http://wp.dev being served from the /Users/Luca/Sites/wp folder
  • http://mu.dev being served from the /Users/Luca/Sites/mu folder

The second one being a multisite installation of WordPress.
In the ~/Desktop/my-plugin folder I’ve set up a plugin that will filter the body_class to add a class depending on the installation being a single or multi site one.

<?php
/**
 * Plugin Name: My Symlinked Plugin
 * Plugin URI: http://theAverageDev.com
 * Description: My Symlinked Plugin
 * Version: 1.0
 * Author: theAverageDev
 * Author URI: http://theAverageDev.com
 * License: GPL 2.0
 */

add_filter('body_class', function ($classes) {
    $classes[] = is_multisite() ? 'multiSite' : 'singleSite';
    return $classes;
});

I want to make sure the plugin is working as intended in both scenarios.

Codeception configuration

In the acceptance.suite.yml file I’m specifying two supported environments: single and multisite.

# Codeception Test Suite Configuration

# suite for WordPress acceptance tests.
# perform tests in browser using WPBrowser or WPWebDriver modules.
class_name: AcceptanceTester
modules:
    enabled:
        - WPBrowser
        - \Helper\Acceptance
        - WPDb
env:
    single:
        modules:
            config:
                WPBrowser:
                    url: 'http://wp.dev'
                WPDb:
                    dsn: 'mysql:host=127.0.0.1;dbname=wp'
    multisite:
        modules:
            config:
                WPBrowser:
                    url: 'http://mu.dev'
                WPDb:
                    dsn: 'mysql:host=127.0.0.1;dbname=mu'

The WPBrowser module will have to hit a different url and the WPDb module will need to modify a different database according to the environment in use.
I’d now like the tad\WPBrowser\Extension\Symlinker to conditionally symlink the plugin file in the right wp-content/plugins folder according to the current environment.
In the codeception.yml file I’m defining the different destinations:

extensions:
    enabled:
        - tad\WPBrowser\Extension\Symlinker
    config:
        tad\WPBrowser\Extension\Symlinker:
            mode: plugin
            destination:
                single: /Users/Luca/Sites/wp/wp-content/plugins
                multisite: /Users/Luca/Sites/mu/wp-content/plugins

Running the tests

To test the idea I’ve set up the SymlinkerCest test:

<?php


class SymlinkedCest
{
    public function _before(AcceptanceTester $I)
    {
        $I->haveOptionInDatabase('active_plugins', ['my-plugin/my-plugin.php']);
    }

    public function _after(AcceptanceTester $I)
    {
    }

    /**
     * @test
     * it should see multisite in multisite env
     * @env multisite
     */
    public function it_should_see_multisite_in_multisite_env(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->seeElement('body.multiSite');
    }

    /**
     * @test
     * it should see single in single env
     * @env single
     */
    public function it_should_see_single_in_single_env(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->seeElement('body.singleSite');
    }
}

and run it with different environments to test the plugin in both cases:
Running single site environment tests

Running multi site environment tests

On GitHub

The extension is part of wp-browser version 1.10.11.

Probably related

I appreciate your input