Skip to content

Commit 8f46a67

Browse files
committed
Added phpunit
1 parent d188b9a commit 8f46a67

3 files changed

Lines changed: 212 additions & 0 deletions

File tree

phpunit/bootstrap.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* PHPUnit bootstrap file.
4+
*/
5+
6+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
7+
8+
// Check if we're installed in a src checkout.
9+
$pos = stripos( __FILE__, '/src/wp-content/plugins/' );
10+
if ( ! $_tests_dir && false !== $pos ) {
11+
$_tests_dir = substr( __FILE__, 0, $pos ) . '/tests/phpunit/';
12+
}
13+
14+
if ( ! $_tests_dir ) {
15+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
16+
}
17+
18+
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
19+
echo "Could not find $_tests_dir/includes/functions.php\n";
20+
exit( 1 );
21+
}
22+
23+
// Give access to tests_add_filter() function.
24+
require_once $_tests_dir . '/includes/functions.php';
25+
26+
function _manually_load_plugin() {
27+
require dirname( __DIR__ ) . '/where-did-they-go-from-here.php';
28+
}
29+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
30+
31+
// Include the PHPUnit Polyfills autoloader.
32+
require dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
33+
34+
// Start up the WP testing environment.
35+
require $_tests_dir . '/includes/bootstrap.php';

