Skip to content

Commit 46aafd1

Browse files
committed
Add basic Travis setup
1 parent d046fb8 commit 46aafd1

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: php
2+
php:
3+
- '7.1'
4+
install:
5+
- composer install
6+
- bash tests/install-tests.sh wordpress_test root '' 127.0.0.1 latest
7+
script:
8+
- phpunit

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<phpunit
2+
bootstrap="tests/bootstrap.php"
3+
backupGlobals="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
>
9+
<testsuites>
10+
<testsuite>
11+
<directory prefix="class-" suffix=".php">tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<filter>
15+
<blacklist>
16+
<directory suffix=".php">.</directory>
17+
</blacklist>
18+
<whitelist>
19+
<directory suffix=".php">./inc</directory>
20+
<file>./plugin.php</file>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

tests/bootstrap.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Bootstrap the plugin unit testing environment.
4+
*
5+
* @package WordPress
6+
* @subpackage JSON API
7+
*/
8+
9+
// Support for:
10+
// 1. `WP_DEVELOP_DIR` environment variable
11+
// 2. Plugin installed inside of WordPress.org developer checkout
12+
// 3. Tests checked out to /tmp
13+
if ( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
14+
$test_root = getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit';
15+
} elseif ( file_exists( '../../../../tests/phpunit/includes/bootstrap.php' ) ) {
16+
$test_root = '../../../../tests/phpunit';
17+
} elseif ( file_exists( '/tmp/wordpress-tests-lib/includes/bootstrap.php' ) ) {
18+
$test_root = '/tmp/wordpress-tests-lib';
19+
}
20+
21+
require $test_root . '/includes/functions.php';
22+
23+
tests_add_filter( 'muplugins_loaded', function () {
24+
require dirname( __DIR__ ) . '/plugin.php';
25+
});
26+
27+
require $test_root . '/includes/bootstrap.php';

tests/install-tests.sh

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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] [skip-database-creation]"
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+
SKIP_DB_CREATE=${6-false}
14+
15+
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
16+
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
17+
18+
download() {
19+
if [ `which curl` ]; then
20+
curl -s "$1" > "$2";
21+
elif [ `which wget` ]; then
22+
wget -nv -O "$2" "$1"
23+
fi
24+
}
25+
26+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
27+
WP_TESTS_TAG="tags/$WP_VERSION"
28+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
29+
WP_TESTS_TAG="trunk"
30+
else
31+
# http serves a single offer, whereas https serves multiple. we only want one
32+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
33+
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
34+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
35+
if [[ -z "$LATEST_VERSION" ]]; then
36+
echo "Latest WordPress version could not be found"
37+
exit 1
38+
fi
39+
WP_TESTS_TAG="tags/$LATEST_VERSION"
40+
fi
41+
42+
set -ex
43+
44+
install_wp() {
45+
46+
if [ -d $WP_CORE_DIR ]; then
47+
return;
48+
fi
49+
50+
mkdir -p $WP_CORE_DIR
51+
52+
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
53+
mkdir -p /tmp/wordpress-nightly
54+
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
55+
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
56+
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
57+
else
58+
if [ $WP_VERSION == 'latest' ]; then
59+
local ARCHIVE_NAME='latest'
60+
else
61+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
62+
fi
63+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
64+
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
65+
fi
66+
67+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
68+
}
69+
70+
install_test_suite() {
71+
# portable in-place argument for both GNU sed and Mac OSX sed
72+
if [[ $(uname -s) == 'Darwin' ]]; then
73+
local ioption='-i .bak'
74+
else
75+
local ioption='-i'
76+
fi
77+
78+
# set up testing suite if it doesn't yet exist
79+
if [ ! -d $WP_TESTS_DIR ]; then
80+
# set up testing suite
81+
mkdir -p $WP_TESTS_DIR
82+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
83+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
84+
fi
85+
86+
if [ ! -f wp-tests-config.php ]; then
87+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
88+
# remove all forward slashes in the end
89+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
90+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
91+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
92+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
93+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
94+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
95+
fi
96+
97+
}
98+
99+
install_db() {
100+
101+
if [ ${SKIP_DB_CREATE} = "true" ]; then
102+
return 0
103+
fi
104+
105+
# parse DB_HOST for port or socket references
106+
local PARTS=(${DB_HOST//\:/ })
107+
local DB_HOSTNAME=${PARTS[0]};
108+
local DB_SOCK_OR_PORT=${PARTS[1]};
109+
local EXTRA=""
110+
111+
if ! [ -z $DB_HOSTNAME ] ; then
112+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
113+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
114+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
115+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
116+
elif ! [ -z $DB_HOSTNAME ] ; then
117+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
118+
fi
119+
fi
120+
121+
# create database
122+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
123+
}
124+
125+
install_wp
126+
install_test_suite
127+
install_db

0 commit comments

Comments
 (0)