Skip to content

Commit 96d6b3b

Browse files
youknowriadnylen
authored andcommitted
Setup server side unit tests (#617)
* Setup server side unit tests
1 parent e706f6e commit 96d6b3b

8 files changed

+397
-50
lines changed

.travis.yml

+64-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
1-
language: node_js
2-
node_js:
3-
- "node"
1+
sudo: false
2+
3+
language: php
4+
5+
notifications:
6+
email:
7+
on_success: never
8+
on_failure: change
9+
10+
cache:
11+
directories:
12+
- vendor
13+
- $HOME/.composer/cache
14+
15+
before_install:
16+
- nvm install 7 && nvm use 7
17+
18+
matrix:
19+
include:
20+
- php: 7.1
21+
env: WP_VERSION=latest
22+
- php: 5.6
23+
env: WP_VERSION=latest
24+
- php: 5.2
25+
env: WP_VERSION=latest
26+
- php: 5.6
27+
env: TRAVISCI=phpcs
28+
- php: 7.1
29+
env: TRAVISCI=js
30+
31+
before_script:
32+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
33+
- |
34+
if [[ ! -z "$WP_VERSION" ]] ; then
35+
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
36+
if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then
37+
mkdir -p $HOME/phpunit-bin
38+
wget -O $HOME/phpunit-bin/phpunit https://phar.phpunit.de/phpunit-4.8.phar
39+
chmod +x $HOME/phpunit-bin/phpunit
40+
export PATH=$PATH:$HOME/phpunit-bin/
41+
else
42+
composer global require "phpunit/phpunit=5.7.*"
43+
fi
44+
fi
45+
- |
46+
if [[ "$TRAVISCI" == "phpcs" ]] ; then
47+
composer global require wp-coding-standards/wpcs
48+
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
49+
fi
50+
451
script:
5-
- "npm run ci"
52+
- |
53+
if [[ ! -z "$WP_VERSION" ]] ; then
54+
phpunit
55+
WP_MULTISITE=1 phpunit
56+
fi
57+
- |
58+
if [[ "$TRAVISCI" == "phpcs" ]] ; then
59+
phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php')
60+
fi
61+
- |
62+
if [[ "$TRAVISCI" == "js" ]] ; then
63+
npm install
64+
npm run ci
65+
fi

bin/install-wp-tests.sh

+127
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)