Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds the option to provide a custom phpunit command via args #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Just add to your `.pre-commit-config.yaml` file with the following
## php-lint

```yaml
<<<<<<< HEAD
- repo: [email protected]:hootsuite/pre-commit-php.git
sha: 1.1.0
hooks:
Expand All @@ -47,7 +46,6 @@ A systems hook that just runs `php -l` against stage files that have the `.php`

## php-unit


```yaml
- repo: [email protected]:hootsuite/pre-commit-php.git
sha: 1.1.0
Expand All @@ -59,6 +57,8 @@ A bash script that will run the appropriate phpunit executable. It will assume
- Find the executable to run at either `vendor/bin/phpunit`, `phpunit` or `php phpunit.phar` (in that exact order).
- There is already a `phpunit.xml` in the root of the repo

*Optional:* You can provide a custom command as an argument, this will overwrite the default behavior and enables you to further customize your php-unit setup. e.g.: `args: ["composer run php-unit"]`

Note in its current state, it will run the whole PHPUnit test as along as `.php` file was committed.

## php-cs
Expand Down
52 changes: 31 additions & 21 deletions pre_commit_hooks/php-unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,60 @@

# Bash PHP Unit Task Runner
#
# php-unit command is run via composer run command
# so you can for example execute php-unit via a docker container
#
# Exit 0 if no errors found
# Exit 1 if errors were found
#
# Requires
# - php
#
# Arguments
# - None
# - : Your custom phpunit command

# Echo Colors
msg_color_magenta='\e[1;35m'
msg_color_yellow='\e[0;33m'
msg_color_none='\e[0m' # No Color

# Loop through the list of paths to run php lint against
# Check if a custom phpunit command was provided as argument
phpunit_custom_command=$1;

# Loop through the list of paths to run phpunit against
echo -en "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none} \n"

phpunit_local_exec="phpunit.phar"
phpunit_command="php $phpunit_local_exec"
if [ "$phpunit_custom_command" = "" ]; then
phpunit_local_exec="phpunit.phar"
phpunit_command="php $phpunit_local_exec"

# Check vendor/bin/phpunit
phpunit_vendor_command="vendor/bin/phpunit"
phpunit_global_command="phpunit"
if [ -f "$phpunit_vendor_command" ]; then
phpunit_command=$phpunit_vendor_command
else
# Check vendor/bin/phpunit
phpunit_vendor_command="vendor/bin/phpunit"
phpunit_global_command="phpunit"
if [ -f "$phpunit_vendor_command" ]; then
phpunit_command=$phpunit_vendor_command
else
if hash phpunit 2>/dev/null; then
phpunit_command=$phpunit_global_command
phpunit_command=$phpunit_global_command
else
if [ -f "$phpunit_local_exec" ]; then
phpunit_command=$phpunit_command
else
echo "No valid PHP Unit executable found! Please have one available as either $phpunit_vendor_command, $phpunit_global_command or $phpunit_local_exec"
exit 1
fi
if [ -f "$phpunit_local_exec" ]; then
phpunit_command=$phpunit_command
else
echo "No valid PHP Unit executable found! Please have one available as either $phpunit_vendor_command, $phpunit_global_command or $phpunit_local_exec"
exit 1
fi
fi
fi
else
phpunit_command=$phpunit_custom_command
fi

echo "Running command $phpunit_command"
command_result=`eval $phpunit_command`
if [[ $command_result =~ FAILURES ]]
then
echo "Failures detected in unit tests..."
echo "$command_result"
exit 1
echo "Failures detected in unit tests..."
echo "$command_result"
exit 1
fi
exit 0
exit 0