Skip to content

Instantly share code, notes, and snippets.

@DevinWalker
Forked from keesiemeijer/setup-phpunit.sh
Created May 26, 2017 20:46
Show Gist options
  • Save DevinWalker/161860650b755fbc33b840682a3b4039 to your computer and use it in GitHub Desktop.
Save DevinWalker/161860650b755fbc33b840682a3b4039 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.githubusercontent.com/keesiemeijer/a888f3d9609478b310c2d952644891ba/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
# ===============================================================================
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