Mocking Dependencies with PHPUnit
Create mock objects to isolate units and control collaborator behavior.
Why Mock?
Mocking replaces a real dependency (database, email service, HTTP client) with a controllable fake so tests are fast, isolated, and deterministic.
createMock()
createMock(ClassName::class) creates a mock object that implements all methods of the class, returning null by default.
<?php
$mailer = $this->createMock(Mailer::class);
// All methods return null by defaultAll lessons in this course
- PHPUnit Setup and First Test
- Assertions and Test Methods
- Data Providers for Parameterized Tests
- Mocking Dependencies with PHPUnit