Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.

Installation of PHP Unit-1

To globally install PHAR we can run the following command on a Linux terminal

Installation of PHP Unit-2

We can also download PHAR directly from the terminal using the following commands:

Installation of PHP Unit-3

#Installation of PHAR in windows:

Globally installation of PHAR on a windows machine involves following diligently the following steps:

  1. Create a directory for PHP binaries e.g: “C:\bin”
  2. Append ;C:bin on our PATH environment variable.
  3. Download PHAR and save it inside the C:\bin folder we created as C:\bin\phpunit.phar
  4. Open the windows terminal and create a wrapping batch script with the following commands:
  5. Installation of PHP Unit-4
  6. Opening a new terminal, to confirm if our setup is complete by executing PHPUnit from any path as shown:
  7. Installation of PHP Unit-5

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:

Installation of PHP Unit-6

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:

    Installation of PHP Unit-7

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