David Carr

Run tests on commit

PHPUnit Tests Tutorials 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.

Laravel Modules Book by David Carr

Help support the blog so that I can continue creating new content!

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2022 DC Blog. All code MIT license. All rights reserved.