Skip to content

Commit

Permalink
Update testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
tlovett1 committed Nov 26, 2017
1 parent 82a0dc2 commit 83c3cac
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 37 deletions.
59 changes: 49 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
sudo: false

language: php

php:
- 5.4
- 7.1
notifications:
email:
on_success: never
on_failure: change

branches:
only:
- master

cache:
directories:
- vendor
- $HOME/.composer/cache

env:
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=1
- WP_VERSION=4.4 WP_MULTISITE=0
- WP_VERSION=4.4 WP_MULTISITE=1
matrix:
include:
- php: 7.1
env: WP_VERSION=latest
- php: 7.1
env: WP_VERSION=4.4
- php: 5.3
env: WP_VERSION=latest
- php: 5.3
env: WP_VERSION=4.4

before_script:
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [[ ! -z "$WP_VERSION" ]] ; then
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then
composer global require "phpunit/phpunit=4.8.*"
else
composer global require "phpunit/phpunit=5.7.*"
fi
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
composer global require wp-coding-standards/wpcs
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
fi
script: phpunit
script:
- |
if [[ ! -z "$WP_VERSION" ]] ; then
phpunit
WP_MULTISITE=1 phpunit
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php')
fi
35 changes: 25 additions & 10 deletions bin/install-wp-tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
exit 1
fi

Expand All @@ -10,6 +10,7 @@ DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-false}

WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
Expand All @@ -24,6 +25,8 @@ download() {

if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
WP_TESTS_TAG="tags/$WP_VERSION"
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
Expand All @@ -46,15 +49,21 @@ install_wp() {

mkdir -p $WP_CORE_DIR

if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p /tmp/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

Expand All @@ -71,13 +80,14 @@ install_test_suite() {
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

cd $WP_TESTS_DIR

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
Expand All @@ -87,6 +97,11 @@ install_test_suite() {
}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
return 0
fi

# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
Expand Down
10 changes: 10 additions & 0 deletions phpcs.ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
<description>Generally-applicable sniffs for WordPress plugins</description>

<rule ref="WordPress-Core" />
<rule ref="WordPress-Docs" />

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
</ruleset>
24 changes: 12 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
19 changes: 14 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package Safe_Redirect_Manager
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}

define( 'PHPUNIT_SRM_TESTSUITE', 1 );
define( 'PHPUNIT_SRM_TESTSUITE', true );

require_once( $_tests_dir . '/includes/functions.php' );
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../safe-redirect-manager.php';
require dirname( dirname( __FILE__ ) ) . '/safe-redirect-manager.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require( $_tests_dir . '/includes/bootstrap.php' );

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
20 changes: 20 additions & 0 deletions tests/test-sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Class SampleTest
*
* @package Safe_Redirect_Manager
*/

/**
* Sample test case.
*/
class SampleTest extends WP_UnitTestCase {

/**
* A single example test.
*/
function test_sample() {
// Replace this with some actual testing code.
$this->assertTrue( true );
}
}

0 comments on commit 83c3cac

Please sign in to comment.