Installing PHPUnit for Unit Testing in PHP (win+linux)

The simplest way to get PHPUnit is to download PHPUnit’s PHP Archive (PHAR), which bundles all the necessary components PHPUnit needs (plus some optional ones) into a single file:

To use the PHP Archive (PHAR) you need the phar extension.

To use PHAR’s —self-update feature you need the openssl extension.

If the Suhosin extension is enabled, you need to allow PHAR execution in php.ini:

suhosin.executor.include.whitelist = phar

To install PHAR globally:

$ wget https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.

You can also just use the downloaded PHAR file directly:

$ wget https://phar.phpunit.de/phpunit.phar
$ php phpunit.phar --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.

Windows

Overall, installing PHAR on Windows is the same process as manually installing Composer on Windows:
  1. Create a directory for PHP's binary executable, for example C:\bin
  2. Append ;C:\bin to the PATH environment variable (related help)
  3. Download https://phar.phpunit.de/phpunit.phar and save the file to C:\bin\phpunit.phar
  4. Open a command line (for example, press Windows+R » type cmd » ENTER)
  5. Create an outer wrapper batch script (you end up with C:\bin\phpunit.cmd):
    C:\Users\username>cd C:\binC:\bin>echo @php "%~dp0phpunit.phar" %* > phpunit.cmdC:\bin>exit
  6. Open a new command line window and confirm that you can run PHPUnit from any path:
    C:\Users\username>phpunit --versionPHPUnit x.y.z by Sebastian Bergmann and contributors.
PHPUnit official Chinese manual https://phpunit.de/manual/current/zh_cn/installation.html
Download address: https://phar.phpunit.de/