Skip to content

Commit 57fb181

Browse files
committed
Fix tests
1 parent 3cc8036 commit 57fb181

File tree

6 files changed

+290
-46
lines changed

6 files changed

+290
-46
lines changed

.phpcs.xml.dist

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WordPress Coding Standards based custom ruleset for your plugin">
3+
<description>Generally-applicable sniffs for WordPress plugins.</description>
4+
5+
<!-- What to scan -->
6+
<file>.</file>
7+
<exclude-pattern>/vendor/</exclude-pattern>
8+
<exclude-pattern>/node_modules/</exclude-pattern>
9+
10+
<!-- How to scan -->
11+
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
12+
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
13+
<arg value="sp"/> <!-- Show sniff and progress -->
14+
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
15+
<arg name="colors"/>
16+
<arg name="extensions" value="php"/>
17+
<arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. -->
18+
19+
<!-- Rules: Check PHP version compatibility -->
20+
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
21+
<config name="testVersion" value="5.6-"/>
22+
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
23+
<rule ref="PHPCompatibilityWP"/>
24+
25+
<!-- Rules: WordPress Coding Standards -->
26+
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
27+
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
28+
<config name="minimum_supported_wp_version" value="4.6"/>
29+
<rule ref="WordPress">
30+
<exclude name="WordPress.VIP"/>
31+
</rule>
32+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
33+
<properties>
34+
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
35+
<property name="prefixes" type="array" value="oauth2"/>
36+
</properties>
37+
</rule>
38+
<rule ref="WordPress.WP.I18n">
39+
<properties>
40+
<!-- Value: replace the text domain used. -->
41+
<property name="text_domain" type="array" value="oauth2"/>
42+
</properties>
43+
</rule>
44+
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">
45+
<properties>
46+
<property name="blank_line_check" value="true"/>
47+
</properties>
48+
</rule>
49+
</ruleset>

.travis.yml

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,62 @@
1+
sudo: false
2+
dist: trusty
3+
14
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
5+
6+
notifications:
7+
email:
8+
on_success: never
9+
on_failure: change
10+
11+
branches:
12+
only:
13+
- master
14+
15+
cache:
16+
directories:
17+
- $HOME/.composer/cache
18+
19+
matrix:
20+
include:
21+
- php: 7.2
22+
env: WP_VERSION=latest
23+
- php: 7.1
24+
env: WP_VERSION=latest
25+
- php: 7.0
26+
env: WP_VERSION=latest
27+
- php: 5.6
28+
env: WP_VERSION=latest
29+
- php: 5.6
30+
env: WP_VERSION=trunk
31+
- php: 5.6
32+
env: WP_TRAVISCI=phpcs
33+
34+
before_script:
35+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
36+
- |
37+
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
38+
phpenv config-rm xdebug.ini
39+
else
40+
echo "xdebug.ini does not exist"
41+
fi
42+
- |
43+
if [[ ! -z "$WP_VERSION" ]] ; then
44+
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
45+
composer global require "phpunit/phpunit=4.8.*|5.7.*"
46+
fi
47+
- |
48+
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
49+
composer global require wp-coding-standards/wpcs
50+
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
51+
fi
52+
753
script:
8-
- vendor/bin/phpcs --standard=phpcs.ruleset.xml .
9-
- phpunit
54+
- |
55+
if [[ ! -z "$WP_VERSION" ]] ; then
56+
phpunit
57+
WP_MULTISITE=1 phpunit
58+
fi
59+
- |
60+
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
61+
phpcs
62+
fi

bin/install-wp-tests.sh

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

phpcs.ruleset.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

phpunit.xml.dist

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0"?>
12
<phpunit
23
bootstrap="tests/bootstrap.php"
34
backupGlobals="false"
@@ -8,16 +9,8 @@
89
>
910
<testsuites>
1011
<testsuite>
11-
<directory prefix="class-" suffix=".php">tests</directory>
12+
<directory prefix="test-" suffix=".php">./tests/</directory>
13+
<exclude>./tests/test-sample.php</exclude>
1214
</testsuite>
1315
</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>
2316
</phpunit>

tests/bootstrap.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
<?php
22
/**
3-
* Bootstrap the plugin unit testing environment.
3+
* PHPUnit bootstrap file
44
*
5-
* @package WordPress
6-
* @subpackage JSON API
7-
*/
5+
* @package Oauth2
6+
*/
87

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';
8+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
9+
10+
if ( ! $_tests_dir ) {
11+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
12+
}
13+
14+
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
15+
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // WPCS: XSS ok.
16+
exit( 1 );
1917
}
2018

21-
require $test_root . '/includes/functions.php';
19+
// Give access to tests_add_filter() function.
20+
require_once $_tests_dir . '/includes/functions.php';
2221

23-
tests_add_filter(
24-
'muplugins_loaded',
25-
function () {
26-
require dirname( __DIR__ ) . '/plugin.php';
27-
}
28-
);
22+
/**
23+
* Manually load the plugin being tested.
24+
*/
25+
function _manually_load_plugin() {
26+
require dirname( dirname( __FILE__ ) ) . '/oauth2.php';
27+
}
28+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
2929

30-
require $test_root . '/includes/bootstrap.php';
30+
// Start up the WP testing environment.
31+
require $_tests_dir . '/includes/bootstrap.php';

0 commit comments

Comments
 (0)