Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Instant Hands-on Testing with PHPUnit How-to

You're reading from  Instant Hands-on Testing with PHPUnit How-to

Product type Book
Published in May 2013
Publisher Packt
ISBN-13 9781782169581
Pages 82 pages
Edition 1st Edition
Languages
Author (1):
Michael Lively Michael Lively
Profile icon Michael Lively

Preface

The goal of almost every software developer is to create code that is free from defects and works as intended. To help ensure this goal is met we spend a significant amount of time testing the code that we have written. As software grows in size and complexity the likelihood of defects being introduced increases. Naturally, we have to increase our efforts in testing to help accommodate the increased risk.

When I first began writing software I would test my applications by manually running through a series of steps on a deployed application. I would add items to my cart, I would remove them again, I would manually check to make sure taxes and shipping costs were calculated correctly. I spent a lot of time looking at as much functionality as I could think of. Commonly, I would miss some less obvious pieces of functionality and small bugs would slowly creep into the code as the software aged and my memory of the intricate details of how it worked faded.

Fortunately, I was introduced to the concept of automated testing fairly early on in my career. Automated testing, specifically automated unit testing is a powerful tool in any programmer's arsenal. It allows us to have an easily repeatable way to check and make sure the software we write is working the way we intend. It provides future maintainers of the code with the ability to have confidence that they can change functionality and not be worried that they will regress the code back to old errors. It gives us freedom as software developers to focus more time on moving our software forward instead of living in fear that any change we make will cause the system to break.

There are many tools to help you write effective unit tests quickly. In PHP, the leading tool is PHPUnit. In this book, we are going to learn how you can use PHPUnit in your project to create a test suite that can give you an increased level of confidence in the software you are writing.

What this book covers

Installing PHPUnit (Simple) will teach you how to install PHPUnit using the PEAR package manager. You will also learn about some alternative ways to install PHPUnit.

Writing your first test (Simple) will help you create a basic test and learn about the common phases of a unit test written in PHPUnit.

Running tests (Simple) will show you how to run your tests using the command line tool and gain some insight into the various options the tool gives you.

Configuring PHPUnit (Simple) will show you how to move the configuration of PHPUnit from the command line to configuration files that can easily be shared as a part of your project.

Adding PHPUnit to your project (Simple) will teach you the steps necessary to integrate unit tests into your project.

Generating tests from code (Advanced) will show you how the PHPUnit's Skeleton Generator can be utilized to generate tests from existing code. You will even see how you can generate code stubs from tests for true test-driven development.

Using test fixtures (Simple) will teach you how to use shared fixtures to reduce code duplication and to reduce the code necessary to set up new tests.

Using data providers (Intermediate) will show you how data providers can be leveraged to rapidly create test cases that validate a variety of calls using separate data points.

Using test dependencies (Advanced) will show you how you can isolate failed tests by using a consumer-producer pattern inside your test cases. This will allow you to spend less time determining which piece of functionality caused your test to fail.

Using mock objects (Simple) will teach you how to leverage mock objects to keep the unit of code being tested small.

Testing abstract classes (Intermediate) will show you how PHPUnit's mock functionality can be used to test abstract classes.

Testing traits (Intermediate) will teach you how you can use PHPUnit to test traits in a very simple yet dynamic way.

Testing exceptions and errors (Intermediate) will show you how you can ensure that the proper errors and exceptions are being thrown from code at the correct time. It is just as important to know that your application fails properly as it is to know it works properly.

Testing output (Intermediate) will teach you how you can leverage PHPUnit's output buffering features to ensure your code is outputting text correctly to your end users.

Testing protected and private methods (Intermediate) will teach you a sound strategy for testing private and protected methods.

Testing database interaction (Advanced) will show you how PHPUnit can be used to ensure your application is working properly with your database.

Viewing code coverage (Advanced) will teach you how PHPUnit's code coverage reporting can give you clues on where both the well tested and the not-so-well tested code resides in your system.

What you need for this book

The examples in this book were written using PHP 5.3.24 and PHPUnit 3.7. All code samples were verified against a Linux box with Ubuntu 12.04 LTS.

Who this book is for

This book is written for anyone who has an interest in unit testing but doesn't necessarily know where to start in integrating it with their project. It will provide useful tips and insights into how PHPUnit can be used with your projects and it should give you enough information to whet your appetite for the various features offered by PHPUnit.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The auto_discover setting tells PEAR that any time a package from a new channel is requested, it should automatically register that channel."

A block of code is set as follows:

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

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<php>
  <includePath>src</includePath>
  <const name="DB_DSN" value="sqlite:data/game-test.db" />
</php>

Any command-line input or output is written as follows:

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

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.

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 $15.99/month. Cancel anytime}