Last Updated: February 25, 2016
·
2.173K
· jaitaiwan

Phpunit + Git Pre-commit awesomeness

For a recent project I've needed to make sure that all my code is ship-shape for delivery. This has meant using phpunit to do testing across my new code. I decided that I wanted an interactive console experience so I've created this cool pre-commit hook which will run a beautiful testdox phpunit test and then if there are errors it will ask if you want a more detailed test. If the tests work it'll pop you through all your normal git funtimes!

#!/bin/bash

exec < /dev/tty
phpunit --colors -c $(echo $PWD)/functions/phpunit.xml --testdox
#Get the last processes exit code
rc=$?
if [[ $rc != 0 ]] ; then
    echo -n "It looks like some of your tests failed. Would you like to see a more detailed test output? (y/n) "
    read YN
    if [ -z "$YN" ]; then
        exit $rc;
    elif [ "$YN" != "y" ]; then
        exit $rc;
    fi
    phpunit --colors -c $(echo $PWD)/functions/phpunit.xml --verbose
fi

exit $rc;

You can change your code to suit and do cool things like just going with automatic test discovery etc. Enjoy!