Skip to content

Instantly share code, notes, and snippets.

@Najki
Last active October 13, 2025 11:15
Show Gist options
  • Select an option

  • Save Najki/747be48f401086d5a043ce760299d11f to your computer and use it in GitHub Desktop.

Select an option

Save Najki/747be48f401086d5a043ce760299d11f to your computer and use it in GitHub Desktop.
Run PHPUnit in monorepo from main directory or child directory
#!/bin/bash
set -euo pipefail
function phpunit_exists() {
if [ -f bin/phpunit ] || [ -f vendor/bin/phpunit ]; then
return 0
else
return 1
fi
}
function run_phpunit() {
if [ -f bin/phpunit ]; then
SYMFONY_DEPRECATIONS_HELPER=weak bin/phpunit --no-coverage --display-phpunit-deprecations $@
elif [ -f vendor/bin/phpunit ]; then
SYMFONY_DEPRECATIONS_HELPER=weak vendor/bin/phpunit --no-coverage --display-phpunit-deprecations $@
else
echo "Could not find bin/phpunit or vendor/bin/phpunit."
exit 1
fi
}
######
if [ $# -eq 0 ]; then
echo " Usage:"
echo " $0 <path>"
echo ""
echo " Example:"
echo " $0 api/tests/SomeTest.php"
echo " This would enter api directory and run \"bin/phpunit --no-coverage tests/SomeTest.php\""
exit 1
fi
path=$1
subdirectory=$(echo $path | cut -d'/' -f1)
pathWithoutSubdirectory=$(echo $path | cut -d'/' -f2-)
remainingArgs=${@:2}
if phpunit_exists; then
if [ -f $path ]; then
run_phpunit $path $remainingArgs
elif [ -f $pathWithoutSubdirectory ]; then
run_phpunit $pathWithoutSubdirectory $remainingArgs
else
echo "Could not find target test in $path or $pathWithoutSubdirectory."
exit 1
fi
exit 0
fi
pushd $subdirectory > /dev/null
if phpunit_exists; then
if [ -f $path ]; then
run_phpunit $path $remainingArgs
elif [ -f $pathWithoutSubdirectory ]; then
run_phpunit $pathWithoutSubdirectory $remainingArgs
else
echo "Could not find target test in $path or $pathWithoutSubdirectory."
popd > /dev/null
exit 1
fi
popd > /dev/null
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment