The service container resolves dependencies and can bind abstractions to implementations.
Example:
$this->app->bind(PaymentGateway::class, StripePaymentGateway::class);
A service can depend on:
public function __construct(PaymentGateway $gateway) {}
For tests, the binding can be replaced with a fake or mock implementation.
Scenario:
An order service normally charges a real payment gateway, but automated tests should not send real payments. Dependency injection allows the payment implementation to be substituted.
This is one reason dependency inversion and interface-based design can improve testability.
0 comments:
Post a Comment
Thanks