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.
Test the behaviour of the code when integrated in the system: here, WordPress
Local by Flywheel
https://localbyflywheel.com
Dependency manager for PHP
https://getcomposer.org
– Project Root
|– composer.json
|– phpunit.xml
|– tests/
|– Unit/
|– bootstrap.php
|– Integration/
|– bootstrap.php
|– 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>
<?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>
{
"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"
}
}
}
// 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
{
"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"
}
}
// Exécution des tests unitaires
composer test-unit
// Exécution des tests d'intégration
composer test-integration
Script to setup everything on Local by Flywheel (instructions included):
https://gist.github.com/keesiemeijer/a888f3d9609478b310c2d952644891ba
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/';
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);
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__)));
/**
* 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);
}
$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);
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)
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)
https://travis-ci.org/
sudo: false
language: php
branches:
only:
- master
- develop
- /branch-.*$/
cache:
directories:
- vendor
- "$HOME/.composer/cache"
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
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