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.
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.
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.
Use composer to install PHPUnit
Let's configure PHP Unit and get it working with our code.
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.
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.
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.
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.
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.
Acceptance testing is all about planning.
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.
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.