xip-solid-foundation / pre-commit
用于预提交钩子和基本测试的包
v1.0.9
2022-01-27 12:01 UTC
Requires
- brianium/paratest: ^6.4
- dama/doctrine-test-bundle: ^6.7
- doctrine/orm: ^2.11
- friendsofphp/php-cs-fixer: ^3.3
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
README
带有基本测试的预提交钩子安装程序
先决条件
- Docker Compose,安装 Docker Compose
- PHP Docker 镜像
您可以使用 Symfony-Docker 下载并安装 Docker for Symfony。
安装
使用 Symfony Flex 安装的程序
$ composer require xip-solid-foundation/pre-commit
未使用 Symfony Flex 的程序
步骤 1:安装包
打开命令行,进入您的项目目录,并执行以下命令以安装此包的最新稳定版本
$ composer require xip-solid-foundation/pre-commit
步骤 2:添加 Pre-commit 文件
示例
# Automatic usage by installing symbolic link:
# ln -sf ../../bin/pre-commit .git/hooks/pre-commit
# Enable TTY, so fastest runs on a single line and looks cooler
exec < /dev/tty
if [[ -z `docker ps -q --no-trunc | grep $(docker-compose ps -q apache2)` ]]; then
echo -e "\e[41m COMMIT FAILED: Docker-compose is not running! \e[0m";
exit 1
fi
# Install phpunit synchronously to avoid problems.
docker-compose exec php bin/phpunit install
docker-compose exec -T php bin/console cache:warmup --env=prod
if [[ $? != "0" ]]
then
echo -e "\e[41m COMMIT FAILED: Cannot warm cache for env=prod! \e[0m":
exit 1
fi
# see which files you wanted to commit, fix code style and re-add
FILES=` git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | tr '\n' ' '`
if [[ $FILES ]]
then
FILES_NO_APPLICATION=`echo ${FILES} | sed "s/api\///g"`
docker-compose exec -T php vendor/bin/php-cs-fixer fix
git add ${FILES}
fi
# remove outdated code coverage files
rm -rf var/coverage.txt public/test.html
# warmup test cache to speed up tests
docker-compose exec php bin/console cache:warmup --env=test
# Run Tests and generate code coverage
docker-compose exec php bash -c "XDEBUG_MODE=coverage vendor/bin/paratest --coverage-html=public/test.html --coverage-text=var/coverage.txt tests"
if [[ $? != "0" ]]
then
echo -e "\e[41m COMMIT FAILED: You have test errors! \e[0m":
exit 1
fi
# merge code coverage
echo "Full HTML coverage at http://0.0.0.0/test.html/index.html"
OK=`docker-compose exec -T php head -n10 var/coverage.txt | grep Lines | grep "100.00%"`
if [[ ${OK} = "" ]]
then
echo -e "\e[41m COMMIT FAILED: Code coverage not 100%! \e[0m":
exit 1
fi
echo ""
用法
现在您可以使用以下命令运行预提交
$ bin/pre-commit