Unisys12's Blog

My adventures, trials & tribulations of learning web development as a hobby! I mean... who does that?

Installing PHPUnit on Windows

Phillip Jackson - 2014-04-02 01:38:42
Easiest way I have found to get start with PHPUnit

Getting Started with Testing

About 2 years ago or so, I said that I needed to learn TDD. That’s Test Driven Development.I won’t go into what it is or what it’s for here. Or even really later. It’s a topic that has been beat into my brain so much, I am sick of thinking about it and just want to get on with it. I know it is something I need to learn and do. But I had such a hella awful time getting it to work, I just gave up. Even had a Twitter buddy give me some advice and I think I finally got it working, but I had spent so much effort up to that point, I just said to hell with it. But not now, no sir. This puppy is going down tonight come hell our high water!

PHPUnit on Windows

So, there are tons of tutorials out there showing how easy it is getting PHPUnit on /*nix systems. Well, guess what? If you are using Composer in your projects, then you can have it just as easy in any Windows enviroment as well. Just following these simple steps and you will be on your way!

Install Packages Globally Through Composer

I might have read about this before, but forgot all about it if I did. You can actually install packages globally on your system, which can then be accessed anywhere on your system from the command line. Just like you can do with Bower and Node. Sweet!

First, we need to add Composers vendor/bin directory to our system path. In Windows, this is typically C:\Users\\AppData\Roaming\Composer\Vendor\Bin. To do this -

  1. Click your Windows keys

  2. Right click on “Computer”

  3. Click on “Properties”

  4. On the left hand side of the windows, click on “Advanced System Settings”

  5. Click the button at the bottom of the window titled “Environment Variables”

  6. PATH, under ‘User Variables’ will already be highlighted in blue, so just double click it

  7. Press the right arrow key to jump to the end of the line

  8. Add a semi-colon(;) to the end of the line, making sure that there is not a space before or after the semicolon (very important)

  9. Either end your path manually or open Windows Explorer and traverse to the location mentioned above. Once in the Bin directory, right click in the address bar and choose “Copy path as text”. Finally, paste that into the PATH dialog right after the semi-colon. Remember! No Stinking Spaces!!

  10. Click “OK”. And “OK” again. One last time, “OK” again.

Downloading PHPUnit

Open your command line of choice, it really doesn’t matter, and enter following:

composer global require phpunit/phpunit=~4.0

This will run through the normal process of downloading PHPUnit, latest version above 4.0, along with any required dependencies. You will notice, as soon as you click enter on the above command, that Composer actually switches to your local vendor/bin directory. Hence the reason we added that directory to our path. Once the download is complete, we can enter the following into our command line...

phpunit --version

and this is what I got as a result...

PHPUnit 4.0.14 by Sebastian Bergmann

… which clearly verifies that it works.

A Quick Note

I found this method of installation in the docs of PHPUnit, but theirs have single quotes wrapping the ‘packagename/packagename=version’. This caused Composer to spit back a parser error, for me. I just removed the single quotes and all was good. Also, in the docs, they use a ‘*’ for the versioning. I tend to the like the tilde a bit better, so… that’s just my preference.

comments powered by Disqus