【问题标题】:Pre commit hook to check PHP code coverage and code sniffer for Symfony用于检查 PHP 代码覆盖率和 Symfony 代码嗅探器的预提交钩子
【发布时间】:2017-12-17 23:45:44
【问题描述】:

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

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

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

【问题讨论】:

    标签: php git shell symfony pre-commit-hook


    【解决方案1】:

    如果您的 bash 脚本按预期工作,您只需将其命名为 pre-commit 并将其放入您的 git 挂钩中:/path/to/repo/.git/hooks。您会在此目录中找到一些示例。

    有关 git 挂钩的更多信息:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

    那么,对于你的代码嗅探器,我推荐https://github.com/squizlabs/PHP_CodeSniffer

    还有https://github.com/phpro/grumphp会为你做一切。

    【讨论】:

      【解决方案2】:

      当您可以使用客户端 git 挂钩轻松完成时,无需使用 grumphp。下面的示例运行 php-cs-fixer 并在您运行 git push origin ..... 时防止将损坏的代码推送到 github 存储库。

      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 "\n\t\033[1;31m[PHP-CS-Fixer] Push Aborted\n\n\033[0m"
      
              return 1
          fi
      fi
      

      授予权限

      chmod +x .git/hooks/pre-push
      

      测试

      git push origin .....
      

      example here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多