PHPUnit is a tool that allows you to write automated tests for your PHP code. One of the main advantages to writing tests is that they give you assurance that you haven’t introduced bugs into your application when making changes to your code. When you make changes to an application, you can run these tests and ensure that you haven’t accidentally introduced a bug into your app.

The PHP on IBM i culture is still pretty young. That is, there seems to be a small but growing number of PHP developers whose primary environment is on the IBM i. Therefore, while there is tons of information on the web helping you with anything you want to do in PHP, very little of it is tailored for the IBM i developer. If you’re one of these devs, this tutorial was written to help you install PHPUnit on your System i.

There are a few ways to install PHPUnit, and I’ll just cover two here. The first involves using PEAR and the second (and easiest) PHAR.

PEAR

Step 1: Start a PASE shell by entering the following from an i5/OS command line:

CALL QP2TERM

Step 2: Change target directory from /QOpenSys/usr/bin/-sh to /usr/local/zendsvr6/bin/ by entering the following:

cd /usr/local/zendsvr6/bin/

Step 3: Start a PASE command line and specify the preferred pear package state by entering the following (h/t Scott Klement here) :

pear config-set preferred_state stable

Step 4: pear.phpunit.de is the PEAR channel that is used for the distribution of PHPUnit. You will need to register this channel with the local PEAR environment:

pear channel-discover pear.phpunit.de

Step 5: Register pear.symfony.com with the PEAR environment:

pear channel-discover pear.symfony.com
pear install pear.symfony.com

Step 6: Install PHPUnit:

./pear install phpunit/PHPUnit

Now you can find the PHPUnit source files in /usr/local/zendsvr6/bin/

PHAR

The second (and IMO easiest) way to install PHPUnit is to use a PHAR file.

Step 1: Start a PASE shell by entering the following from an i5/OS command line:

CALL QP2TERM

Step 2: Navigate to /usr/local/zendsvr6/bin if you have Zend Server 6 installed and /usr/local/zendsvr/bin if you have an earlier version.

cd /usr/local/zendsvr6/bin

or

cd /usr/local/zendsvr/bin

Step 3: Download the phar file into the current directory. In chapter 3 of the PHPUnit manual, Mr. Bergmann instructs us to run the following command to download the PHAR file:

wget https://phar.phpunit.de/phpunit.phar

If you’re like me, then you received the following error message:

https://phar.phpunit.de/phpunit.phar: Unsupported scheme.

The “unsupported scheme” message refers to the fact that the default installation of wget on AIX for IBM i does not support SSL, but it does support FTP and HTTP. So, simply remove the “s” from “https” and you should be able to download the PHAR file successfully.

wget http://phar.phpunit.de/phpunit.phar

If the download succeeded then you should be able to run “ls” to display the contents of the current directory and see phpunit.phar listed.

Step 4: Add execute permissions for the phar file.

chmod +x phpunit.phar

Step 5: Install PHPUnit with

php phpunit.phar