Skip to content

Commit f69f5df

Browse files
authored
Merge pull request #839 from Codeinwp/fix-local-tests
Fix local PHP tests
2 parents dad4b3b + 7b7a032 commit f69f5df

File tree

6 files changed

+138
-42
lines changed

6 files changed

+138
-42
lines changed

.github/workflows/test-php.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ jobs:
4444
php-version: ${{ matrix.php-version }}
4545
extensions: simplexml, mysql
4646
tools: phpunit-polyfills:1.1
47+
- name: Install Subversion
48+
run: sudo apt-get update && sudo apt-get install -y subversion
4749
- name: Checkout source code
4850
uses: actions/checkout@v4
4951
- name: Install WordPress Test Suite
50-
run: bash bin/install-wp-tests.sh wordpress_tests root root 127.0.0.1 latest true
52+
run: composer install-wp-tests
5153
- name: Install Composer dependencies
52-
run: composer install --prefer-dist --no-progress --no-suggest --no-dev
54+
run: composer install --prefer-dist --no-progress --no-suggest
5355
- name: Run phpunit
54-
run: phpunit
56+
run: composer phpunit
5557
phpstan:
5658
name: PHPStan on PHP 8.0
5759
runs-on: ubuntu-latest

bin/install-wp-tests.sh

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,43 @@ DB_HOST=${4-localhost}
1212
WP_VERSION=${5-latest}
1313
SKIP_DB_CREATE=${6-false}
1414

15-
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
16-
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
15+
TMPDIR=${TMPDIR-/tmp}
16+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
17+
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
18+
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}
1719

18-
command_exists() {
19-
type -t "$1" >/dev/null 2>&1
20-
}
2120
download() {
22-
if command_exists "curl"; then
23-
curl -s -o "${2:--}" "$1"
24-
elif command_exists "wget"; then
25-
wget -nv -O "${2:--}" "$1"
21+
if [ `which curl` ]; then
22+
curl -s "$1" > "$2";
23+
elif [ `which wget` ]; then
24+
wget -nv -O "$2" "$1"
25+
else
26+
echo "Error: Neither curl nor wget is installed."
27+
exit 1
2628
fi
2729
}
28-
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
29-
WP_TESTS_TAG="tags/$WP_VERSION"
30+
31+
# Check if svn is installed
32+
check_svn_installed() {
33+
if ! command -v svn > /dev/null; then
34+
echo "Error: svn is not installed. Please install svn and try again."
35+
exit 1
36+
fi
37+
}
38+
39+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
40+
WP_BRANCH=${WP_VERSION%\-*}
41+
WP_TESTS_TAG="branches/$WP_BRANCH"
42+
43+
elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
44+
WP_TESTS_TAG="branches/$WP_VERSION"
45+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
46+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
47+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
48+
WP_TESTS_TAG="tags/${WP_VERSION%??}"
49+
else
50+
WP_TESTS_TAG="tags/$WP_VERSION"
51+
fi
3052
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
3153
WP_TESTS_TAG="trunk"
3254
else
@@ -40,10 +62,8 @@ else
4062
fi
4163
WP_TESTS_TAG="tags/$LATEST_VERSION"
4264
fi
43-
4465
set -ex
4566

