Use createStub() to return canned values without verifying call expectations — simpler than mocks.
Stubs
// Stubs return canned responses without expectations
class PaymentTest extends TestCase {
public function testProcessPaymentSuccess(): void {
// Stub — we control what it returns, no expectations
$gateway = $this->createStub(PaymentGateway::class);
$gateway->method("charge")->willReturn(["status" => "success"]);
$service = new PaymentService($gateway);
$result = $service->process(100.00);
$this->assertEquals("success", $result["status"]);
}
public function testProcessPaymentFailure(): void {
$gateway = $this->createStub(PaymentGateway::class);
$gateway->method("charge")->willReturn(["status" => "failed"]);
$service = new PaymentService($gateway);
$this->expectException(PaymentFailedException::class);
$service->process(100.00);
}
// Stub throwing exceptions
public function testGatewayError(): void {
$gateway = $this->createStub(PaymentGateway::class);
$gateway->method("charge")->willThrowException(new GatewayException("Timeout"));
$this->expectException(GatewayException::class);
(new PaymentService($gateway))->process(50.00);
}
}