Running PHPUnit on Git hook

I use Git and I love Git! I knew about Git hooks but never used in real life. So today I decided to get my hands dirty with Git hooks. I had a feeling it’d be awesome if I could use the “pre-commit” hook to do some routine processing before the changes are committed. I had two things in mind: 1) Unit Testing with PHPUnit or 2) Generating documentation using DocBlox. I chose Unit testing favouring simplicity.

Summary: I have a git repo for my PHP project. I have written some unit tests for the project and I want to run an automated unit test before changes are committed. If the unit tests succeed, the changes will be committed, if one of the tests fails, the commit will not proceed. This will ensure that I am always committing *working* codes to my Git repo.

Understanding Git Hooks
Git hooks are nothing but executable scripts which are run by Git on special events. If the hook script returns an exit code of 0, Git continues with whatever it was doing. For any non zero exit code, Git halts the operation. It’s that simple!

Git hooks are stored in the “.git/hooks” directory of a git repo. There are some samples already generated for us to check out! The script should be named with the hook, that is for the “pre-commit” hook the file name should also be “pre-commit”.

My Lame Unit Test
To begin with, we need at least one test case. So I wrote this really lame unit test and saved it as “Mytest.php”

Try running the script with PHPUnit like this:

Output:

Okay, so we have a valid unit test that passes. Now let’s add a Git pre-commit hook to run this unit test before commit is made.

Setting Up Git Hook
Assuming that your project is already under Git DVCS, navigate to .git/hooks and create a file named “pre-commit” with the following contents:

PS: The pre-commit script is a modified version of this nice Gist: https://gist.github.com/975252

Make the “pre-commit” file executable by issuing this command:

NB: This is important that you make the script executable otherwise it will not function as a hook.

Making a Commit
We shall create some file or make some changes and try to commit. In my case, I just added the “Mytest.php” file and tried to commit. Here’s the output:

Aha! All tests passed and the commit was made. Now let’s force the unit test to fail. Change the unit test to look like this:

And let’s commit now:

Compare the output with previous output. There is nothing similar to:

The commit was not made because the test failed. So, it worked! 😀

I am really loving what it is going to offer me! I shall be writing tests for most of my projects so I never unintentionally break something in a commit. Unit testing is just one type of validation, I believe we could use lots of others (eg. PHP Code Sniffer) 🙂


7 Comments Running PHPUnit on Git hook

  1. Pingback: Rodando Testes com Pre-Commit (GIT HOOKS) & Laravel | Lucas Macedo

  2. alex

    Hi Masnun. I’m curious. After all these years, do you still think it’s a good idea to hook phpunit into pre-commit?

    Cheers

    Alex

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *