You are previewing Instant Hands-on Testing with PHPUnit How-to.
O'Reilly logo
Instant Hands-on Testing with PHPUnit How-to

Book Description

A practical guide to getting started with PHPUnit to improve code quality

  • Learn something new in an Instant! A short, fast, focused guide delivering immediate results

  • Discover how to make best use of PHPUnit in your projects

  • Get started with code testing using PHPUnit in no time

  • Learn how to use PHPUnit like a seasoned veteran

In Detail

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

  1. Instant Hands-on Testing with PHPUnit How-to
    1. Instant Hands-on Testing with PHPUnit How-to
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Instant Hands-on Testing with PHPUnit How-to
      1. Installing PHPUnit (Simple)
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Installing PHPUnit using Composer
          2. Installing PHPUnit on older versions of PHP
      2. Writing your first test (Simple)
        1. Getting ready...
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Class names
      3. Running tests (Simple)
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Command line options
            1. --colors
            2. --stop-on-error and --stop-on-failure
            3. --debug
            4. --strict
      4. Configuring PHPUnit (Simple)
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Additional configurations
          2. Using phpunit.xml.dist
      5. Adding PHPUnit to your project (Simple)
        1. Getting ready...
        2. How to do it…
        3. How it works…
        4. There's more...
      6. Generating tests from code (Advanced)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using @assert to generate additional code
          2. Using the Skeleton Generator for test-driven development
      7. Using test fixtures (Simple)
        1. How to do it...
        2. How it works...
        3. There's more...
      8. Using data providers (Intermediate)
        1. How to do it...
        2. How it works...
          1. Identifying test failures
      9. Using test dependencies (Advanced)
        1. How to do it...
        2. How it works...
          1. Multiple test dependencies
          2. Multiple dependent tests
      10. Using mock objects (Simple)
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Thoughts on partial mocks
          2. Ignoring parameters on method expectations
          3. Stubbing exceptions
          4. Stubbing multiple return values
          5. Stubbing with callbacks
          6. Using mock builders
          7. Using alternative mock frameworks
            1. Phake
            2. Mockery
      11. Testing abstract classes (Intermediate)
        1. How to do it...
        2. How it works...
          1. Abstract classes in Phake
      12. Testing traits (Intermediate)
        1. How to do it...
        2. How it works...
      13. Testing exceptions and errors (Intermediate)
        1. How to do it...
        2. How it works...
        3. There's more...
      14. Testing output (Intermediate)
        1. How to do it...
        2. How it works...
        3. There's more...
      15. Testing protected and private methods (Intermediate)
        1. How to do it...
        2. How it works...
          1. Private and protected methods
      16. Testing database interaction (Advanced)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Alternate data sets
            1. Flat XML data set
            2. MySQL XML data set
            3. YAML data set
          2. Validating your data with queries
      17. Viewing code coverage (Advanced)
        1. Getting ready
        2. How to do it...
        3. How it works...