Skip to content

Instantly share code, notes, and snippets.

@mindctrl
Forked from keesiemeijer/setup-phpunit.sh
Created January 29, 2019 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindctrl/8dde3b5ee689806d2c3357ffd912346a to your computer and use it in GitHub Desktop.
Save mindctrl/8dde3b5ee689806d2c3357ffd912346a to your computer and use it in GitHub Desktop.
Setup PHPUnit for use in the Local by Flywheel app
#!/usr/bin/env bash
# ===============================================================================
# Script to install PHPUnit in the Local by Flywheel Mac app
# These packages are installed
#
# PHPUnit, git, subversion, composer, curl and wget
#
# The $WP_CORE_DIR and $WP_TESTS_DIR environment variables are added to the ~/.bashrc file
#
# These directories are used by plugins that have their unit tests scaffolded by WP-CLI.
# VVV also adds these as enviroment variables by default.
#
# WordPress and the WP_UnitTestCase are installed in these directories for use by PHPUnit.
# A database is created for WordPress with these credentials:
# db_name: wordpress_test
# db_user: root
# db_pass: root
#
# You only have to run this script once. PHPUnit (and the other packages)
# are still available next time you ssh into your site.
#
# To update packages, WordPress and the WP_UnitTestCase re-run this script.
#
# Note: This script doesn't install the packages globally in the Local by Flywheel app
# Packages are only installed for the site where you've run this script.
# ===============================================================================
# ===============================================================================
# Instructions
#
# 1 - Download this file (setup-phpunit.sh) inside your site's /app folder
# curl -o setup-phpunit.sh https://gist.githubusercontent.com/keesiemeijer/a888f3d9609478b310c2d952644891ba/raw/
#
# 2 - Right click your site in the Local App and click Open Site SSH
# A new terminal window will open
#
# 3 - Go to your site's /app folder:
# cd /app
#
# 4 - Run this script
# bash setup-phpunit.sh
#
# 5 - Reload the .bashrc file
# source ~/.bashrc
# ===============================================================================
# ===============================================================================
# PHPUnit version
#
# By default PHPUnit version 5.7.27 is installed (or 4.8.36 for PHP versions < 5.6)
# See ticket https://core.trac.wordpress.org/ticket/39822
#
# Run the command with a version if you need to test with a specific PHPUnit version
#
# bash setup-phpunit.sh 6
#
# This example will install the latest PHPUnit from version 6 (e.g. 6.4.4)
# Available versions can be found here: https://phar.phpunit.de
#
# ===============================================================================
printf "Installing packages...\n"
# Strings used in error messages
readonly QUIT="Stopping script..."
readonly CONNECTION="Make sure you're connected to the internet"
readonly RED='\033[0;31m' # Red color
readonly RESET='\033[0m' # No color
# Re-synchronize the package index files from their sources
apt-get update -y
# Install packages.
apt-get install -y wget subversion curl git
# Check if packages used for downloading have been installed
if [[ ! -f "/usr/bin/wget" || ! -f "/usr/bin/curl" || ! -f "/usr/bin/svn" ]]; then
printf "${RED}ERROR${RESET} Missing packages. %s\n%s\n" "$CONNECTION" "$QUIT"
exit 1
fi
# Install composer
if ! [[ -f "/usr/local/bin/composer" ]]; then
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer || exit
if [[ -f "$HOME/.bashrc" ]]; then
printf "Adding .composer/vendor/bin to the PATH\n"
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> "$HOME/.bashrc"
fi
else
printf "Updating composer...\n"
composer self-update || printf "${RED}WARNING${RESET} Could not update composer. %s\n" "$CONNECTION"
fi
# Get the current php version
readonly PHP_VERSION=$(php -r "echo PHP_VERSION;")
# Set default PHPUnit version to 5 if no specific version was supplied
PHPUNIT_VERSION=${1-5}
if [[ ${PHP_VERSION:0:2} = "5." && ${PHP_VERSION:2:1} -lt 6 ]]; then
# Install PHPUnit 4.*.* for PHP versions lower than 5.6
PHPUNIT_VERSION="4"
fi
if wget --spider "https://phar.phpunit.de/phpunit-$PHPUNIT_VERSION.phar" >/dev/null 2>&1; then
printf "Installing PHPUnit...\n"
wget -q --show-progress "https://phar.phpunit.de/phpunit-$PHPUNIT_VERSION.phar"
chmod +x "phpunit-$PHPUNIT_VERSION.phar"
mv "phpunit-$PHPUNIT_VERSION.phar" /usr/local/bin/phpunit
else
printf "${RED}WARNING${RESET} Could not install PHPUnit version %s. %s and use a valid PHPUnit version (if supplied)\n" "phpunit-$PHPUNIT_VERSION.phar" "$CONNECTION"
fi
# Set environment variables
if [[ -f "$HOME/.bashrc" ]]; then
# Get the variables.
source "$HOME/.bashrc"
if [[ -z "${WP_TESTS_DIR}" ]]; then
printf "Setting WP_TESTS_DIR environment variable\n"
echo 'export WP_TESTS_DIR=/tmp/wordpress-tests-lib' >> "$HOME/.bashrc"
fi
if [[ -z "${WP_CORE_DIR}" ]]; then
printf "Setting WP_CORE_DIR environment variable\n"
echo 'export WP_CORE_DIR=/tmp/wordpress' >> "$HOME/.bashrc"
fi
# Get the new variables.
source "$HOME/.bashrc"
fi
if [[ -z "$WP_CORE_DIR" || -z "$WP_TESTS_DIR" ]]; then
printf "${RED}ERROR${RESET} Could not set the WordPress (directory) environment variables used by PHPUnit\n%s\n" "$QUIT"
exit 1
fi
if [[ -d "$WP_CORE_DIR" && -d "$WP_TESTS_DIR" ]]; then
# Get the latest WordPress version
readonly WP_LATEST=$(wget -q -O - "http://api.wordpress.org/core/version-check/1.5/" | head -n 4 | tail -n 1);
if [[ -z "$WP_LATEST" ]]; then
printf "${RED}ERROR${RESET} Could not get latest WordPress version from api.wordpress.org. %s\n%s\n" "$CONNECTION" "$QUIT"
exit 1
fi
# Update WordPress and WP_UnitTestCase to the latest version.
if wget --spider "https://wordpress.org/wordpress-$WP_LATEST.tar.gz" >/dev/null 2>&1; then
printf "Updating WordPress to %s \n" "$WP_LATEST"
wget -q --show-progress -O "/tmp/wordpress.tar.gz" "https://wordpress.org/wordpress-$WP_LATEST.tar.gz"
tar --strip-components=1 -zxmf "/tmp/wordpress.tar.gz" -C "$WP_CORE_DIR"
else
printf "${RED}WARNING${RESET} Could not download %s %s\n" "https://wordpress.org/wordpress-$WP_LATEST.tar.gz." "$CONNECTION"
fi
if [[ -f "$WP_TESTS_DIR/wp-tests-config.php" ]]; then
# Move the config outside the $WP_TESTS_DIR dir to Update WP_UnitTestCase.
mv "$WP_TESTS_DIR/wp-tests-config.php" "/tmp/wp-tests-config.php"
fi
if wget --spider "https://develop.svn.wordpress.org/tags/$WP_LATEST/tests/phpunit/includes/" >/dev/null 2>&1; then
printf "Updating %s WP_UnitTestCase...\n" "$WP_LATEST"
svn export --quiet --force "https://develop.svn.wordpress.org/tags/$WP_LATEST/tests/phpunit/includes/" "$WP_TESTS_DIR/includes"
svn export --quiet --force "https://develop.svn.wordpress.org/tags/$WP_LATEST/tests/phpunit/data/" "$WP_TESTS_DIR/data"
if [[ -f "/tmp/wp-tests-config.php" ]]; then
# Move the config back.
cp "/tmp/wp-tests-config.php" "$WP_TESTS_DIR/wp-tests-config.php"
fi
else
printf "${RED}WARNING${RESET} Could not download %s. %s.\n" "https://develop.svn.wordpress.org/tags/$WP_LATEST/tests/phpunit/includes/" "$CONNECTION"
fi
else
# Install WordPress and WP_UnitTestCase.
printf "Installing WordPress and WP_UnitTestCase\n"
wget -q --show-progress -O "/tmp/install-wp-tests.sh" "https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh"
if [[ -f "/tmp/install-wp-tests.sh" ]]; then
bash "/tmp/install-wp-tests.sh" "wordpress_test" "root" "root" "localhost" "latest" "true"
else
printf "${RED}WARNING${RESET} Could not install WordPress and WP_UnitTestCase\n"
fi
fi
# Install database if it doesn't exist
printf "Checking if database wordpress_test exists\n"
database=$(mysqlshow --user=root --password=root wordpress_test| grep -v Wildcard | grep -o wordpress_test)
if ! [[ "wordpress_test" = "$database" ]]; then
printf "Creating database wordpress_test\n"
mysqladmin create wordpress_test --user=root --password=root --host=localhost
else
printf "Database wordpress_test already exists\n"
fi
if [[ -f "$WP_TESTS_DIR/wp-tests-config.php" ]]; then
# VVV has the tests config outside the $WP_TESTS_DIR dir.
cp "$WP_TESTS_DIR/wp-tests-config.php" "/tmp/wp-tests-config.php"
fi
printf "\nFinished setting up packages\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment