Skip to content

Instantly share code, notes, and snippets.

@iCaspar
Forked from keesiemeijer/setup-phpunit.sh
Created September 28, 2018 14:39
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 iCaspar/43d18ea19e6a37287c4c652fadf39cf7 to your computer and use it in GitHub Desktop.
Save iCaspar/43d18ea19e6a37287c4c652fadf39cf7 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
# ===============================================================================
printf "Installing packages...\n"
readonly QUIT_MSG="Stopping script..."
# Get current php version
readonly PHP_VERSION=$(php -r "echo PHP_VERSION;")
# 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 "%s\nMissing packages. Make sure you're connected to the internet.\n" "$QUIT_MSG"
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
fi
# Install PHPUnit 4.8 or 5.7
# See ticket https://core.trac.wordpress.org/ticket/39822
PHPUNIT_VERSION="5.7"
if [[ ${PHP_VERSION:0:2} = "5." && ${PHP_VERSION:2:1} -lt 6 ]]; then
# Install PHPUnit 4.8 for PHP versions lower than 5.6
PHPUNIT_VERSION="4.8"
fi
if [[ "/usr/local/bin/phpunit" != $(which phpunit) ]]; then
wget "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 "phpunit is already installed\n"
fi
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 "%s\n Could not find the WordPress directories used by PHPUnit\n" "$QUIT_MSG"
exit 1
fi
if [[ -d "$WP_CORE_DIR" && -d "$WP_TESTS_DIR" ]]; then
# Get the latest WordPress version
latest=$(wget -q -O - "http://api.wordpress.org/core/version-check/1.5/" | head -n 4 | tail -n 1);
if [[ -z "$latest" ]]; then
printf "%s\nCould not get latest WordPress version from api.wordpress.org\n" "$QUIT_MSG"
exit 1
fi
# Update WordPress and WP_UnitTestCase to the latest version.
if wget --spider "https://wordpress.org/wordpress-$latest.tar.gz" >/dev/null 2>&1; then
printf "Updating WordPress to %s \n" "$latest"
wget -nv -O "/tmp/wordpress.tar.gz" "https://wordpress.org/wordpress-$latest.tar.gz"
tar --strip-components=1 -zxmf "/tmp/wordpress.tar.gz" -C "$WP_CORE_DIR"
else
printf "Could not download %s\n" "https://wordpress.org/wordpress-$latest.tar.gz\n"
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/$latest/tests/phpunit/includes/" >/dev/null 2>&1; then
printf "Updating WP_UnitTestCase %s\n" "$latest"
svn export --quiet --force "https://develop.svn.wordpress.org/tags/$latest/tests/phpunit/includes/" "$WP_TESTS_DIR/includes"
svn export --quiet --force "https://develop.svn.wordpress.org/tags/$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 "Could not download %s\n" "https://develop.svn.wordpress.org/tags/$latest/tests/phpunit/includes/\n"
fi
else
# Install WordPress and WP_UnitTestCase.
printf "Installing WordPress and WP_UnitTestCase\n"
wget -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 "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