Book Image

Instant Hands-on Testing with PHPUnit How-to

By : Michael Lively
Book Image

Instant Hands-on Testing with PHPUnit How-to

By: Michael Lively

Overview of this book

No developer wants to accept the inherent difficulty of writing software as an excuse for not finding the bugs in our code before anyone else does. PHPUnit is a framework that was created to allow developers to solve that very problem. It provides a feature-rich environment with most of the tools necessary to provide adequate tests for any project. "Instant Hands-on Testing with PHPUnit How-to" provides a thorough overview of the functionality provided by the PHPUnit framework. It shows how the plethora of features in the framework can be used to write tests for real world projects to ensure they function and will continue to function in the ways that you expect. This book will show how you can set up the scaffolding necessary to run unit tests in your project with PHPUnit. It will walk you through the process of how to write a basic test and how to maintain your project's test suite. You will learn how to use some of the more advanced features of PHPUnit and then see how you can use mock objects to isolate the code you are testing. We will then discover how to create tests that verify your interaction with databases and even see how you can use PHPUnit to understand which code you are actually testing. At the end of the book you will have all of the basic understanding necessary to begin adding tests to your project. This book provides a great foundation for becoming a expert at writing unit tests.
Table of Contents (7 chapters)

Testing exceptions and errors (Intermediate)


A negative test is a test that is created to show error conditions or exceptions from the system under test. Negative tests can be easy to ignore. However, it is not only important to make sure your code works the way it is supposed to but it is also important to know that it also fails the way it is supposed to.

Fortunately, PHPUnit provides very easy to use functionality to help ensure that your code is throwing errors and exceptions at the appropriate time.

How to do it...

This functionality can be shown through some negative tests for the following code:

<?php
abstract class Player
{
  // ...

  public function requestCard()
  {
    $cardNumber = $this->chooseCardNumber();

    if (!$this->hasCard($cardNumber))
    {
      throw new RuntimeException('Invalid card chosen by player');
    }

    return $cardNumber;
  }

  // ...
}

To properly test that the exception is being thrown we can write the following test:

<?php
class PlayerTest extends PHPUnit_Framework_TestCase
{
  public function testRequestCardThrowsOnInvalidCard()
  {
    $this->player->expects($this->once())
        ->method('chooseCardNumber')
        ->will($this->returnValue('2'));

    $this->setExpectedException('RuntimeException', 'Invalid card chosen by player');
    $this->player->requestCard();
  }
}

How it works...

You can test that your code throws an exception using the setExpectedException() method. This tells PHPUnit to make sure that a specified exception is thrown before the test is finished. It takes the fully qualified class name of the exception as the first parameter. You can specify an optional second and third parameter with the expected message and code for the exception. If either of these parameters are not specified then the message and code will not be checked.

In this test, you are setting up the player class to choose a card number that does not currently exist in the hand. When this occurs a RuntimeException should be thrown with the message Invalid card chosen by player when Player::requestCard() is called. In the event that the error doesn't get thrown the test will fail.

There's more...

PHPUnit also allows you to specify expected exceptions using annotations.

/**
 * @expectedException RuntimeException
 * @expectedExceptionMessage Invalid card chosen by player
 */
public function testRequestCardThrowsOnInvalidCardUsingAnnotation()
{
  $this->player->expects($this->once())
      ->method('chooseCardNumber')
      ->will($this->returnValue('2'));

  $this->player->requestCard();
}

Instead of using the setExpectedException() method you can use the @expectedException annotation. The @expectedException annotation accepts the fully qualified class name of the exception that should be thrown. The @expectedExceptionMessage annotation accepts the message that should be set on the exception. There is also an @expectedExceptionCode annotation that can be used to set an exception code if necessary.