Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Hands-on Testing with PHPUnit How-to

You're reading from   Instant Hands-on Testing with PHPUnit How-to A practical guide to getting started with PHPUnit to improve code quality

Arrow left icon
Product type Paperback
Published in May 2013
Publisher Packt
ISBN-13 9781782169581
Length 82 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Michael Lively Michael Lively
Author Profile Icon Michael Lively
Michael Lively
Arrow right icon
View More author details
Toc

Installing PHPUnit (Simple)


The first step to understanding how to test with PHPUnit is to understand how to make it available in our environment. In this recipe, we will install PHPUnit using the PHP Extension and Application Repository (PEAR) package manager. While PEAR has been available to PHP developers for some time, new packaging frameworks such as Composer have become increasingly more common. We will also cover these additional packaging methods.

How to do it...

Execute the following commands in a shell:

sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/PHPUnit

How it works...

Installing via PEAR will give you access to PHPUnit across all projects in your environment. PHPUnit uses its own PEAR server for distribution. These are called channels. By default, PEAR is only aware of its own channel. The auto_discover setting tells PEAR that any time a package from a new channel is requested, it should automatically register that channel. Otherwise, each new channel would have to be added explicitly using pear channel-discover. Not only does PHPUnit itself have its own channel, but some of its dependencies are on other custom channels. This is why we use pear config-set to enable auto_discover. Because we are using auto_discover, we only need to run pear install to complete the installation.

There's more...

The standard PEAR installation of PHPUnit should provide everything you need to start writing tests. PEAR is a convenient way to install PHPUnit at the environment level. You may find that installing PHPUnit using a dependency manager called Composer works better for your needs.

Installing PHPUnit using Composer

It is becoming more and more common in the PHP world to bundle everything that you need to run and develop an application at the application level. Composer is a dependency manager for PHP that works well in handling this concept. To understand how Composer works and how you can integrate it into your project, read the Getting Started guide on their website at http://getcomposer.org. Once you have Composer integrated into your site, you can add the following package to your composer.json file:

{
    "require-dev": {
        "phpunit/phpunit": "3.7.*" 
   }
}

This will set up PHPUnit as a development requirement for your package. This works well as you typically don't need your end users to run your tests. Also, you should always double check the PHPUnit page on packagist https://packagist.org/packages/phpunit/phpunit to see what the latest version is. Please note that the remaining examples in this book make use of the PEAR installed version of PHPUnit.

Installing PHPUnit on older versions of PHP

Currently, the latest version of PHPUnit is 3.7. This version requires PHP 5.3.3 or higher. If you find yourself using an older version of PHPUnit, due to a bug in PEAR you may have an issue attempting to install PHPUnit.

Duplicate package channel://pear.phpunit.de/File_Iterator-1.3.3 found
Duplicate package channel://pear.phpunit.de/File_Iterator-1.3.2 found
install failed

If you see these errors then you need to explicitly tell PEAR to install these packages in addition to the PHPUnit package. If you see this error with File_Iterator, you will likely see the same error with Text_Template as well.

sudo pear install pear.phpunit.de/File_Iterator pear.phpunit.de/Text_Template pear.phpunit.de/PHPUnit

This will provide PEAR the information that it needs to be able to install all of the appropriate packages. If you are using a newer version of PHP (5.3.3 or higher) you shouldn't have this problem.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image