PHPUnit is a great framework for unit testing. It comes with PEAR which is quite easy to install if you own your own server but can be a headache if you are on a shared host. You need a SSH access to your server for this tutorial (French OVH pro shared hosting provides it)
First PEAR
Follow these steps if you don’t have PEAR installed
- Download a copy of http://pear.php.net/go-pear.phar and put it in a /pear/download directory on your server using FTP
- Using SSH navigate to this directory and execute this command : php go-pear.phar If you get any warning about wrong or missing signature try to use this command : php – d phar.require_hash=0 go-pear.phar
- If the PHP version is < 5.3.0, you’ll probably get an error. Execute cd /usr/local/bin and then dir. It’ll show you different versions of PHP installed on the server. You can pick the one you want. Go back to /pear/download and replace php by the right one in the previous command (step 2)
- Answer the question and set the configuration to the right directories : almost all should start by /pear except php binaries and some others
Second check PEAR config
The second step is to check PEAR config using pear config-show. PEAR folders should start by /pear otherwise you’ll get an error when trying to install the packages because of missing write rights. You can edit a configuration element using pear config-set (use pear help config-set for details)
Installing the packages
Now you have to download all the stuff to your server to process to the offline installation.
- Download a copy of http://pear.phpunit.de/channel.xml and save it in /pear/download under phpunit.xml
- Download a copy of http://pear.symfony-project.com/channel.xml and save it in /pear/download under symfony.xml
- Download PHPUnit package and its dependencies. For instance, if you go to http://pear.phpunit.de/ and check for PHPUnit dependencies you will find pear.phpunit.de/File_Iterator, pear.phpunit.de/Text_Template, pear.phpunit.de/PHP_CodeCoverage, pear.phpunit.de/PHP_Timer, pear.phpunit.de/PHPUnit_MockObject, pear.symfony-project.com/YAML. So you will have to download all these packages. For each one pick the latest release.
- Upload all you have downloaded to /pear/download using FTP
- Navigate using SSH to /pear/download
- Add both phpunit and symfony channels executing pear channel-add phpunit.xml and pear channel-add symfony.xml
- Install packages starting with ones with no dependencies using pear install –offline –nodeps package_name
You’re done with the installation
Using PHPUnit
In your PHP code you have to do theses steps before starting your tests :
- Add /pear to PHP include_path with set_include_path(get_include_path() . PATH_SEPARATOR . ‘/pear’);
- Include PHPUnit autoload function require(‘PHPUnit/Autoload.php’);
Enjoy unit testing !