Run tests on commit

David Carr

Tests Tutorials PHPUnit Bash

Go into git hooks folder and copy pre-commit.sample to pre-commit

cp .git/hooks/pre-commit.sample .git/hooks/pre-commit

then edit it with vim:

vi .git/hooks/pre-commit

clear the contents by pressing dG to clear the contents.

Next to into insert mode by pressing i

now paste in:

#!/usr/bin/env php
<?php
echo "Running tests.. ";
exec('vendor/bin/phpunit', $output, $returnCode);
if ($returnCode !== 0) {
    // Show full output
    echo PHP_EOL . implode($output, PHP_EOL) . PHP_EOL;
    echo "Aborting commit.." . PHP_EOL;
    exit(1);
}
// Show summary (last line)
echo array_pop($output) . PHP_EOL;
exit(0);

Now save and exit by pressing ESC then :wq to write and quit

now every commit will run the tests suite.

So you only want to do this where you have tests passing or you can't move forward until you fix the tests.

Copyright © 2006 - 2024 DC Blog - All rights reserved.