Running PHPunit PHAR from anyware under Windows CLI

1- Download https://phar.phpunit.de/phpunit.phar and move it to a folder
For example : “D:\Tools\phpunit\phpunit.phar”

2- Add “D:\Tools\phpunit\” to your Windows PATH

Unfortunately this will work only if you execute phpunit from “D:\Tools\phpunit\”, trying to do it from another folder (Obviously your project folder aka “D:\Workspace\MyAwesomeProject”) will not work


 php phpunit.phar . Could not open input file: phpunit.phar

To fix this, just create a new file “phpunit.bat” in the same folder as “phpunit.phar” with the following content

@ECHO OFF
php “%~dp0phpunit.phar” %*

More about the weird %~dp0 :
http://htipe.wordpress.com/2008/10/09/the-dp0-variable/

If you are using cygwin/mintyy, create also a file “phpunit”

#!/bin/sh

dir=$(d=$(dirname “$0”); cd “$d” && pwd)

# if running in cygwin
if command -v ‘cygpath’ >/dev/null 2>&1; then
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m $dir);
fi
fi

dir=$(echo $dir | sed ‘s/ /\ /g’)
php “${dir}/phpunit.phar” $*

phpunit.phar-from-everywhere_3

Now you can call phpunit.bat or directly phpunit if using cygwin


phpunit --version PHPUnit 4.1.0 by Sebastian Bergmann.  $ phpunit --colors MoneyTest PHPUnit 4.1.0 by Sebastian Bergmann.  .  Time: 39 ms, Memory: 4.00Mb  OK (1 test, 1 assertion)