Global Scope strikes again!
A colleague helped me out yesterday to discover why the Vehicle was not being retrieved. It is being created and attached to the Parking instance, but something kept preventing me from "seeing" it.
That something was my Global Scope on the model:
protected static function booted()
{
static::addGlobalScope('user', function (Builder $query) {
$query->where('user_id', auth()->id());
});
}
Its purpose is to prevent anyone who does not own the vehicle from getting information about it. Or, put another way, it is to make sure the authenticated user can only retrieve their own vehicles. And since my test was creating a new Vehicle using a factory, which in turn creates a new user to tie to it, the $user that I was actingAs() in the test was not the owner of the Vehicle. Thus, the test couldn't retrieve it.
The fix was simple. Either remove the global scope or simply account for it when setting up the data for the test. I chose the latter (simplifying the count in the process):
$user = User::factory()->create();
$parkings = Parking::factory(5)
->for(Vehicle::factory()
->hasAttached($user)
)
->for(Shed::factory())
->sequence(
[...]
Problem solved.