Installation of PHP Unit
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 test and testing
Next:
Writing tests for phpunit (data providers)
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework