andre liem

Published on September 15th, 2021 Laravel Dependency Injection + Unit Tests

When writing tests in Laravel, use the app() helper to resolve the class. Laravel will automatically inject the dependencies.


// In the phpunit 
$orderInvoice = app(CreateBasketInvoiceAction::class)($basket);

// Action
public function __construct(CreateOrderAction $createOrderAction)
{
    $this->createOrderAction = $createOrderAction;
}