Step-by-Step Guide to Installing PHPUnit
Introduction
In this tutorial, we will look at the setting up of the latest version of PHPUnit on our local machine, ready to start writing some PHPUnit tests.
#Requirements
PHPUnit 8.0, as will be used in this tutorial recommends at least a minimum of PHP 7.2 for optimum performance. Dom and JSON extensions which are already enabled by default as from PHP 5.0 is also strongly recommended for PHPUnit to run on our local machine.
PHPUnit also requires reflection, pcre and spl extensions which come bounded and enabled by default in PHP. These extensions cannot be disabled without patching PHP’s build system and its C sources.
In addition to the above mentioned extensions, PHPUnit code coverage report requires Xdebug version 2.7 or above and tokenizer extensions. Xmlwriter extension is also required in PHPUnit for generating XML reports.
#PHP Archive (PHAR)
The PHP Archive (PHAR) is the easiest way to obtain PHPUnit. The PHP Archive (PHAR) has all required dependencies as well as some optional dependencies of PHPUnit all bundled in a single file.
To use PHP Archives (PHAR), the phar extension is required, which could be easily enabled in the “php.ini” file if we have Suhosin extension enabled, as shown in the following code snippet.
To globally install PHAR we can run the following command on a Linux terminal
We can also download PHAR directly from the terminal using the following commands:
#Installation of PHAR in windows:
Globally installation of PHAR on a windows machine involves following diligently the following steps:
- Create a directory for PHP binaries e.g: “C:\bin”
- Append ;C:bin on our PATH environment variable.
- Download PHAR and save it inside the C:\bin folder we created as C:\bin\phpunit.phar
- Open the windows terminal and create a wrapping batch script with the following commands:
- Opening a new terminal, to confirm if our setup is complete by executing PHPUnit from any path as shown:
For Cygwin and/or MingW32 (e.g., TortoiseGit) shell environments, we may skip step 4. above, and save the downloaded file as phpunit (without .phar extension), and make it executable via chmod 775 phpunit
#Verification of PHPUnit PHAR Releases
Official releases of PHPUnit updates are signed by the release manager. The signature usually in PGA and SHA1 are available for verification at phar.phpunit.de.
#Adding Composer to PHPUnit
To use the composer, we add a (development-time) dependency on phpunit/phpunit to our project’s composer.json file. We can require this by running the following commands:
Optional packages
The following optional packages are available and can be installed using composer
- PHP_Invoker
A utility class for invoking callables with a timeout. This package is required to enforce test timeouts in strict mode.
This package is included in the PHAR distribution of PHPUnit. It can be installed via Composer using the following command:
In the next tutorial, since we already have PHPUnit installed, we will proceed with writing PHP unit tests. Don’t forget to like and share with your friends.
Previous:
A Gentle introduction to Unit Testing in PHP.
Next:
Writing Tests for PHPUnit Using Data Providers.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/PHPUnit/installation-of-php-unit.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics