Last Updated: February 25, 2016
· 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!


exec < /dev/tty
phpunit --colors -c $(echo $PWD)/functions/phpunit.xml --testdox
#Get the last processes exit code
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;
    phpunit --colors -c $(echo $PWD)/functions/phpunit.xml --verbose

exit $rc;

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