46-
4767
install_wp() {
4868

4969
if [ -d $WP_CORE_DIR ]; then
@@ -53,27 +73,44 @@ install_wp() {
5373
mkdir -p $WP_CORE_DIR
5474

5575
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
56-
mkdir -p /tmp/wordpress-nightly
57-
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
58-
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
59-
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
76+
mkdir -p $TMPDIR/wordpress-trunk
77+
rm -rf $TMPDIR/wordpress-trunk/*
78+
check_svn_installed
79+
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
80+
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
6081
else
6182
if [ $WP_VERSION == 'latest' ]; then
6283
local ARCHIVE_NAME='latest'
84+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
85+
# https serves multiple offers, whereas http serves single.
86+
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
87+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
88+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
89+
LATEST_VERSION=${WP_VERSION%??}
90+
else
91+
# otherwise, scan the releases and get the most up to date minor version of the major release
92+
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
93+
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
94+
fi
95+
if [[ -z "$LATEST_VERSION" ]]; then
96+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
97+
else
98+
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
99+
fi
63100
else
64101
local ARCHIVE_NAME="wordpress-$WP_VERSION"
65102
fi
66-
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
67-
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
103+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
104+
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
68105
fi
69106

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

73110
install_test_suite() {
74111
# portable in-place argument for both GNU sed and Mac OSX sed
75112
if [[ $(uname -s) == 'Darwin' ]]; then
76-
local ioption='-i .bak'
113+
local ioption='-i.bak'
77114
else
78115
local ioption='-i'
79116
fi
@@ -82,15 +119,18 @@ install_test_suite() {
82119
if [ ! -d $WP_TESTS_DIR ]; then
83120
# set up testing suite
84121
mkdir -p $WP_TESTS_DIR
85-
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
86-
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
122+
rm -rf $WP_TESTS_DIR/{includes,data}
123+
check_svn_installed
124+
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
125+
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
87126
fi
88127

89128
if [ ! -f wp-tests-config.php ]; then
90129
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
91130
# remove all forward slashes in the end
92131
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
93132
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
133+
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
94134
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
95135
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
96136
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
@@ -99,6 +139,23 @@ install_test_suite() {
99139

100140
}
101141

142+
recreate_db() {
143+
shopt -s nocasematch
144+
if [[ $1 =~ ^(y|yes)$ ]]
145+
then
146+
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
147+
create_db
148+
echo "Recreated the database ($DB_NAME)."
149+
else
150+
echo "Leaving the existing database ($DB_NAME) in place."
151+
fi
152+
shopt -u nocasematch
153+
}
154+
155+
create_db() {
156+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
157+
}
158+
102159
install_db() {
103160

104161
if [ ${SKIP_DB_CREATE} = "true" ]; then
@@ -122,7 +179,14 @@ install_db() {
122179
fi
123180

124181
# create database
125-
mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute "CREATE DATABASE IF NOT EXISTS $DB_NAME;"
182+
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
183+
then
184+
echo "Reinstalling will delete the existing test database ($DB_NAME)"
185+
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
186+
recreate_db $DELETE_EXISTING_DB
187+
else
188+
create_db
189+
fi
126190
}
127191

128192
install_wp

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
"scripts": {
4848
"format": "phpcbf --standard=phpcs.xml --report-summary --report-source",
4949
"phpcs": "phpcs --standard=phpcs.xml --extensions=php -s -d memory_limit=-1",
50-
"test": "[ -z \"$WP_TESTS_DIR\" ] || [ -z \"$WP_CORE_DIR\" ] && echo 'Error: Both WP_TESTS_DIR and WP_CORE_DIR must be defined.' && exit 1 || phpunit --configuration=phpunit.xml",
5150
"phpstan": "phpstan analyse --memory-limit=-1",
52-
"install-wp-tests": "bash bin/install-wp-tests.sh wordpress_test root '' localhost latest"
51+
"phpunit": "phpunit --configuration=phpunit.xml",
52+
"install-wp-tests": "bash bin/install-wp-tests.sh wordpress_tests root root 127.0.0.1 latest true"
5353
},
5454
"require": {
5555
"php": ">=7.4",

development.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44
*
55
* @package ThemeIsle
66
*/
7-
if ( ! empty( getenv( 'WP_TESTS_DIR' ) ) ) {
7+
8+
// This condition tries to detect if we're running PHPUnit. This is complex because the file is loaded via the
9+
// composer autoloader. You can detect that 99% of the time because PHPUnit uses the constant
10+
// PHPUNIT_COMPOSER_INSTALL to load the autoloader. That said, if you run a test in a separate process, it won't
11+
// run that code again and the constant doesn't exist. As a fallback, we use the PHPUNIT_RUNNING environment variable
12+
// defined in the phpunit.xml to detect if we're in a PHPUnit process. We can't always use the environment variable
13+
// because by default PHPUnit loads the composer autoloader before it assigns those environment variables. That's
14+
// why both conditions are needed.
15+
if ( defined( 'PHPUNIT_COMPOSER_INSTALL' ) || getenv( 'PHPUNIT_RUNNING' ) ) {
816
return;
917
}
18+
1019
if ( ! defined( 'ENABLE_OPTIMOLE_WP_DEV' ) ) {
1120
define( 'ENABLE_OPTIMOLE_WP_DEV', true );
1221
}
22+
1323
if ( ENABLE_OPTIMOLE_WP_DEV ) {
1424
$optiml_constants = [
1525
'OPTIML_API_ROOT' => 'https://staging-dashboard.optimole.com/api/',

phpunit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
convertNoticesToExceptions="true"
77
convertWarningsToExceptions="true"
88
>
9+
<php>
10+
<env name="PHPUNIT_RUNNING" value="1"/>
11+
</php>
12+
913
<testsuites>
1014
<testsuite name="General Unit tests ( Requires PHP 5.4) " >
1115
<directory phpVersion="5.4.0" phpVersionOperator=">=" prefix="test-" suffix=".php">./tests/</directory>

tests/bootstrap.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
<?php
22
/**
33
* PHPUnit bootstrap file
4-
*
5-
* @package eyepatch-manager
64
*/
75

8-
if ( getenv( 'WP_LOCAL_TESTING' ) === 'true' ) {
9-
require dirname(dirname(__FILE__)) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
10-
}
11-
126
$_tests_dir = getenv( 'WP_TESTS_DIR' );
7+
138
if ( ! $_tests_dir ) {
14-
$_tests_dir = '/tmp/wordpress-tests-lib';
9+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
10+
}
11+
12+
// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
13+
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
14+
if ( false !== $_phpunit_polyfills_path ) {
15+
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
16+
}
17+
18+
if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) {
19+
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
20+
exit( 1 );
1521
}
1622

17-
/**
18-
* The path to the main file of the plugin to test.
19-
*/
2023
define( 'WP_USE_THEMES', false );
2124
define( 'WP_TESTS_FORCE_KNOWN_BUGS', true );
2225
define( 'OPTML_PHPUNIT_TESTING', true );
2326
// Give access to tests_add_filter() function.
24-
require_once $_tests_dir . '/includes/functions.php';
25-
27+
require_once "{$_tests_dir}/includes/functions.php";
2628

2729
/**
2830
* Manually load the plugin being tested.
@@ -67,10 +69,24 @@ function( $message ) {
6769
}
6870

6971
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
72+
7073
// Start up the WP testing environment.
7174
require $_tests_dir . '/includes/bootstrap.php';
75+
76+
// If PHPUnit is run from the project directory, it loads the themeisle SDK via the composer autoload file at the
77+
// beginning of the script execution. This is too early because the SDK needs WordPress to be loaded. This code checks
78+
// if the SDK was actually loaded and forces it to load again if it wasn't loaded properly.
79+
if ( ! function_exists( 'tsdk_translate_link' ) ) {
80+
require __DIR__ . '/../vendor/codeinwp/themeisle-sdk/load.php';
81+
}
82+
83+
// Activate Optimole plugin
7284
activate_plugin( 'optimole-wp/optimole-wp.php' );
85+
86+
// Set up the current logged in user
7387
global $current_user;
88+
7489
$current_user = new WP_User( 1 );
7590
$current_user->set_role( 'administrator' );
91+
7692
wp_update_user( array( 'ID' => 1, 'first_name' => 'Admin', 'last_name' => 'User' ) );

0 commit comments

Comments
 (0)