WP ROCKET UNIT & INTEGRATION TESTS SETUP

REMINDER

REMINDER

UNIT TEST

Test return of each unit (function or method) to ensure it is the expected result in complete isolation, without any external dependent on the overall state of the system.

REMINDER

INTEGRATION TEST

Test the behaviour of the code when integrated in the system: here, WordPress

PREREQUISITE

PREREQUISITE

LOCAL DEVELOPMENT ENVIRONMENT

Local by Flywheel

https://localbyflywheel.com

PREREQUISITE

COMPOSER

Dependency manager for PHP

https://getcomposer.org

ORGANIZATION

ORGANIZATION

– Project Root

  |– composer.json

  |– phpunit.xml

  |– tests/

        |– Unit/

            |– bootstrap.php

        |– Integration/

            |– bootstrap.php

            |– phpunit.xml

ORGANIZATION

COMPOSER.JSON

  • composer configuration file
  • At the project's root
  • Define the project's  dependencies
  • Define autoloading

ORGANIZATION

TESTS DEPENDENCIES

  • PHPUnit :  Test framework for PHP (https://phpunit.de/)
  • Brain Monkey : Tool to simplify writing tests for WordPress
    (https://brain-wp.github.io/BrainMonkey/)

ORGANIZATION

PHPUNIT.XML

  • PHPUnit's configuration file
  • One file for each type of test (2 files)
  • One at the root for unit tests
  • One in the tests/Integration directory

 

ORGANIZATION

BOOTSTRAP.PHP

  • Initialization file for tests
  • Check PHP version
  • Require composer autoloader
  • And more

 

CONFIGURATION

CONFIGURATION

UNIT TEST PHPUNIT.XML

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
         bootstrap="tests/Unit/bootstrap.php">
    <testsuites>
        <testsuite name="unit">
            <directory suffix=".php">./tests/Unit/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">.</directory>
        </whitelist>
    </filter>
</phpunit>

CONFIGURATION

INTEGRATION TEST PHPUNIT.XML

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
		 bootstrap="bootstrap.php">
	<testsuites>
		<testsuite name="integration">
			<directory suffix=".php">.</directory>
		</testsuite>
	</testsuites>
</phpunit>

CONFIGURATION

COMPOSER.JSON

{
    "name": "wp-media/wp-rocket",
    "require": {
	"php": "^5.6|^7"
    },
    "require-dev": {
	"brain/monkey": "^2.2",
	"phpunit/phpunit": "~5.7.9",
    },
    "autoload-dev": {
	"psr-4": {
	    "WP_Rocket\\Tests\\Unit\\": "tests/Unit",
	    "WP_Rocket\\Tests\\Integration\\": "tests/Integration"
	}
    }
}

CONFIGURATION

SOME COMMANDS

// Initialization of composer
composer install

// Unit tests command
vendor/bin/phpunit --testsuite unit --colors=always
// Integration tests command
vendor/bin/phpunit --testsuite integration --colors=always --configuration tests/phpunit/integration/phpunit.xml

CONFIGURATION

COMPOSER.JSON (Updated)

{
    "name": "wp-media/wp-rocket",
    "require": {
	"php": "^5.6|^7"
    },
    "require-dev": {
	"brain/monkey": "^2.2",
	"phpunit/phpunit": "~5.7.9",
    },
    "autoload-dev": {
	"psr-4": {
	    "WP_Rocket\\Tests\\Unit\\": "tests/Unit",
	    "WP_Rocket\\Tests\\Integration\\": "tests/Integration"
	}
    },
    "scripts": {
        "test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always",
        "test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/phpunit/integration/phpunit.xml"
  }
}

CONFIGURATION

NEW COMMANDS

// Exécution des tests unitaires
composer test-unit
// Exécution des tests d'intégration
composer test-integration

CONFIGURATION

WordPress TEST SUITE

Script to setup everything on Local by Flywheel (instructions included):

https://gist.github.com/keesiemeijer/a888f3d9609478b310c2d952644891ba

CONFIGURATION

WordPress TEST SUITE

  • Installed Packages: PHPUnit, curl wget, rsync, git, subversion and composer
  • WordPress is installed in the /tmp/wordpress directory for PHPUnit
  • The WordPress test suite is installed in /tmp/wordpress-tests-lib
  • WordPress and test suite paths are added to the ~/.bashrc as environment variables

CONFIGURATION

UNIT TESTS BOOSTRAP (PART 1)

if (version_compare(phpversion(), '5.6.0', '<')) {
    die('WP Rocket Plugin Unit Tests require PHP 5.6 or higher.');
}

define('WP_ROCKET_PLUGIN_TESTS_ROOT', __DIR__);
define('WP_ROCKET_PLUGIN_ROOT', dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR);

$rocket_common_autoload_path = WP_ROCKET_PLUGIN_ROOT . 'vendor/';

CONFIGURATION

UNIT TESTS BOOSTRAP (PART 2)

if (! file_exists($rocket_common_autoload_path . 'autoload.php')) {
    die('Whoops, we need Composer before we start running tests.  Please type: `composer install`.  When done, try running `phpunit` again.');
}

require_once $rocket_common_autoload_path . 'autoload.php';

unset($rocket_common_autoload_path);

CONFIGURATION

INTEGRATION TESTS BOOSTRAP

if (version_compare(phpversion(), '5.6.0', '<')) {
    die('WP Rocket Plugin Integration Tests require PHP 5.6 or higher.');
}

// Define testing constants.
define('WP_ROCKET_PLUGIN_TESTS_ROOT', __DIR__);
define('WP_ROCKET_PLUGIN_ROOT', dirname(dirname(__DIR__)));

CONFIGURATION

/**
 * Gets the WP tests suite directory
 *
 * @return string
 */
function WPRocketPluginGetWPTestsDir()
{
    $tests_dir = getenv('WP_TESTS_DIR');

    // Travis CI & Vagrant SSH tests directory.
    if (empty($tests_dir)) {
        $tests_dir = '/tmp/wordpress-tests-lib';
    }
    // If the tests' includes directory does not exist, try a relative path to Core tests directory.
    if (! file_exists($tests_dir . '/includes/')) {
        $tests_dir = '../../../../tests/phpunit';
    }
    // Check it again. If it doesn't exist, stop here and post a message as to why we stopped.
    if (! file_exists($tests_dir . '/includes/')) {
        trigger_error('Unable to run the integration tests, as the WordPress test suite could not be located.', E_USER_ERROR);  // @codingStandardsIgnoreLine.
    }
    // Strip off the trailing directory separator, if it exists.
    return rtrim($tests_dir, DIRECTORY_SEPARATOR);
}

CONFIGURATION

$rocket_tests_dir = WPRocketPluginGetWPTestsDir();

// Give access to tests_add_filter() function.
require_once $rocket_tests_dir . '/includes/functions.php';

/**
 * Manually load the plugin being tested.
 */
function rocket_manually_load_plugin()
{
    require WP_ROCKET_PLUGIN_ROOT . '/wp-rocket.php';
}
tests_add_filter('muplugins_loaded', 'rocket_manually_load_plugin');

require_once $rocket_tests_dir . '/includes/bootstrap.php';

unset($rocket_tests_dir);

IN ACTION

IN ACTION

UNIT TESTS

composer test-unit
> "vendor/bin/phpunit" --testsuite unit --colors=always
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.2 with Xdebug 2.7.0RC2
Configuration: /app/public/wp-content/plugins/wp-rocket/phpunit.xml.dist

...............                                                   15 / 15 (100%)

Time: 1.48 seconds, Memory: 14.00MB

OK (15 tests, 15 assertions)

IN ACTION

INTEGRATION TESTS

composer test-integration
> "vendor/bin/phpunit" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist
Installing...
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.2 with Xdebug 2.7.0RC2
Configuration: /app/public/wp-content/plugins/wp-rocket/tests/Integration/phpunit.xml.dist

..............                                                    14 / 14 (100%)

Time: 1.13 seconds, Memory: 36.50MB

OK (14 tests, 14 assertions)

CONTINUOUS INTEGRATION

CONTINUOUS INTEGRATION

https://travis-ci.org/

  • Continuous integration tool
  • Link to the GitHub repository
  • Configuration with .travis.yml

 

CONTINUOUS INTEGRATION

.travis.yml (part 1)

sudo: false
language: php
branches:
  only:
  - master
  - develop
  - /branch-.*$/
cache:
  directories:
  - vendor
  - "$HOME/.composer/cache"

CONTINUOUS INTEGRATION

.travis.yml (part 2)

matrix:
  include:
  - php: 7.3
    env: WP_VERSION=latest
  - php: 7.2
    env: WP_VERSION=latest
  - php: 7.2
    env: WP_VERSION=4.7.13
  - php: 7.1
    env: WP_VERSION=latest
  - php: 7.0
    env: WP_VERSION=latest
  - php: 5.6
    env: WP_VERSION=latest

CONTINUOUS INTEGRATION

.travis.yml (part 3)

before_script:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- composer install --no-progress
- |
  if [[ ! -z "$WP_VERSION" ]] ; then
    bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
  fi
script:
- composer test-unit
- composer test-integration

QUESTIONS ?

WP Rocket tests setup

By Remy Perona

WP Rocket tests setup

  • 1,625