phpunit/install.sh

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#!/usr/bin/env bash
2+
3+
if [ $# -lt 3 ]; then
4+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
5+
exit 1
6+
fi
7+
8+
DB_NAME=$1
9+
DB_USER=$2
10+
DB_PASS=$3
11+
DB_HOST=${4-localhost}
12+
WP_VERSION=${5-latest}
13+
14+
TMPDIR=${TMPDIR-/tmp}
15+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
16+
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
17+
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}
18+
19+
RETRIES=5
20+
SLEEP=10
21+
22+
download_and_extract_wordpress_develop() {
23+
local ref=$1
24+
local archive_url=$2
25+
local dest_dir=$3
26+
27+
rm -rf "$dest_dir"
28+
mkdir -p "$dest_dir"
29+
30+
local archive_file
31+
archive_file=$(mktemp)
32+
if ! retry_run curl -sSL "$archive_url" -o "$archive_file"; then
33+
rm -f "$archive_file"
34+
return 1
35+
fi
36+
37+
if ! tar -xzf "$archive_file" -C "$dest_dir" --strip-components=1; then
38+
rm -f "$archive_file"
39+
return 1
40+
fi
41+
42+
rm -f "$archive_file"
43+
return 0
44+
}
45+
46+
# retry_run: run a command and retry on failure
47+
retry_run() {
48+
local n=0
49+
until "$@"; do
50+
n=$((n+1))
51+
if [ $n -ge $RETRIES ]; then
52+
echo "Command failed after $RETRIES attempts: $@" >&2
53+
return 1
54+
fi
55+
echo "Command failed. Retrying in $SLEEP seconds... ($n/$RETRIES)" >&2
56+
sleep $SLEEP
57+
done
58+
return 0
59+
}
60+
61+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
62+
WP_TESTS_TAG="branches/$WP_VERSION"
63+
elif [[ $WP_VERSION == 'trunk' ]]; then
64+
WP_TESTS_TAG="trunk"
65+
else
66+
# fetch the latest version with retries
67+
TMP_VER_FILE=$(mktemp)
68+
if ! retry_run curl -sSL https://api.wordpress.org/core/version-check/1.1/ -o "$TMP_VER_FILE"; then
69+
echo "Latest WordPress version could not be fetched after $RETRIES attempts" >&2
70+
rm -f "$TMP_VER_FILE"
71+
exit 1
72+
fi
73+
LATEST_VERSION=$(tail -1 "$TMP_VER_FILE")
74+
rm -f "$TMP_VER_FILE"
75+
if [[ -z "$LATEST_VERSION" ]]; then
76+
echo "Latest WordPress version could not be found"
77+
exit 1
78+
fi
79+
WP_TESTS_TAG="tags/$LATEST_VERSION"
80+
fi
81+
82+
set -ex
83+
84+
install_wp_and_test_suite() {
85+
local wp_develop_dir="$TMPDIR/wordpress-develop"
86+
local wp_develop_url=""
87+
local wp_develop_ref=""
88+
89+
if [[ $WP_TESTS_TAG == trunk ]]; then
90+
wp_develop_ref="trunk"
91+
wp_develop_url="https://codeload.github.com/WordPress/wordpress-develop/tar.gz/${wp_develop_ref}"
92+
elif [[ $WP_TESTS_TAG =~ ^branches/ ]]; then
93+
wp_develop_ref=${WP_TESTS_TAG#branches/}
94+
wp_develop_url="https://codeload.github.com/WordPress/wordpress-develop/tar.gz/${wp_develop_ref}"
95+
elif [[ $WP_TESTS_TAG =~ ^tags/ ]]; then
96+
wp_develop_ref=${WP_TESTS_TAG#tags/}
97+
wp_develop_url="https://codeload.github.com/WordPress/wordpress-develop/tar.gz/${wp_develop_ref}"
98+
fi
99+
100+
# setup up WordPress
101+
if [ ! -d "$WP_CORE_DIR" ]; then
102+
mkdir -p "$WP_CORE_DIR"
103+
if [[ -n "$wp_develop_url" ]] && download_and_extract_wordpress_develop "$wp_develop_ref" "$wp_develop_url" "$wp_develop_dir"; then
104+
rm -rf "$WP_CORE_DIR"
105+
mkdir -p "$WP_CORE_DIR"
106+
cp -R "$wp_develop_dir/src/." "$WP_CORE_DIR"
107+
else
108+
# Fallback to SVN if GitHub is unavailable.
109+
retry_run svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/src/ "$WP_CORE_DIR"
110+
fi
111+
fi
112+
113+
# set up testing suite if it doesn't yet exist
114+
if [ ! -d "$WP_TESTS_DIR" ]; then
115+
# set up testing suite
116+
mkdir -p "$WP_TESTS_DIR"
117+
if [[ -n "$wp_develop_url" ]] && [ -d "$wp_develop_dir/tests/phpunit" ]; then
118+
cp -R "$wp_develop_dir/tests/phpunit/includes" "$WP_TESTS_DIR/includes"
119+
cp -R "$wp_develop_dir/tests/phpunit/data" "$WP_TESTS_DIR/data"
120+
else
121+
# Fallback to SVN if GitHub is unavailable.
122+
retry_run svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ "$WP_TESTS_DIR/includes"
123+
retry_run svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ "$WP_TESTS_DIR/data"
124+
fi
125+
fi
126+
127+
if [ ! -f wp-tests-config.php ]; then
128+
# download wp-tests-config-sample.php with retries
129+
if [ -f "$wp_develop_dir/wp-tests-config-sample.php" ]; then
130+
cp "$wp_develop_dir/wp-tests-config-sample.php" "$WP_TESTS_DIR/wp-tests-config.php"
131+
else
132+
retry_run curl -sSL https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php -o "$WP_TESTS_DIR/wp-tests-config.php"
133+
fi
134+
# remove all forward slashes in the end
135+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
136+
sed -i "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
137+
sed -i "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
138+
sed -i "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
139+
sed -i "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
140+
sed -i "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
141+
fi
142+
143+
}
144+
145+
install_db() {
146+
# parse DB_HOST for port or socket references
147+
local PARTS=(${DB_HOST//\:/ })
148+
local DB_HOSTNAME=${PARTS[0]};
149+
local DB_SOCK_OR_PORT=${PARTS[1]};
150+
local EXTRA=""
151+
152+
if ! [ -z $DB_HOSTNAME ] ; then
153+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
154+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
155+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
156+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
157+
elif ! [ -z $DB_HOSTNAME ] ; then
158+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
159+
fi
160+
fi
161+
162+
# create database
163+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
164+
}
165+
166+
install_wp_and_test_suite
167+
install_db

phpunit/tests/test-sample.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
class SampleTest extends WP_UnitTestCase {
4+
5+
function testSample() {
6+
// replace this with some actual testing code
7+
$this->assertTrue( true );
8+
}
9+
}
10+

0 commit comments

Comments
 (0)