Skip to content

Commit 453dbc0

Browse files
committed
Tests: Add start of WP unit tests
1 parent bb17342 commit 453dbc0

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
language: php
2+
3+
notifications:
4+
email:
5+
on_success: never
6+
on_failure: change
7+
8+
php:
9+
- 5.3
10+
- 5.4
11+
- 5.5
12+
- 5.6
13+
- 7.0
14+
15+
env:
16+
- WP_VERSION=latest WP_MULTISITE=0
17+
18+
matrix:
19+
include:
20+
- php: 5.3
21+
env: WP_VERSION=latest WP_MULTISITE=1
22+
23+
before_script:
24+
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
25+
26+
script: phpunit

bin/install-wp-tests.sh

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

phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<phpunit
2+
addUncoveredFilesFromWhitelist="true"
3+
backupGlobals="false"
4+
bootstrap="tests/bootstrap.php"
5+
beStrictAboutOutputDuringTests="true"
6+
beStrictAboutTestsThatDoNotTestAnything="true"
7+
beStrictAboutTodoAnnotatedTests="true"
8+
checkForUnintentionallyCoveredCode="true"
9+
colors="true"
10+
convertErrorsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertWarningsToExceptions="true"
13+
forceCoversAnnotation="true"
14+
processUncoveredFilesFromWhitelist="true"
15+
showUncoveredFiles="true"
16+
verbose="true"
17+
>
18+
<testsuites>
19+
<testsuite>
20+
<directory prefix="test-" suffix=".php">./tests/</directory>
21+
</testsuite>
22+
</testsuites>
23+
</phpunit>

tests/bootstrap.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* PHPUnit bootstrap.
4+
*
5+
* @package gistpress
6+
*/
7+
8+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
9+
if ( ! $_tests_dir ) {
10+
$_tests_dir = '/tmp/wordpress-tests-lib';
11+
}
12+
13+
// Give access to tests_add_filter() function.
14+
require_once $_tests_dir . '/includes/functions.php';
15+
16+
/**
17+
* Manually load the plugin being tested.
18+
*/
19+
function _manually_load_plugin() {
20+
require dirname( dirname( __FILE__ ) ) . '/gistpress.php';
21+
}
22+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
23+
24+
// Start up the WP testing environment.
25+
require $_tests_dir . '/includes/bootstrap.php';

tests/test-sample.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Class SampleTest
4+
*
5+
* @package gistpress
6+
*/
7+
8+
/**
9+
* Sample test case.
10+
*/
11+
class SampleTest extends WP_UnitTestCase {
12+
13+
/**
14+
* A single example test.
15+
*/
16+
function test_sample() {
17+
// Replace this with some actual testing code.
18+
$this->assertTrue( true );
19+
}
20+
}
21+

0 commit comments

Comments
 (0)