O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PHPUnit Essentials

Book Description

Get started with PHPUnit and learn how to write and test code using advanced technologies

In Detail

The ability to write unit tests and software testing have become skills that every PHP developer should master.

This book is a practical guide to PHPUnit and unit testing, covering all aspects of writing PHPUnit tests and using them. The book shows why testable code is better code and how to write good tests with the help of simple and easy-to-understand examples.

With this book, you will learn how to write, organize, and execute effective tests. Step-by-step techniques of how to write testable code, how to refactor the code, and how to run your tests are shown. You will also learn about advanced testing techniques, including how to test databases, APIs, and legacy code. PHPUnit Essentials is a guide for PHP developers who want to learn or improve their software testing skills. It is a book for developers who begin with testing but is also a good source of information for developers who are already familiar with PHPUnit.

What You Will Learn

  • How to install PHPUnit and confi gure IDEs for PHPUnit support
  • What are unit tests and why to test code
  • Write test doubles and learn how to replace dependencies
  • Explore how to test legacy code
  • Test code calling APIs and connecting to the database
  • Run functional tests in a web browser using Selenium tests
  • Use tests as part of the continuous integration process
  • Discover the alternatives to PHPUnit
  • Use PHPUnit tests in the Jenkins and Travis CI servers

Downloading the example code for this book. 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.

Table of Contents

  1. PHPUnit Essentials
    1. Table of Contents
    2. PHPUnit Essentials
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. 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. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Installing PHPUnit
      1. Requirements
        1. Running PHP from the command line
      2. Composer – the dependency manager for PHP
        1. Installing Composer
        2. Installation
          1. Local installation
          2. System-wide installation
      3. Installing PEAR
        1. PHPUnit installation
      4. Other installation methods
        1. Installing the Linux package
        2. Manual installation
      5. Testing the installation
        1. Xdebug
          1. Installing Xdebug
      6. Summary
    9. 2. PHPUnit Support in IDEs
      1. IDEs and PHPUnit
        1. NetBeans
        2. Zend Studio
        3. Eclipse PDT
          1. Installing MakeGood
          2. Creating your FirstTest
        4. PhpStorm
      2. Summary
    10. 3. Tests and What They're All About
      1. Understanding unit testing
        1. What is a unit test?
        2. Assertions
        3. The importance of unit testing
        4. Testing all possible scenarios
        5. What makes a good test?
        6. When to write tests
      2. Anatomy of a PHPUnit test
        1. Defining test methods
        2. Testing functions
        3. Testing methods
      3. The MVC application architecture and tests
        1. Testing controllers
      4. Summary
    11. 4. Testing Dependencies and Exceptions
      1. Detecting dependencies
      2. Handling dependencies
      3. Exceptions are expected
        1. Testing errors and exceptions
      4. Summary
    12. 5. Running Tests from the Command Line
      1. Running tests
        1. Processing test results
          1. Test statuses
        2. Command-line switches
          1. Logging results
          2. Code coverage
          3. Including and excluding tests from the execution
          4. When to stop the test execution
          5. Configuration options
        3. Code coverage analysis
      2. Summary
    13. 6. Test Isolation and Interaction
      1. Test fixtures
        1. Before and after each test method
        2. Before and after each test suite class
      2. Global state
      3. Test dependencies
      4. Data providers
      5. Summary
    14. 7. Organizing Tests
      1. The PHPUnit XML configuration file
        1. Test listeners
        2. Configuring the code coverage
      2. Where to store tests
        1. Test suites
        2. Groups
      3. Using the bootstrap file
      4. Summary
    15. 8. Using Test Doubles
      1. Creating test doubles
      2. Test doubles in action
        1. Using fake
        2. Using stubs
        3. Using mocks and expectations
        4. Test proxies
      3. Understanding Mockery
        1. Installation methods
        2. Comparing Mockery to PHPUnit
        3. How to use Mockery
      4. Summary
    16. 9. Database Testing
      1. Which database to use
      2. Tests for a database
      3. DBUnit
        1. Installing DBUnit
        2. Database test cases
        3. Datasets
        4. Using DBUnit
      4. Doctrine 2 ORM and database testing
      5. Summary
    17. 10. Testing APIs
      1. An example of integration testing
      2. Testing the PayPal API
      3. Testing the Facebook API
      4. Testing the Twitter API
      5. Testing the service-oriented architecture
      6. Summary
    18. 11. Testing Legacy Code
      1. Testing spaghetti code
        1. Black box testing
        2. Using Reflection
      2. Handling dependencies
        1. The Patchwork library
        2. The vfsStream library
        3. The runkit PHP extension
      3. Summary
    19. 12. Functional Tests in the Web Browser Using Selenium
      1. Installing Selenium
        1. The Selenium IDE
        2. The Selenium Server
          1. Installing drivers
        3. The PHPUnit Selenium extension
      2. Testing in the browser
        1. Recording Selenium tests
        2. PHPUnit Selenium2TestCase
      3. Writing Selenium tests
      4. PHP-SeleniumClient
      5. Organizing Selenium tests
      6. Summary
    20. 13. Continuous Integration
      1. Using a Travis CI hosted service
        1. Setting up Travis CI
        2. Using Travis CI
      2. Using the Jenkins CI server
        1. Installation
        2. Usage
          1. Creating a job
            1. Source Code Management
            2. Build Triggers
            3. Build
            4. Post-build Actions
          2. Results
      3. Using the Xinc PHP CI server
        1. Installation
        2. Usage
      4. Summary
    21. 14. PHPUnit Alternatives, Extensions, Relatives, and BDD
      1. Unit testing alternatives
      2. PHPUnit extensions
      3. Behavior-driven development
        1. Understanding BDD
        2. Testing with PHPSpec
          1. Installing PHPSpec
          2. Using PHPSpec
        3. Functional testing with Behat
          1. Installing Behat
          2. Using Behat
      4. Summary
    22. Index