Skip to content

Commit fcb0551

Browse files
committed
added phpunit
1 parent 02054bf commit fcb0551

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

bin/install-wp-tests.sh

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

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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="test-" suffix=".php">./tests/</directory>
12+
</testsuite>
13+
</testsuites>
14+
</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 file
4+
*
5+
* @package Visualizer
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__ ) ) . '/index.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';

0 commit comments

Comments
 (0)