Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Forked from DevinWalker/setup-phpunit.sh
Last active April 14, 2018 17:03
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 Shelob9/1502bb34741206f48b7c41d0f4ee9734 to your computer and use it in GitHub Desktop.
Save Shelob9/1502bb34741206f48b7c41d0f4ee9734 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
#
# WordPress and the WP_UnitTestCase are installed in the /tmp directory
# The $WP_CORE_DIR and $WP_TESTS_DIR variables are added to the .bashrc file
#
# The $WP_CORE_DIR and $WP_TESTS_DIR variables are used by plugins that
# have their unit tests scaffolded by WP-CLI. It's also set by VVV.
#
# You only have to run this script once. PHPUnit (and the other packages)
# are still available next time you ssh into your site.
#
# 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 https://gist.github.com/Shelob9/1502bb34741206f48b7c41d0f4ee9734/raw/9e467c984314213b6e7bb9c58ea47a458b0d79fe/setup-phpunit.sh
#
# 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
#
# 6 - Install tests (run from plugin's bin if is Caldera Forms or similar)
# bash install-wp-tests.sh local root root localhost latest true
#
# ===============================================================================
if ! ping -c 3 --linger=5 8.8.8.8 >> /dev/null 2>&1; then
# Bail if there is no internet connection
printf "Could not install packages\n"
printf "No network connection detected\n\n"
exit 1
fi
# Re-synchronize the package index files from their sources
apt-get update -y
# Install packages.
apt-get install -y wget subversion curl php5-cli git
# Install composer
if [[ "/usr/local/bin/composer" != $(which composer) ]]; then
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
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 "composer is already installed\n"
fi
# Install PHPUnit 5.7 instead of version 6 and up
# See ticket https://core.trac.wordpress.org/ticket/39822
if [[ "/usr/local/bin/phpunit" != $(which phpunit) ]]; then
wget https://phar.phpunit.de/phpunit-5.7.19.phar
chmod +x phpunit-5.7.19.phar
mv phpunit-5.7.19.phar /usr/local/bin/phpunit
else
printf "phpunit is already installed\n"
fi
# Install WordPress and WP_UnitTestCase
if [[ ! -f "install-wp-tests.sh" ]]; then
wget "https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh"
fi
if [[ -f "$HOME/.bashrc" ]]; then
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
fi
if [[ -f "install-wp-tests.sh" ]]; then
bash "install-wp-tests.sh" "wordpress_test" "root" "root" "localhost"
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