Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre commit hook to check PHP code coverage and code sniffer for Symfony

How can I do a pre-commit hook for PHP Symfony code to analyze code coverage and code sniffer report? What are commands and tools I should use?

As of now, I can generate a code coverage report using PHPUnit in clover format:

#!/bin/bash

echo "##################################################################################################"
echo "Starting PHPUnit tests : "`date "+%y-%m-%d %H-%M-%S"`
echo "##################################################################################################"

php app/console -e=dev doctrine:database:drop --force
php app/console -e=dev doctrine:database:create
php app/console -e=dev doctrine:schema:create
php app/console -e=dev -n doctrine:fixtures:load
#phpunit -c app --coverage-html build/html
phpunit -c app --log-junit build/unit.xml
'[' -f build/coverage.xml ']'
phpunit -c app --coverage-clover build/coverage.xml
php app/console -e=dev doctrine:schema:drop --force
php app/console -e=dev doctrine:database:drop --force
echo "Finishing Cron at "`date "+%y-%m-%d %H-%M-%S"`
echo "Cron Task Complete"
echo "##################################################################################################"
like image 585
aniruddha Avatar asked May 13 '26 13:05

aniruddha


1 Answers

If your bash script works as expected, you just need to name it pre-commit and put it in your git hooks: /path/to/repo/.git/hooks. You'll find some sample in this directory.

For more information about git hooks: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

Then, for you code sniffer, I recommend https://github.com/squizlabs/PHP_CodeSniffer.

There is also https://github.com/phpro/grumphp that will do everything for you.

like image 191
YoannFleuryDev Avatar answered May 15 '26 04:05

YoannFleuryDev