Skip to content

Instantly share code, notes, and snippets.

@rattfieldnz
Created October 17, 2019 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rattfieldnz/8108552ca630df2679f366965dc28f81 to your computer and use it in GitHub Desktop.
Save rattfieldnz/8108552ca630df2679f366965dc28f81 to your computer and use it in GitHub Desktop.
Script to run PHPUnit tests in a Laravel-based project, with keys not stored in phpunit.xml.
#!/bin/bash
clear;
if [ -z "$1" ] && [ -z "${GOOGLE_API_KEY}" ]; then
echo 'Please provide a Google API key.'
exit
fi
if [ -z "$2" ] && [ -z "${CODECOV_TOKEN}" ]; then
echo 'Please provide a Code Coverage (codecov.io) key.'
exit
fi
# Set Google API key for use with PHPUnit.
if [ -z "${GOOGLE_API_KEY}" ]; then
export GOOGLE_API_KEY=${1}
fi
# Set Code Coverage (codecov.io) key for use with PHPUnit.
if [ -z "${CODECOV_TOKEN}" ]; then
export CODECOV_TOKEN=${2}
fi
echo -e "Your Google API key is: ${GOOGLE_API_KEY}.\n"
echo -e "Your Code Coverage (codecov.io) key is: ${CODECOV_TOKEN}.\n"
sleep 2;
echo -e "Now checking if XDebug is installed...\n";
php -v | grep -q 'Xdebug';
if [[ $? != 0 ]]; then
sleep 1;
echo -e "XDebug is not installed. Installing now...\n"
echo -e "Installing php-dev...\n";
sudo apt-get install php-dev -y > /dev/null 2>&1;
echo -e "Updating PECL channel...\n";
sudo pecl channel-update pecl.php.net > /dev/null 2>&1;
echo -e "Installing XDebug...\n";
sudo pecl install xdebug > /dev/null 2>&1;
echo -e "Finished installing XDebug.\n"
fi
sleep 1;
php -v | grep -q 'Xdebug';
if [[ $? == 0 ]]; then
echo -e "XDebug is installed. Now running PHPUnit tests and generating code coverage report...\n\n"
php artisan key:generate --force
sudo vendor/bin/phpunit --coverage-clover coverage.xml --verbose;
bash <(curl -s https://codecov.io/bash)
echo -e "\nPHPUnit testing and code coverage reports now completed!"
else
echo -e "\nPHPUnit testing could not be done - XDebug is not installed!"
fi
@rattfieldnz
Copy link
Author

Usage:

chmod u+x run_phpunit;
./run_phpunit YOURGOOGLEAPIKEY YOURCODECOVERAGEKEY

Feel free to fork this and modify to suit your needs :).

@rattfieldnz
Copy link
Author

Note: If you're using CI tools like TravisCI or CircleCI, you can add above keys securely in their configurations.

CircleCI: https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-project.
TravisCI: https://docs.travis-ci.com/user/environment-variables/#defining-variables-in-repository-settings

The two CI providers above also offer options to add encrypted variables in your [provider].yml file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment