Installing PHPUnit under PHP 5.4 from Homebrew

On my mac, I use Jose Gonzales’ homebrew tap for installing PHP 5.4. It works amazingly well, generating apache modules and FPM plists for every version in case I need them, as well as the CLI version.

Unfortunately, it also means that all of you pear extensions are installed under your PHP 5.4 install, meaning that you have to do a bit of work to get it all working. Here are the steps I followed:

Install PHPUnit

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover pear.symfony.com
sudo pear install --alldeps phpunit/phpunit

Configure your PHP include path

Edit your php.ini to set up your include path, it’s normally at /usr/local/etc/php/5.4/php.ini (or run php -i to see).

Search for include_path and add the path /usr/local/Cellar/php54/5.4.7/lib/php/. It should look something like this:

include_path=".:/usr/local/Cellar/php54/5.4.7/lib/php/"

Note that you might need to change 5.4.7 to whatever version of PHP you have installed. Run php -v to find out.

Done

Open a new terminal window and type phpunit – things should be working fine

14 thoughts to “Installing PHPUnit under PHP 5.4 from Homebrew”

    1. Thanks for mentioning in case others have the same issue. This is actually documented in the README for the homebrew recipe. A better way to add to your path is to use the following line:

      export PATH="$(brew --prefix josegonzalez/php/php54)/bin:$PATH"

      1. It would, but I’m not sure if that would work. PHPUnit includes a lot of extra files itself, and if the include path is not set up it might not be able to find them (e.g. the File Iterator, or any DBUnit tests)

  1. It looks like right now (php 5.4.11 from josegonzalez/homebrew-php) the path to /usr/local/Cellar/php54/5.4.7/lib/php/ is in PHP’s include_path by default.

    The default php.ini in /usr/local/etc/php/5.4/php.ini has “include_path” commented out (with “;”). But somehow it automatically includes this directory at run time.

    Verified by the following command, ran right after “brew install php54”:

    $ which php && readlink `which php` && php -r ‘echo get_include_path();’

    returns:

    /usr/local/bin/php
    ../Cellar/php54/5.4.11/bin/php
    .:/usr/local/Cellar/php54/5.4.11/lib/php

  2. To avoid further modification of the php.ini file, you could use:

    ; php54 libraries
    include_path=”.:/usr/local/opt/php54/lib/php/”

    In the /usr/local/etc/php/5.4/php.ini file.

    As /usr/local/opt/php54 is symlinked to your latest Homebrewed php54 install, it should get updated whenever you issue a brew upgrade (or specifically install a new brewed version of php54). This means, of course, that you may have to re-execute the 3 pear commands outlined above after the upgrade…

    Also, it might be important to note that, to make sure you are effectively executing the Homebrewed version of php (as well as some other executables installed with Homebrew) it is necessary to update your ~.bash_profile configuration. Add this:

    # Homebrew PHP54
    export PATH=”$(brew –prefix josegonzalez/php/php54)/bin:$(brew –prefix josegonzalez/php/php54)/lib/php:$PATH”
    # update PATH for Homebrew
    export PATH=/usr/local/bin:/usr/local/sbin:$PATH

    (after making those changes you should then open a new terminal window to have your updated ~.bash_profile used in the CLI)

    To verify that everything works as expected, type

    php -v

    in the command line, the displayed version number should then fit your homebrewed version, which will most probably be more recent than the OS X packaged one.

    Hope this may help some avoid loosing some hairs trying to figure out what php is effectively running 😉

  3. Try running “brew unlink php54 && brew link php54” after installing PHPUnit. This will let homebrew create a symlink from all the files in the bin folder to the main homebrew bin folder. No need to modify $PATH then.

Leave a Reply