Skip to content

Instantly share code, notes, and snippets.

@nacin
Created July 10, 2012 16:44
Show Gist options
  • Save nacin/3084568 to your computer and use it in GitHub Desktop.
Save nacin/3084568 to your computer and use it in GitHub Desktop.
Nacin's WP Test Wrapper
function wptest() {
TESTROOT=/Users/nacin/Sites/tests
ARGS="--colors $@"
FORCE=0
DEBUG=0
REPLACE=$(echo $ARGS | sed -e 's/ -f\>//')
if [ "$ARGS" != "$REPLACE" ]; then
FORCE=1
ARGS=$REPLACE
fi
REPLACE=$(echo $ARGS | sed -e 's/ -d\>//')
if [ "$ARGS" != "$REPLACE" ]; then
DEBUG=1
ARGS=$REPLACE
fi
CWD=$(pwd)
# Find a WordPress install nearby. It's either inside /wordpress, at our level, or up a level or two.
if [ -f $CWD/wp-settings.php ]; then
ABSPATH=$CWD/
elif [ -f $CWD/wordpress/wp-settings.php ]; then
ABSPATH=$CWD/wordpress/
elif [ -f $CWD/../wp-settings.php ]; then
ABSPATH=$CWD/../
elif [ -f $CWD/../../wp-settings.php ]; then
ABSPATH=$CWD/../../
else
ABSPATH=$TESTROOT/wordpress/
fi
# Force a fresh DB install as we're doing our own config.
rm -f $ABSPATH/.wp-tests-version
# Generate some config flags
echo "<?php" > $TESTROOT/.wp-tests-run
echo "define('ABSPATH', '$ABSPATH');" >> $TESTROOT/.wp-tests-run
if [ $DEBUG -gt 0 ]; then
echo "define('WP_DEBUG', true);" >> $TESTROOT/.wp-tests-run
fi
if [ $FORCE -gt 0 ]; then
echo "define('WP_TESTS_FORCE_KNOWN_BUGS', true);" >> $TESTROOT/.wp-tests-run
fi
cat $TESTROOT/.wp-tests-run
# Switch to the test root and run phpunit
cd $TESTROOT
phpunit $ARGS
# We'll again need to force a fresh DB install to test multisite.
rm -f $ABSPATH/.wp-tests-version
echo "define('WP_TESTS_MULTISITE', true);" >> $TESTROOT/.wp-tests-run
echo -e "\nRunning the tests under multisite…\n"
phpunit $ARGS
# Remove our config; force a new install next time.
rm -f $TESTROOT/.wp-tests-run
rm -f $ABSPATH/.wp-tests-version
# Return to whence we came.
cd $CWD
}
<?php
# What the top of my wp-tests-config.php file looks like:
$dir = dirname( __FILE__ );
if ( file_exists( $dir . '/.wp-tests-run' ) ) {
include $dir . '/.wp-tests-run';
} else {
define( 'ABSPATH', $dir . '/wordpress/' );
// Test with multisite enabled: (previously -m)
// define( 'WP_TESTS_MULTISITE', true );
// Force known bugs: (previously -f)
// define( 'WP_TESTS_FORCE_KNOWN_BUGS', true );
// Test with WordPress debug mode on (previously -d)
// define( 'WP_DEBUG', true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment