Skip to content

Instantly share code, notes, and snippets.

@nickturrietta
Created June 13, 2019 20:59
Show Gist options
  • Select an option

  • Save nickturrietta/ae67497a5a8144fa396d9546d600ea75 to your computer and use it in GitHub Desktop.

Select an option

Save nickturrietta/ae67497a5a8144fa396d9546d600ea75 to your computer and use it in GitHub Desktop.
Pre-Commit hook
#!/usr/bin/php
<?php
echo PHP_EOL;
// output a little introduction
echo '>> Starting unit tests' . PHP_EOL;
// Get the path to root of app
$root = realpath(__DIR__ . '/../..');
// get the name for this project; probably the topmost folder name
$projectName = basename($root);
// change to working directory
if ( is_dir($root . '/httpdocs') ) {
chdir($root . '/httpdocs');
}
else {
chdir($root);
}
// execute unit tests (it is assumed that a phpunit.xml configuration is present
// in the root of the project)
exec('phpunit', $output, $returnCode); // cwd is assumed here
// if the build failed, output a summary and fail
if ( $returnCode !== 0 ) {
// find the line with the summary; this might not be the last
while ( ($minimalTestSummary = array_pop($output)) !== null ) {
if ( strpos($minimalTestSummary, 'Tests:' ) !== false) {
break;
}
}
// output the status
echo '>> Test suite for ' . $projectName . ' failed:' . PHP_EOL;
echo $minimalTestSummary;
echo chr(27) . '[0m' . PHP_EOL; // disable colors and add a line break
echo PHP_EOL;
// abort the commit
exit(1);
}
echo '>> All tests for ' . $projectName . ' passed.' . PHP_EOL;
echo PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment