Skip to content

Instantly share code, notes, and snippets.

@zuDevonRW
Created April 2, 2015 14:51
Show Gist options
  • Save zuDevonRW/d1d0fc1dcffbe6d7e422 to your computer and use it in GitHub Desktop.
Save zuDevonRW/d1d0fc1dcffbe6d7e422 to your computer and use it in GitHub Desktop.
PHP Unit test on pre-commit
#!/bin/bash
# Locate our phpunit.
phpunit=`which phpunit`
# Define a location to save the output.
outputlog="/tmp/phpunit_output_`date +%s`.log"
# Get name of the project (probably topmost directory name).
projectname=${PWD##*/}
echo
echo -e " + Starting unit tests for \033[1m\E[37;44m${projectname}\033[0m."
# execute unit tests. (Assume that phpunit.xml is in root of project).
output=`$phpunit htdocs/tests`
returnCode=$?
# Save the output of phpunit for posterity.
echo "$output" > $outputlog
# if unit tests fail, output a summary and exit with failure code.
if [ $returnCode -ne 0 ]; then
# find the line with the summary.
while read -r line; do
if [[ $line =~ Failures: ]] ; then
summary=$line
break
fi
done <<< "$output"
# output the status.
echo -e " + Test suite \033[1m\E[47;41mfailed\033[0m: "
echo
echo -e "$summary\033[0m"
echo
echo " + The full output of phpunit has been saved in:"
echo -e " \033[1m${outputlog}\033[0m"
echo
# abort the commit.
echo -e " + \033[1m\E[47;41mABORTING COMMIT\033[0m"
echo
exit $returnCode
else
echo -e " + All tests \033[1m\E[47;42mpassed\033[0m. The full output of phpunit has been saved in:"
echo -e " \033[1m${outputlog}\033[0m"
echo " + Proceeding with commit. Have a nice day."
echo
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment