
This course includes our updated coding exercises so you can practice your skills as you learn.
See a demo
Introduction to the course, and how to get the most out of it.
Information about the version of PHPUnit used on the course.
Make sure you can run PHP on the command line.
Make sure you have a code editor installed.
Install PHPUnit as a PHAR PHP executable file, and configure aliases to run it easily from the command line.
Install PHPUnit using Composer, and configure aliases to run it easily from the command line.
Write your first test using PHPUnit.
Write a test for a function that's in a separate file.
How to write multiple test methods to test the same code.
Test methods in a class defined in a separate file.
Using various flags on the command line to control PHPUnit's output.
How to run code automatically before the tests are executed.
Load class files automatically with a PSR-4 autoloader.
Configure Composer to autoload class files and function scripts.
How to configure PHPUnit using a configuration file.
Organise test classes into test suites so they can be run separately.
Specify the filter flag to only run specific tests.
How to mark methods for execution as test methods.
How to mark tests as incomplete, so you can add placeholder tests to implement later.
Fixing errors and assertion failures when testing.
Using data providers to reduce repetition.
Learn when multiple assertions in a single method are used.
Configure test methods to be dependent on others, and use the data from one in another.
Set up each test method with the same state to remove repetition.
How adding new test methods is simpler when the initial state is known.
How to test for expected exceptions being thrown.
Learn why testing classes in isolation is necessary.
Use dependency injection to supply objects that a class uses so they can easily be replaced.
Create test stubs to replace objects a class depends on.
Configure a test stub when expecting exceptions.
Using mocks instead of stubs to check for how the test double is used.
Create a custom mock object instead of accepting the defaults.
Configure an empty project to start TDD.
The fundamental process of TDD.
Refactor the code while obtaining the same test results.
Add a classes that emerges from the TDD process.
Add tests that require existing code to be modified.
Add tests that require additional functionality to emerge from the TDD process.
Install a driver that PHPUnit uses to generate a code coverage report.
Configuration required for PHPUnit to use Xdebug.
Tell PHPUnit where the source code is located on disk.
Learn the options needed to generate code coverage reports when running tests.
Regenerate a code coverage report to show increased code coverage.
Work with an example of a class with non-public methods, and discuss what should be tested and what shouldn't.
Using inheritance to change a method's visibility so it can be tested.
Using reflection to execute a non-public method so it can be tested.
Using reflection to access protected properties so they can be tested.
How to avoid a notice when creating mock objects without expectations (introduced in PHPUnit 12.5.0)
Use various techniques to test concrete members of an abstract class.
If you are a PHP developer and you want to learn how to write better code, with fewer bugs, you will need to know how to write unit tests.
Learn how to unit test your PHP code using the PHPUnit testing framework.
Learn what unit tests are
Learn how to write unit tests using PHPUnit
Build a test suite for PHP functions and classes
Make test methods dependent on each other
Use test doubles: mocks and stubs, including how to use Mockery
Learn how and why you should use dependency injection
Learn test-driven development (TDD)
Learn how to test protected and private methods and properties (and whether you actually should or not!)
Learn how to test abstract classes
A skill that will enable you to write higher quality code, in a shorter time, with better documentation and fewer bugs.
Learning how to write unit tests for your code will make you write better code. With automated tests, you can find bugs more easily, and if you break something you'll notice straight away.
Many experienced developers write unit tests for their code. It's a repeatable way of checking that the code works as intended, and also serves as documentation. Many popular PHP projects are unit tested, such as WordPress and Moodle, and if you use frameworks such as Laravel or Symfony, these provide tight integration with the PHPUnit testing framework.
Content and Overview
How do you currently test your code? Maybe you run your code in a browser, use var_dump a lot and so on. The problem with this is that you only tend to test new code - you don't go back and test old code. What if you add some new code that breaks the old code?
With automated unit tests, you have more control. If your new code breaks something, you'll notice straight away. You can fix bugs more easily, thereby reducing development time.
Tests are also a form of documentation, just like comments: they serve to show why the code has been written that way.
Suitable for all PHP developers, you'll start by learning the basics of unit testing PHP code, including how to use assertions.
Then you'll learn how to test functions and classes, and how to configure the PHPUnit framework.
Next you'll learn how to use test dependencies, fixtures and how to test for exceptions.
You'll also see how test doubles work - mock objects and stub methods.
Then you'll learn how to write code using test-driven development - writing the tests first, then the code.
You'll also learn how to test protected and private class methods and properties, and whether this is actually a good idea or not.
Testing static methods is covered in detail, including several methods for dealing with static dependencies.
When you complete the course you'll be able to write unit tests for your existing PHP code, and develop new code using test-driven development.
Complete with all the code shown in the lectures, you'll be able to work alongside the instructor and will receive a verifiable certificate of completion upon finishing the course.
Also, at all times throughout the course you have access to the instructor in the Q&A section to ask for help with any topic related to the course.
Enrol now and add unit testing to your PHP skillset!