Mocking
Mocks replace real dependencies with controlled fakes. PHPUnit's createMock() generates an implementation from an interface or class.
Mocks replace real dependencies with controlled fakes. PHPUnit's createMock() generates an implementation from an interface or class.
class OrderServiceTest extends TestCase {
public function test_sends_confirmation_email(): void {
$mailer = $this->createMock(Mailer::class);
$mailer->expects($this->once())
->method("send")
->with($this->equalTo("user@example.com"));
$service = new OrderService($mailer);
$service->place(new Order(["email" => "user@example.com"]));
}
}
Inject dependencies via constructor to make classes mockable — avoid static calls in testable code.