How to install PHPUnit on windows

Installing PHPUnit on Windows operationg system is really painful.

I was thinking like the above line last 2 days, since I installed and run phpunit command globally.

In order to install it on your local machine, you need to setup composer first. How to install composer on windows is not part of this tutorial. Thats why skipping that part. But this link might be helpful for you.

If you want to install phpunit globally, you need to add -g (global) command with other part of the installation command.

composer global require phpunit/phpunit

It will install latest version of phpunit globally in your system.

After the successful installation you can check the version of phpunit.

phpunit --version

This command will display the version like below.

PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

At the time of this writing 9.5.10 is the latest version of phpunit.

Seems so easy, Right !

If you are using xampp as a web server in your system, running phpunit command from terminal ended some error like below.

PHP Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\php\pear\PHPUnit\TextUI\Command.php on line 277

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\php\pear\PHPUnit\TextUI\Command.php on line 277
PHP Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\php\pear\PHPUnit\TextUI\Command.php on line 285

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in C:\xampp\php\pear\PHPUnit\TextUI\Command.php on line 285
PHPUnit 3.7.21 by Sebastian Bergmann.

The error says that you are using 3.7.21 version of PHPUnit.

But you install the latest version of PHPUnit, right ! Yes, you have installed the latest version of PHPUnit. But that version comes from the old version fo PHPUnit, which had installed while you install xampp in your system.

Solve

You need to update your PHPUnit version to the latest version. Here are few simple steps you need to follow in order to solve this problem.

Step 1: Download the PHAR file of latest version of PHPUnit or whatever version you want. This article is meant to using the latest version of PHPUnit, at time of writing. Here is the link to download.

Step 2: Copy that file and paste that into the PHP directory of xampp. PHP directory in xampp might be like below.

C:\xampp\php

Now rename that downloaded file to phpunit.phar

Step 3: Now you need to edit phpunit.bat file. Open that file with any code editor and replace like below.

Main line 
"%PHPBIN%" "C:\xampp\php\phpunit" %*

New line
"%PHPBIN%" "C:\xampp\php\phpunit.phar" %*

No need to restart apache from xampp control panel.

Now run phpunit from your terminal. It will display the perfect version like below and many other stuff.

phpunit / phpunit -- version
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Usage:
  phpunit [options] UnitTest.php
  phpunit [options] <directory>

If you face any other problem please comment below.

Here is official site of PHPUnit.

Thats all for this article. Thanks for reading.