52

Though I'm sure others have eventually managed to figure this out, I've been following the various documentation out there and have been having a heck of a rough time of it.

http://www.phpunit.de/manual/current/en/installation.html

Makes it sound pretty easy. However depending on your setup, you might be going down a rabbit hole.

PEAR for example must be of a version higher than 1.8.1. I had 1.8.0 at the time, so I went to find out how to update PEAR

PEAR upgrade-all

Gives an error. No access.

sudo PEAR upgrade-all

Works, but upgrades the PEAR install owned by the user 'sudo' (and not your primary account... or something like that, short version is 5that it plain don't work)

cd ~
pico .bash_profile

add

export PATH=/usr/local/bin:$PATH

give you the correct PEAR when you type PEAR, you're finally ready for step 1 of the install PHPUnit instructions.

pear channel-discover pear.phpunit.de

Error. You don't have access to /usr/local/temp

sudo chmod 777 /usr/local/temp

Error. You don't have access to /usr/local/temp/channel.xml

sudo chmod 777 /usr/local/temp/channel.xml
pear channel-discover pear.phpunit.de

Error.

Registry directory is not writeable by the current user

BUT I'M ON A MAC!

/sigh
/facepalm
/tears

I've actually got a "working copy" of phpunit on my machine. (YAY!) the odd problem is that it only works when I type phpunit from one specific folder

cd /usr/local/PEAR
phpunit

^^ WORKS

cd ~
phpunit

^^ Returns

Warning: require_once(PHPUnit/Util/Filter.php): failed to open stream: No such file or directory in /usr/local/bin/phpunit on line 46

Fatal error: require_once(): Failed opening required 'PHPUnit/Util/Filter.php' (include_path='.:/usr/lib/php') in /usr/local/bin/phpunit on line 46

So close yet so far... I'm wondering if anyone has some input on getting this guy working right?

Alex C
  • 16,624
  • 18
  • 66
  • 98
  • Most people should skip the accepted answer and [use Homebrew instead](https://stackoverflow.com/a/36347339/114558). – rinogo Nov 05 '18 at 20:14
  • homebrew does NOT work on mojave ... fyi (could not create symlink error) – gvanto Jan 14 '20 at 14:07

7 Answers7

179

To install via terminal:

curl https://phar.phpunit.de/phpunit.phar -L -o phpunit.phar

chmod +x phpunit.phar

mv phpunit.phar /usr/local/bin/phpunit
Andy
  • 3,141
  • 3
  • 27
  • 22
32

We can install it using Homebrew

$ brew install phpunit
Mike Nguyen
  • 1,024
  • 1
  • 11
  • 16
  • 3
    More precisely `brew install homebrew/php/phpunit ` – Petr Peller May 24 '16 at 15:57
  • 3
    @PetrPeller's comment is less precise in 2021. `Error: homebrew/php was deprecated. This tap is now empty and all its contents were either deleted or migrated.`. But `brew install phpunit` works – lewis Jan 22 '21 at 10:07
16

Via homebrew (only if you've installed php via homebrew as well):

brew tap josegonzalez/php
brew install phpunit
Nathan Kot
  • 2,392
  • 1
  • 21
  • 31
13

Ahhh.... OK ... I think I might have gotten it working now.

the answer was present in the PHPUnit documentation. http://www.phpunit.de/manual/3.4/en/installation.html

After the installation you can find the PHPUnit source files inside your local PEAR directory; the path is usually /usr/lib/php/PHPUnit.

The Mac version of PEAR that I was running, did install PHPUnit if I ran all the commands listed under the "SUDO" user eg:

sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover pear.symfony-project.com
sudo pear upgrade-all

At this point, all the files are downloaded to /usr/local/pear but the command

phpunit

is looking to include files from /usr/lib/php/PHPUnit The solution? To copy the PHPUnit folder from

cp /usr/lib/pear/PHPUnit /usr/lib/php

OR

make a symlink

cd /usr/lib/php
ln -s /usr/lib/pear/PHPUnit PHPUnit

I've seen a lot of people with similar problems, but this particular solution hadn't come up in any of the threads I've seen. Hopefully of use to you :) -Alex

Alex C
  • 16,624
  • 18
  • 66
  • 98
  • 5
    I would recommend setting the include_path in your php.ini file to have the additional directory over the symlink, it is a little more conventional - including on OSX. Works for me. This is also what is recommended in the PEAR docs. – Danny Staple Jan 06 '11 at 22:58
5

You can try installation with Composer. In this way you would isolate the PHPUnit version for the current project avoiding possible problems that might arise when using a single system-wide setup for different projects. As the PHPUnit documentation states (http://phpunit.de/manual/current/en/installation.html#installation.composer), installation is quite easy.

Add the dependency to your composer.json file:

  {
     "require-dev": {
        "phpunit/phpunit": "4.0.*"
      }
  }

Then update dependencies:

composer update

And PHPUnit is ready to use by running :

./vendor/bin/phpunit

Remember to adjust this path if you change the composer install path, which defaults to 'vendor'.

jaime
  • 520
  • 5
  • 15
3

Install PHPUnit

curl https://phar.phpunit.de/phpunit.phar -o phpunit.phar

chmod +x phpunit.phar

mv phpunit.phar /usr/local/bin/phpunit

OR if you have already install PHPUnit then just try this line to update PHPUnit by terminal

 phpunit --self-update

this will update your phpunit.phar file.

Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
2
  1. Download phpunit manually from: https://phar.phpunit.de/phpunit.phar
  2. Move to downloaded folder: $ cd /to/the/download/directory
  3. Rename downloaded phpunit to the phpunit.phar
  4. $ chmod +x phpunit.phar
  5. $ sudo mv phpunit.phar /usr/local/bin/phpunit
Pratik Patel
  • 2,209
  • 3
  • 23
  • 30
Mohammed Jafar
  • 493
  • 6
  • 4