We will give a review of the different tests that can be integrated into PHP using the PHPUnit Framework. Discovering what needs to be covered at the lower levels of development and how to execute them.
This course can be complemented with practical cases in workshop mode in subsequent days on projects indicated by the applicant.
Requirements
- Knowledge of PHP (medium)
- Knowledge of Object Orientation (medium)
- Terminal handling (basic)
- Have PHP / Composer installed
- Have an IDE installed (PHPStorm, Netbeans, Visual Studio Code)
What you will learn
Course modules
Introduction
We review the different tests that can be integrated into PHP using the PHPUnit framework, and what needs to be covered at the lower levels of development.
Local work environment setup
Preparing the machine to run tests: installing PHPUnit with Composer and configuring the IDE to launch them and read the results.
- Composer
- IDE configuration
Fundamentals
The building blocks of a test: assertions, how to combine several of them, and the conventions that keep a test suite readable.
- Introduction to assertions
- Using multiple assertions
- Correcting bugs through testing
- Naming conventions and standards
PHPUnit XML configuration
Where PHPUnit reads its settings from and how to adapt the XML configuration file to the project.
Test dependencies, fixtures and exceptions
Controlling the state a test needs: dependencies between tests, set-up and tear-down, fixtures, and checking that exceptions are thrown.
- Dependencies between tests
- SetUp and TearDown
- Test fixtures
- Exceptions test
Mocks and stubs
Isolating the code under test from its external dependencies using mock objects, dependency injection and the Mockery framework.
- Creating mock objects to avoid external dependencies
- Dependency injection
- Mockery - Framework
TDD - Test Driven Development
Turning testing into a development method: writing the test before the code and using it to drive and refactor the implementation.
- Writing tests before code
- Targeted development test
- Code refactor with tests
Advanced test
Reaching the parts of a class that are not directly accessible: protected and private methods and attributes, and static methods.
- Protected / private methods
- Protected / private attributes
- Static methods
Conclusions
Recap of what has been covered and how to carry on applying it to the project's own code.
