PHPUnit on pre-commit Git Hook seems to use different PHP version

I’m trying to set up a pre-commit git hook that will run and validate our unit tests. We are using PHPUnit in the Symfony 2 platform.

For some reason it seems that when I run the unit tests via the git hook that it is using a different version of PHP.

When I check my php version I get:

php -v
PHP 5.4.14 (cli) (built: May  8 2013 10:23:18) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

Here is my git hook:

#!/usr/bin/php
<?php
// Hook configuration
$project = 'My Project';

// Tell the commiter what the hook is doing
echo PHP_EOL;
echo '>> Starting unit tests'.PHP_EOL;

// Execute project unit tests
exec('bin/phpunit -c app/', $output, $returnCode);

// 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 and abort the commit
    echo '>> Test suite for '.$project.' failed:'.PHP_EOL;
    echo $minimalTestSummary;
    echo chr(27).'[0m'.PHP_EOL; // disable colors and add a line break
    echo PHP_EOL;
    exit(1);
}

echo '>> All tests for '.$project.' passed.'.PHP_EOL;
echo PHP_EOL;
exit(0);

When I run the unit tests manually (“bin/phpunit -c app/” from my project directory) the tests execute with out error. When I run the tests via the git hook I get a PHP Parse Error. I’ve determined that the parse error stems from the use of array bracket notation ([‘key’=>’value’]) that was added in PHP 5.4

When I echo php -v in the git hook I get the following output

Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies

Since the Zend Engine is different (2.4.0 when run manually and 2.3.0 when run via the git hook) I’m assuming that there is a PHP version mismatch happening.

Does anyone have any clues as to why this is happening?

Thanks!

  • Python unittests in Jenkins?
  • Disallow `strftime` in a code base
  • How to write unit tests for GitPython clone/pull functions?
  • Loading a specific file from a module in node
  • Git: pre-merge hook?
  • How to test extract of files
  • Is there a way to run unit test for a WebGL webapp in a headless browser
  • How to keep the unit test output in Jenkins
  • One Solution collect form web for “PHPUnit on pre-commit Git Hook seems to use different PHP version”

    the git/phpunit executable most likely sees a different PATH environment variable than your shell (bash/zsh/…) executable.

    If a program looks for i.e. php – the first match from your PATH variable will be used.

    Probably you’re adding a folder containing a different PHP executable to the PATH variable in one of your shell startup files.

    Possible files include:

    /etc/profile
    /etc/zshenv
    /etc/bashrc
    ~/.profile
    ~/.bashrc
    ~/.zshrc
    ...
    
    Git Baby is a git and github fan, let's start git clone.