Skip to content

Commit

Permalink
Merge pull request #949 from Automattic/improve/phpunit-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryJones authored Jul 24, 2023
2 parents f066f70 + 04137dd commit c07ce27
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 193 deletions.
22 changes: 16 additions & 6 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ jobs:

strategy:
matrix:
php: [ '7.1', '7.2', '7.3', '7.4', '8.0' ]
wordpress: [ '5.7', '5.8', '5.9', '6.0', '6.1', '6.2' ]
wordpress: [ '5.7', '6.2' ]
php: [ '7.1', '7.4', '8.0', '8.2' ]
allowed_failure: [ false ]
include:
- php: "8.0"
# Ignore platform requirements, so that PHPUnit 7.5 can be installed on PHP 8.0 (and above).
composer-options: "--ignore-platform-reqs"
- php: '8.2'
extensions: pcov
ini-values: pcov.directory=., "pcov.exclude=\"~(vendor|tests)~\""
coverage: pcov
allowed_failure: false
- php: '8.2'
wordpress: 'trunk'
allowed_failure: true
exclude:
- php: '8.2'
wordpress: '5.7'
fail-fast: false

steps:
Expand All @@ -37,10 +42,15 @@ jobs:
ini-values: ${{ matrix.ini-values }}
coverage: ${{ matrix.coverage }}

- name: Install PHPUnit 7.x for WP < 5.9
if: ${{ matrix.wordpress < 5.9 }}
# Ignore platform requirements, so that PHPUnit 7.5 can be installed on PHP 8.0 (and above).
run: composer require --dev phpunit/phpunit:"^7.5" --ignore-platform-req=php+ --no-update --no-scripts --no-interaction

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
composer-options: "${{ matrix.composer-options }}"
composer-options: --ignore-platform-req=php+

- name: Set up problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/node_modules/
/vendor/
/.phpcs.xml
/.phpunit.result.cache
/composer.lock
/phpcs.xml
/phpunit.xml
Expand Down
107 changes: 88 additions & 19 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
exit 1
fi

Expand All @@ -10,9 +10,12 @@ DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-false}

WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}

download() {
if [ `which curl` ]; then
Expand All @@ -22,8 +25,21 @@ download() {
fi
}

if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
WP_TESTS_TAG="tags/$WP_VERSION"
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
WP_BRANCH=${WP_VERSION%\-*}
WP_TESTS_TAG="branches/$WP_BRANCH"

elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
WP_TESTS_TAG="tags/${WP_VERSION%??}"
else
WP_TESTS_TAG="tags/$WP_VERSION"
fi
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
Expand All @@ -35,7 +51,6 @@ else
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {
Expand All @@ -46,22 +61,44 @@ install_wp() {

mkdir -p $WP_CORE_DIR

if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p $TMPDIR/wordpress-trunk
rm -rf $TMPDIR/wordpress-trunk/*
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
# https serves multiple offers, whereas http serves single.
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
LATEST_VERSION=${WP_VERSION%??}
else
# otherwise, scan the releases and get the most up to date minor version of the major release
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
fi
if [[ -z "$LATEST_VERSION" ]]; then
local ARCHIVE_NAME="wordpress-$WP_VERSION"
else
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
fi
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
local ioption='-i .bak'
local ioption='-i.bak'
else
local ioption='-i'
fi
Expand All @@ -70,14 +107,17 @@ install_test_suite() {
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
rm -rf $WP_TESTS_DIR/{includes,data}
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

cd $WP_TESTS_DIR

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
Expand All @@ -86,7 +126,29 @@ install_test_suite() {

}

recreate_db() {
shopt -s nocasematch
if [[ $1 =~ ^(y|yes)$ ]]
then
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
create_db
echo "Recreated the database ($DB_NAME)."
else
echo "Leaving the existing database ($DB_NAME) in place."
fi
shopt -u nocasematch
}

create_db() {
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
return 0
fi

# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
Expand All @@ -104,7 +166,14 @@ install_db() {
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
then
echo "Reinstalling will delete the existing test database ($DB_NAME)"
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
recreate_db $DELETE_EXISTING_DB
else
create_db
fi
}

install_wp
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
"require-dev": {
"automattic/vipwpcs": "^2.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7",
"dms/phpunit-arraysubset-asserts": "^0.5.0",
"php-parallel-lint/php-parallel-lint": "^1.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"phpunit/phpunit": "^4 || ^5 || ^6 || ^7",
"phpunit/phpunit": "^5 || ^6 || ^7 || ^8 || ^9",
"squizlabs/php_codesniffer": "^3.5",
"wp-coding-standards/wpcs": "^2.3.0",
"yoast/phpunit-polyfills": "^1.0.1"
"yoast/wp-test-utils": "^1.1"
},
"autoload": {
"classmap": [
Expand All @@ -57,11 +58,11 @@
"bash bin/install-wp-tests.sh wordpress_test root root localhost"
],
"integration": [
"@php ./vendor/bin/phpunit --testsuite WP_Tests"
"@php ./vendor/bin/phpunit --exclude=ms-required"
],
"integration-ms": [
"@putenv WP_MULTISITE=1",
"@composer integration"
"@php ./vendor/bin/phpunit --exclude=ms-excluded"
]
},
"config": {
Expand Down
3 changes: 2 additions & 1 deletion php/class-coauthors-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public function get_coauthors_search_results( $request ) {
$response = array();

$search = strtolower( $request->get_param( 'q' ) );
$ignore = explode( ',', $request->get_param( 'existing_authors' ) );
$ignorable = null === $request->get_param( 'existing_authors' ) ? '' : $request->get_param( 'existing_authors' );
$ignore = explode( ',', $ignorable );
$authors = $this->coauthors->search_authors( $search, $ignore );

if ( ! empty( $authors ) ) {
Expand Down
3 changes: 0 additions & 3 deletions php/class-coauthors-guest-authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ function __construct() {
);
register_post_type( $this->post_type, $args );

// Some of the common sizes used by get_avatar
$this->avatar_sizes = array();

// Hacky way to remove the title and the editor
remove_post_type_support( $this->post_type, 'title' );
remove_post_type_support( $this->post_type, 'editor' );
Expand Down
14 changes: 8 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<const name="WP_TESTS_MULTISITE" value="1" />
</php>
convertNoticesToExceptions="true"
convertDeprecationsToExceptions="true"
>
<testsuites>
<testsuite name="WP_Tests">
<testsuite name="default">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
51 changes: 44 additions & 7 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
<?php
/**
* PHPUnit bootstrap file.
*
* @package Automattic\LegacyRedirector
*/

use Yoast\WPTestUtils\WPIntegration;

require_once dirname( dirname( __FILE__ ) ) . '/vendor/yoast/wp-test-utils/src/WPIntegration/bootstrap-functions.php';

$_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

// Load the composer autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
}

require_once $_tests_dir . '/includes/functions.php';
if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) {
echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}

// Give access to tests_add_filter() function.
require_once "{$_tests_dir}/includes/functions.php";

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../co-authors-plus.php';
// Updated from default (__FILE__), since this bootstrap is an extra level down in tests/Integration/.
require dirname( dirname( __FILE__ ) ) . '/co-authors-plus.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';
// Make sure the Composer autoload file has been generated.
WPIntegration\check_composer_autoload_exists();

// Start up the WP testing environment.
require "{$_tests_dir}/includes/bootstrap.php";

/*
* Register the custom autoloader to overload the PHPUnit MockObject classes when running on PHP 8.
*
* This function has to be called _last_, after the WP test bootstrap to make sure it registers
* itself in FRONT of the Composer autoload (which also prepends itself to the autoload queue).
*/
WPIntegration\register_mockobject_autoloader();

require dirname( __FILE__ ) . '/coauthorsplus-testcase.php';
// Add custom test case.
require __DIR__ . '/coauthorsplus-testcase.php';
16 changes: 13 additions & 3 deletions tests/coauthorsplus-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
/**
* Base unit test class for Co-Authors Plus
*/
class CoAuthorsPlus_TestCase extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
class CoAuthorsPlus_TestCase extends \Yoast\WPTestUtils\WPIntegration\TestCase {

/**
* @var CoAuthors_Plus
*/
protected $_cap;
/**
* @var Endpoints
*/
protected $_api;

public function set_up() {
parent::set_up();

global $coauthors_plus;
$this->_cap = $coauthors_plus;
Expand Down
Loading

0 comments on commit c07ce27

Please sign in to comment.