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.

Fathom Analytics $10 discount on your first invoice using this link

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

Sponsor

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.