Introduction to Testing with PHPUnit
4.2 (134 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.
723 students enrolled
Wishlisted Wishlist

Please confirm that you want to add Introduction to Testing with PHPUnit to your Wishlist.

Add to Wishlist

Introduction to Testing with PHPUnit

An introduction to testing your code with PHPUnit
Best Seller
4.2 (134 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.
723 students enrolled
Created by Trevor Sawler
Last updated 1/2016
Current price: $10 Original price: $20 Discount: 50% off
5 hours left at this price!
30-Day Money-Back Guarantee
  • 5 hours on-demand video
  • 3 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
What Will I Learn?
  • 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
View Curriculum
  • You should have at least a basic knowledge of PHP
  • You should have at least a basic understanding of Object Oriented programming principles

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.

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.
Students Who Viewed This Course Also Viewed
Curriculum For This Course
33 Lectures
Why Testing is Critical to Good Development
1 Lecture 06:19

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

Preview 06:19
Setting up our project files
5 Lectures 42:21

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.

Choosing an IDE - Atom, or your choice

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

Preview 10:47

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.
Installing Vagrant, Virtualbox and GitBash for Windows

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...

Getting our sample code up and running

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.

Quick overview of the code
Setting up and using PHPUnit
3 Lectures 08:49

Use composer to install PHPUnit

Installing PHPUnit

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

Configuring PHPUnit

Code Coverage
Basic Unit Testing
4 Lectures 34:45

Let's write our first unit test.

Preview 07:49

Writing slightly more complex tests against our code base.

Testing the Validator in more detail

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

Cleaning up the code a bit

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

Preview 06:27
Getting Our Code Ready for Testing
3 Lectures 21:32

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

Dependency Injection, and why you should use it

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

Choosing a Dependency Injection container

Getting started with dependency injection.

Setting up Dependency Injection in our codebase
Mocking Objects & Using mockBuilder & @dataProvider
5 Lectures 01:05:22

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

Mocking objects & updating our tests to use DI

Moving our validator class into the DI container.

Updating the Validator class to use DI & more about mockBuilder

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

Finally testing the validate function, and a smoke test

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

Using @dataProvider for tests

Making our tests cover even more code.

Improving our validation tests
Integration Testing
6 Lectures 01:02:55

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

Setting up integration tests and testing the User model

A sample integration test for the getRows method.

Preview 09:59

What about protected methods? Can I test those?

Aside: Testing protected methods

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

Testing the PageController->getShowHomePage() method

Let's test showing a page from our site.

Testing the getShowPage method

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

Finishing the PageController tests (and a total hack, just for fun)
Functional Testing
3 Lectures 36:16

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

Geting rid of our Hack & Writing functional tests

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.

Writing tests for pages that use the session

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

Additional functional tests
Acceptance Testing Strategies
1 Lecture 07:12

Acceptance testing is all about planning.

This is the easy part... sort of
Final Thoughts & Housekeeping
2 Lectures 04:29

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

Let's not test our tests... excluding directories & files from test suites

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

Where to go from here
About the Instructor
Trevor Sawler
4.3 Average rating
380 Reviews
1,942 Students
3 Courses

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.