Introduction to Testing with PHPUnit

An introduction to testing your code with PHPUnit
4.6 (57 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
316 students enrolled
Take This Course
  • Lectures 33
  • Length 5 hours
  • Skill Level Intermediate Level
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works


Find online courses made by experts from around the world.


Take your courses with you and learn anywhere, anytime.


Learn and practice real-world skills and achieve your goals.

About This Course

Published 1/2016 English

Course Description

Writing tests for modern PHP code can be intimidating for those who have never done so before, and this course is intended to remove the mystery from writing tests. We'll explore how to write tests for every part of your code -- from models, to views, to controllers, and every single line of code in your application. We'll start with very simple tests, and gradually move into more complex strategies. Initially, we will write unit tests, where we test our code at a granular level. Next we move into integration testing, we we ensure that each piece of our code interacts with other components properly. Then we look at functional testing, where we automate the process of testing the end-user's experience.

The course is structured so that all students download the same codebase, and we go through the entire process from start to finish. The source code is available on a GitHub repository, and is tagged so that you have ready access to the source code as it exists for a particular lecture.

What are the requirements?

  • You should have at least a basic knowledge of PHP
  • You should have at least a basic understanding of Object Oriented programming principles

What am I going to get from this course?

  • Write unit, integration, and functional tests in PHP
  • Generate code coverage reports
  • Learn how to refactor code to make it more testable (and better!)
  • Understand how to mock classes/objects, and why that's a good thing
  • Learn how to stub classes, methods and variables
  • Test controllers, models, views, and classes
  • Simulate visitors to your site from within PHPUnit
  • Test the functionality of every public facing page in your site
  • Write tests suites that strive for 100% code coverage

Who is the target audience?

  • This course is intended for people with some knowledge of PHP, but little or no experience writing tests
  • This course is an introduction to writing test suites. If you are looking for advanced test writing techniques, this course is probably not for you.

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.


Section 1: Why Testing is Critical to Good Development

What testing is, the different kinds of testing, and what we are going to cover

Section 2: Setting up our project files

A good Integrated Development Environment, or IDE, will make your life every so much easier. I'm going to use PHPStorm, but you don't have to buy it -- Atom will work just fine.


When we test, it is helpful to have our testing environment mirror our production environment. Vagrant & Virtualbox make this possible.

When we test, it is helpful to have our testing environment mirror our production environment. Vagrant & Virtualbox make this possible, and GitBash gives Windows users access to the same LInux commands I use in this course.

Download the source code for this course. Note that it has deliberate bugs, and I know this! We need something for our tests to find...


Let's run through the sample code, and find out how we can get the code that I write in the lectures from a Git repository.

Section 3: Setting up and using PHPUnit

Use composer to install PHPUnit


Let's configure PHP Unit and get it working with our code.

Code Coverage
Section 4: Basic Unit Testing

Let's write our first unit test.


Writing slightly more complex tests against our code base.


Our code is a bit messy, so let's optimize it.


Sometimes you know what you want to test, but not how to test it. This is a sample case.

Section 5: Getting Our Code Ready for Testing

Dependency injection has the reputation of being difficult. It isn't.


There are many to choose from, but I like one in particular. Here's why.


Getting started with dependency injection.

Section 6: Mocking Objects & Using mockBuilder & @dataProvider

Mocking makes testing ever so much easier -- and it's easy to implement.


Moving our validator class into the DI container.


Back to the Validator class, and writing tests to cover as much as we can.


Why write 10 tests when you can write one, and provide that test with ten pieces of data?


Making our tests cover even more code.

Section 7: Integration Testing

Let's test our code and see how it interacts with other parts of our code.


A sample integration test for the getRows method.


What about protected methods? Can I test those?


Testing controller methods can be daunting. Let's make it simpler.


Let's test showing a page from our site.


An example of cheating. We'll remove the cheat in the next lecture.

Section 8: Functional Testing

Functional tests allow us to simulate visitors to our site without ever firing up a web browser.


Curl is hard. Let's find an easier way to test things like forms on a web page, and let's test the session while we're at it.


Functional testing is easy, so let's write some more.

Section 9: Acceptance Testing Strategies

Acceptance testing is all about planning.

Section 10: Final Thoughts & Housekeeping

How to exclude directories (or files) from our test suite.


There are alternatives and extensions to PHPUnit. Let's have a quick look at some.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

I have twenty years of experience in professional software development, and twenty years of experience as a University level educator and researcher.

As an entrepreneur, I have worked with a broad range of clients, including Thomson Nelson, Hewlett Packard, the Royal Bank of Canada, Keybank, Sprint, and many, many others. I also have extensive management and project management experience. I have led teams of fifty developers and artists on multi-million dollar projects, and much smaller teams on much smaller projects.

As a professor, I have taught in a wide variety of course areas, including Computer Science, English, Irish, and American literature, and a number of "crossover" courses that bridge the liberal arts and technologies.

I have won regional, national, and international awards for my work in the IT field, and have also won awards for my teaching and research efforts as a University professor.

Ready to start learning?
Take This Course