Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

You can prevent developers pushing broken tests to Git repository by using client-side pre-push git hook. Example below checks PHP coding standards with php-cs-fixer as defined in the PSR-1 and PSR-2.


Git hook


This file goes into .git/hooks/ folder of your project where all the other sample hooks are stored. Name it as pre-push.


#!/bin/sh

if [ -f ./bin/php-cs-fixer ]
then
./bin/php-cs-fixer fix --dry-run --quiet src
if [ $? -ne 0 ]
then
RED='\033[0;31m'
CYAN='\033[0;36m'
NA='\033[0m'

printf "\n\t${CYAN}[GIT POLICY]${NA} PHP-CS-Fixer has failed - ${RED}Push Aborted${NA}\n\n"

return 1
fi
fi

Setup


$ chmod +x .git/hooks/pre-push

Test