douan6815
douan6815
2017-07-13 09:20
浏览 82
已采纳

预提交钩子,用于检查Symfony的PHP代码覆盖率和代码嗅探器

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 "##################################################################################################"

图片转代码服务由CSDN问答提供 功能建议

如何为PHP Symfony代码执行预提交挂钩以分析代码覆盖率和代码嗅探器报告? 我应该使用哪些命令和工具?

到目前为止,我可以使用三叶草格式的PHPUnit生成代码覆盖率报告:

 #!/ bin / bash 
 
echo“#######################################  ##################################################  ##########“
echo”启动PHPUnit测试:“`date”+%y-%m-%d%H-%M-%S“`
echo”######  ##################################################  ##########################################“#n 
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“在”日期“完成Cron +%y-%m-%d%H-%M-%S”`
echo“Cron Task Complet  e“
echo”##############################################  ##################################################  ###“
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyuben9434
    douyuben9434 2017-07-13 09:26
    已采纳

    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.

    点赞 评论
  • duanrong0738
    duanrong0738 2017-07-13 15:06

    No need to use grumphp when you can easily accomplish it with client side git hooks. Example below runs php-cs-fixer and prevents pushing broken code to github repository when you run git push origin ......

    your_project_folder/.git/hooks/pre-push

    #!/bin/sh
    
    if [ -f ./bin/php-cs-fixer ]
    then
        ./bin/php-cs-fixer fix --dry-run --verbose --diff src
        if [ $? -ne 0 ]
        then
            printf "
    \t\033[1;31m[PHP-CS-Fixer] Push Aborted
    
    \033[0m"
    
            return 1
        fi
    fi
    

    Grant permissions

    chmod +x .git/hooks/pre-push
    

    Test

    git push origin .....
    

    See example here.

    点赞 评论

相关推荐